Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 36 additions & 13 deletions lnutil/dlclib.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,26 +452,49 @@ func SettlementTx(c *DlcContract, d DlcContractDivision,
feeOurs := feeEach
feeTheirs := feeEach
valueOurs := d.ValueOurs
// We don't have enough to pay for a fee. We get 0, our contract partner
// pays the rest of the fee
if valueOurs < feeEach {
feeOurs = valueOurs
valueOurs = 0
} else {
valueOurs = d.ValueOurs - feeOurs
}

// This code leads to errors at edges of the interval so I disable it

// // We don't have enough to pay for a fee. We get 0, our contract partner
// // pays the rest of the fee
// if valueOurs < feeEach {
// feeOurs = valueOurs
// valueOurs = 0
// } else {
// valueOurs = d.ValueOurs - feeOurs
// }
totalContractValue := c.TheirFundingAmount + c.OurFundingAmount
valueTheirs := totalContractValue - d.ValueOurs

if valueTheirs < feeEach {
feeTheirs = valueTheirs
valueTheirs = 0
feeOurs = totalFee - feeTheirs

// This code leads to errors at edges of the interval so I disable it

// if valueTheirs < feeEach {
// feeTheirs = valueTheirs
// valueTheirs = 0
// feeOurs = totalFee - feeTheirs
// valueOurs = d.ValueOurs - feeOurs
// } else {
// valueTheirs -= feeTheirs
// }


// There is a simplified code but it works at edges of the interval.

if valueOurs == 0 {
valueTheirs = valueTheirs - totalFee
}else{
valueOurs = d.ValueOurs - feeOurs
} else {
}

if valueTheirs == 0 {
valueOurs = valueOurs - totalFee
}else{
valueTheirs -= feeTheirs
}



var buf bytes.Buffer
binary.Write(&buf, binary.BigEndian, uint64(0))
binary.Write(&buf, binary.BigEndian, uint64(0))
Expand Down
78 changes: 45 additions & 33 deletions qln/dlc.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,46 +609,58 @@ func (nd *LitNode) SettleContract(cIdx uint64, oracleValue int64, oracleSig [32]
return [32]byte{}, [32]byte{}, err
}

// TODO: Claim the contract settlement output back to our wallet - otherwise the peer can claim it after locktime.
txClaim := wire.NewMsgTx()
txClaim.Version = 2

settleOutpoint := wire.OutPoint{Hash: settleTx.TxHash(), Index: 0}
txClaim.AddTxIn(wire.NewTxIn(&settleOutpoint, nil, nil))
if ( d.ValueOurs != 0){

addr, err := wal.NewAdr()
txClaim.AddTxOut(wire.NewTxOut(d.ValueOurs-1000, lnutil.DirectWPKHScriptFromPKH(addr))) // todo calc fee - fee is double here because the contract output already had the fee deducted in the settlement TX
fee := int64(500)

// TODO: Claim the contract settlement output back to our wallet - otherwise the peer can claim it after locktime.
txClaim := wire.NewMsgTx()
txClaim.Version = 2

kg.Step[2] = UseContractPayoutBase
privSpend, _ := wal.GetPriv(kg)
settleOutpoint := wire.OutPoint{Hash: settleTx.TxHash(), Index: 0}
txClaim.AddTxIn(wire.NewTxIn(&settleOutpoint, nil, nil))

pubSpend := wal.GetPub(kg)
privOracle, pubOracle := koblitz.PrivKeyFromBytes(koblitz.S256(), oracleSig[:])
privContractOutput := lnutil.CombinePrivateKeys(privSpend, privOracle)
addr, err := wal.NewAdr()
txClaim.AddTxOut(wire.NewTxOut(settleTx.TxOut[0].Value-fee, lnutil.DirectWPKHScriptFromPKH(addr))) // todo calc fee - fee is double here because the contract output already had the fee deducted in the settlement TX

var pubOracleBytes [33]byte
copy(pubOracleBytes[:], pubOracle.SerializeCompressed())
var pubSpendBytes [33]byte
copy(pubSpendBytes[:], pubSpend.SerializeCompressed())
kg.Step[2] = UseContractPayoutBase
privSpend, _ := wal.GetPriv(kg)

settleScript := lnutil.DlcCommitScript(c.OurPayoutBase, pubOracleBytes, c.TheirPayoutBase, 5)
err = nd.SignClaimTx(txClaim, settleTx.TxOut[0].Value, settleScript, privContractOutput, false)
if err != nil {
logging.Errorf("SettleContract SignClaimTx err %s", err.Error())
return [32]byte{}, [32]byte{}, err
}
pubSpend := wal.GetPub(kg)
privOracle, pubOracle := koblitz.PrivKeyFromBytes(koblitz.S256(), oracleSig[:])
privContractOutput := lnutil.CombinePrivateKeys(privSpend, privOracle)

// Claim TX should be valid here, so publish it.
err = wal.DirectSendTx(txClaim)
if err != nil {
logging.Errorf("SettleContract DirectSendTx (claim) err %s", err.Error())
return [32]byte{}, [32]byte{}, err
}
var pubOracleBytes [33]byte
copy(pubOracleBytes[:], pubOracle.SerializeCompressed())
var pubSpendBytes [33]byte
copy(pubSpendBytes[:], pubSpend.SerializeCompressed())

settleScript := lnutil.DlcCommitScript(c.OurPayoutBase, pubOracleBytes, c.TheirPayoutBase, 5)
err = nd.SignClaimTx(txClaim, settleTx.TxOut[0].Value, settleScript, privContractOutput, false)
if err != nil {
logging.Errorf("SettleContract SignClaimTx err %s", err.Error())
return [32]byte{}, [32]byte{}, err
}

// Claim TX should be valid here, so publish it.
err = wal.DirectSendTx(txClaim)
if err != nil {
logging.Errorf("SettleContract DirectSendTx (claim) err %s", err.Error())
return [32]byte{}, [32]byte{}, err
}

c.Status = lnutil.ContractStatusClosed
err = nd.DlcManager.SaveContract(c)
if err != nil {
return [32]byte{}, [32]byte{}, err
}
return settleTx.TxHash(), txClaim.TxHash(), nil

}else{

return settleTx.TxHash(), [32]byte{}, nil

c.Status = lnutil.ContractStatusClosed
err = nd.DlcManager.SaveContract(c)
if err != nil {
return [32]byte{}, [32]byte{}, err
}
return settleTx.TxHash(), txClaim.TxHash(), nil

}