2019-12-11 18:43:47 -08:00
|
|
|
package clmscript
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2020-01-23 16:04:40 -08:00
|
|
|
"crypto/sha256"
|
2019-12-11 18:43:47 -08:00
|
|
|
|
2020-01-23 16:04:33 -08:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
2019-12-11 18:43:47 -08:00
|
|
|
"github.com/btcsuite/btcd/txscript"
|
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
|
"github.com/lightningnetwork/lnd/input"
|
|
|
|
|
"github.com/lightningnetwork/lnd/keychain"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
// AccountKeyFamily is the key family used to derive keys which will be
|
|
|
|
|
// used in the 2 of 2 multi-sig construction of a CLM account.
|
|
|
|
|
//
|
|
|
|
|
// TODO(wilmer): decide on actual value.
|
|
|
|
|
AccountKeyFamily keychain.KeyFamily = 220
|
2020-01-29 16:18:45 -08:00
|
|
|
|
|
|
|
|
// AccountWitnessScriptSize: 79 bytes
|
|
|
|
|
// - OP_DATA: 1 byte (trader_key length)
|
|
|
|
|
// - <trader_key>: 33 bytes
|
|
|
|
|
// - OP_CHECKSIGVERIFY: 1 byte
|
|
|
|
|
// - OP_DATA: 1 byte (auctioneer_key length)
|
|
|
|
|
// - <auctioneer_key>: 33 bytes
|
|
|
|
|
// - OP_CHECKSIG: 1 byte
|
|
|
|
|
// - OP_IFDUP: 1 byte
|
|
|
|
|
// - OP_NOTIF: 1 byte
|
|
|
|
|
// - OP_DATA: 1 byte (account_expiry length)
|
|
|
|
|
// - <account_expiry>: 4 bytes
|
|
|
|
|
// - OP_CHECKLOCKTIMEVERIFY: 1 byte
|
|
|
|
|
// - OP_ENDIF: 1 byte
|
|
|
|
|
AccountWitnessScriptSize = 1 + 33 + 1 + 1 + 33 + 1 + 1 + 1 + 1 + 4 + 1 + 1
|
|
|
|
|
|
|
|
|
|
// MultiSigWitnessSize: 227 bytes
|
2020-03-08 14:59:15 -07:00
|
|
|
// - num_witness_elements: 1 byte
|
|
|
|
|
// - trader_sig_varint_len: 1 byte
|
2020-01-29 16:18:45 -08:00
|
|
|
// - <trader_sig>: 73 bytes
|
2020-03-08 14:59:15 -07:00
|
|
|
// - auctioneer_sig_varint_len: 1 byte
|
2020-01-29 16:18:45 -08:00
|
|
|
// - <auctioneer_sig>: 73 bytes
|
2020-03-08 14:59:15 -07:00
|
|
|
// - witness_script_varint_len: 1 byte
|
2020-01-29 16:18:45 -08:00
|
|
|
// - <witness_script>: 79 bytes
|
2020-03-08 14:59:15 -07:00
|
|
|
MultiSigWitnessSize = 1 + 1 + 73 + 1 + 73 + 1 + AccountWitnessScriptSize
|
2020-01-29 16:18:45 -08:00
|
|
|
|
|
|
|
|
// ExpiryWitnessSize: 154 bytes
|
2020-03-08 14:59:15 -07:00
|
|
|
// - num_witness_elements: 1 byte
|
|
|
|
|
// - trader_sig_varint_len: 1 byte (trader_sig length)
|
2020-01-29 16:18:45 -08:00
|
|
|
// - <trader_sig>: 73 bytes
|
2020-03-08 14:59:15 -07:00
|
|
|
// - witness_script_varint_len: 1 byte (nil length)
|
2020-01-29 16:18:45 -08:00
|
|
|
// - <witness_script>: 79 bytes
|
2020-03-08 14:59:15 -07:00
|
|
|
ExpiryWitnessSize = 1 + 1 + 73 + 1 + AccountWitnessScriptSize
|
2019-12-11 18:43:47 -08:00
|
|
|
)
|
|
|
|
|
|
2020-01-23 16:04:40 -08:00
|
|
|
// TraderKeyTweak computes the tweak based on the current per-batch key and
|
2020-01-29 16:18:45 -08:00
|
|
|
// shared secret that should be applied to an account's base trader key. The
|
|
|
|
|
// tweak is computed as the following:
|
2020-01-23 16:04:40 -08:00
|
|
|
//
|
|
|
|
|
// tweak = sha256(batchKey || secret || traderKey)
|
|
|
|
|
func TraderKeyTweak(batchKey *btcec.PublicKey, secret [32]byte,
|
|
|
|
|
traderKey *btcec.PublicKey) []byte {
|
|
|
|
|
|
|
|
|
|
h := sha256.New()
|
|
|
|
|
_, _ = h.Write(batchKey.SerializeCompressed())
|
|
|
|
|
_, _ = h.Write(secret[:])
|
|
|
|
|
_, _ = h.Write(traderKey.SerializeCompressed())
|
|
|
|
|
return h.Sum(nil)
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-29 16:18:45 -08:00
|
|
|
// AuctioneerKeyTweak computes the tweak based on the tweaked trader's key that
|
|
|
|
|
// should be applied to an account's auctioneer base key. The tweak is computed
|
|
|
|
|
// as the following:
|
2019-12-11 18:43:47 -08:00
|
|
|
//
|
2020-01-29 16:18:45 -08:00
|
|
|
// traderKeyTweak = sha256(batchKey || secret || traderKey)
|
|
|
|
|
// tweakedTraderKey = (traderKey + traderKeyTweak) * G
|
|
|
|
|
// auctioneerKeyTweak = sha256(tweakedTraderKey || auctioneerKey)
|
|
|
|
|
func AuctioneerKeyTweak(traderKey, auctioneerKey, batchKey *btcec.PublicKey,
|
|
|
|
|
secret [32]byte) []byte {
|
|
|
|
|
|
|
|
|
|
traderKeyTweak := TraderKeyTweak(batchKey, secret, traderKey)
|
|
|
|
|
tweakedTraderKey := input.TweakPubKeyWithTweak(traderKey, traderKeyTweak)
|
|
|
|
|
return input.SingleTweakBytes(tweakedTraderKey, auctioneerKey)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AccountWitnessScript returns the witness script of an account.
|
|
|
|
|
func AccountWitnessScript(expiry uint32, traderKey, auctioneerKey,
|
2020-01-23 16:04:40 -08:00
|
|
|
batchKey *btcec.PublicKey, secret [32]byte) ([]byte, error) {
|
|
|
|
|
|
|
|
|
|
traderKeyTweak := TraderKeyTweak(batchKey, secret, traderKey)
|
|
|
|
|
tweakedTraderKey := input.TweakPubKeyWithTweak(traderKey, traderKeyTweak)
|
|
|
|
|
tweakedAuctioneerKey := input.TweakPubKey(auctioneerKey, tweakedTraderKey)
|
|
|
|
|
|
2019-12-11 18:43:47 -08:00
|
|
|
builder := txscript.NewScriptBuilder()
|
|
|
|
|
|
2020-01-23 16:04:40 -08:00
|
|
|
builder.AddData(tweakedTraderKey.SerializeCompressed())
|
2020-01-24 15:28:23 -08:00
|
|
|
builder.AddOp(txscript.OP_CHECKSIGVERIFY)
|
2019-12-11 18:43:47 -08:00
|
|
|
|
2020-01-23 16:04:40 -08:00
|
|
|
builder.AddData(tweakedAuctioneerKey.SerializeCompressed())
|
2020-01-24 15:28:23 -08:00
|
|
|
builder.AddOp(txscript.OP_CHECKSIG)
|
2019-12-11 18:43:47 -08:00
|
|
|
|
2020-01-24 15:28:23 -08:00
|
|
|
builder.AddOp(txscript.OP_IFDUP)
|
|
|
|
|
builder.AddOp(txscript.OP_NOTIF)
|
|
|
|
|
builder.AddInt64(int64(expiry))
|
|
|
|
|
builder.AddOp(txscript.OP_CHECKLOCKTIMEVERIFY)
|
2019-12-11 18:43:47 -08:00
|
|
|
builder.AddOp(txscript.OP_ENDIF)
|
|
|
|
|
|
2020-01-29 16:18:45 -08:00
|
|
|
return builder.Script()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AccountScript returns the output script of an account on-chain.
|
|
|
|
|
//
|
|
|
|
|
// <trader_key> OP_CHECKSIGVERIFY
|
|
|
|
|
// <auctioneer_key> OP_CHECKSIG OP_IFDUP OP_NOTIF
|
|
|
|
|
// <account_expiry> OP_CHECKLOCKTIMEVERIFY
|
|
|
|
|
// OP_ENDIF
|
|
|
|
|
func AccountScript(expiry uint32, traderKey, auctioneerKey,
|
|
|
|
|
batchKey *btcec.PublicKey, secret [32]byte) ([]byte, error) {
|
|
|
|
|
|
|
|
|
|
witnessScript, err := AccountWitnessScript(
|
|
|
|
|
expiry, traderKey, auctioneerKey, batchKey, secret,
|
|
|
|
|
)
|
2019-12-11 18:43:47 -08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-01-29 16:18:45 -08:00
|
|
|
return input.WitnessScriptHash(witnessScript)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SpendExpiry returns the witness required to spend an account through the
|
|
|
|
|
// expiration script path.
|
|
|
|
|
func SpendExpiry(witnessScript, traderSig []byte) wire.TxWitness {
|
|
|
|
|
witness := make(wire.TxWitness, 3)
|
|
|
|
|
witness[0] = nil // Invalid auctioneer sig to force expiry path.
|
|
|
|
|
witness[1] = traderSig
|
|
|
|
|
witness[2] = witnessScript
|
|
|
|
|
return witness
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SpendMultiSig returns the witness required to spend an account through the
|
|
|
|
|
// multi-sig script path.
|
|
|
|
|
func SpendMultiSig(witnessScript, traderSig, auctioneerSig []byte) wire.TxWitness {
|
|
|
|
|
witness := make(wire.TxWitness, 3)
|
|
|
|
|
witness[0] = auctioneerSig
|
|
|
|
|
witness[1] = traderSig
|
|
|
|
|
witness[2] = witnessScript
|
|
|
|
|
return witness
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsExpirySpend determines whether the provided witness corresponds to the
|
|
|
|
|
// expiration script path of an account.
|
|
|
|
|
func IsExpirySpend(witness wire.TxWitness) bool {
|
|
|
|
|
if len(witness) != 3 {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
if len(witness[0]) != 0 {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsMultiSigSpend determines whether the provided witness corresponds to the
|
|
|
|
|
// multi-sig script path of an account.
|
|
|
|
|
func IsMultiSigSpend(witness wire.TxWitness) bool {
|
|
|
|
|
if len(witness) != 3 {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
if len(witness[0]) == 0 {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
2019-12-11 18:43:47 -08:00
|
|
|
}
|
|
|
|
|
|
2020-01-23 16:04:38 -08:00
|
|
|
// IncrementKey increments the given key by the backing curve's base point.
|
|
|
|
|
func IncrementKey(key *btcec.PublicKey) *btcec.PublicKey {
|
|
|
|
|
curveParams := key.Curve.Params()
|
|
|
|
|
newX, newY := key.Curve.Add(key.X, key.Y, curveParams.Gx, curveParams.Gy)
|
|
|
|
|
return &btcec.PublicKey{
|
|
|
|
|
X: newX,
|
|
|
|
|
Y: newY,
|
|
|
|
|
Curve: btcec.S256(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-11 18:43:47 -08:00
|
|
|
// LocateOutputScript determines whether a transaction includes an output with a
|
|
|
|
|
// specific script. If it does, the output index is returned.
|
|
|
|
|
func LocateOutputScript(tx *wire.MsgTx, script []byte) (uint32, bool) {
|
|
|
|
|
for i, txOut := range tx.TxOut {
|
|
|
|
|
if !bytes.Equal(txOut.PkScript, script) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
return uint32(i), true
|
|
|
|
|
}
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|