pool/clmscript/script.go
Wilmer Paulino dca3f2717e account: implement account manager with support to create accounts
The account manager is a subsystem of the client that will be
responsible for management of all CLM accounts on-chain. This initial
version only supports opening accounts at the moment, but this will be
extended later on to also support closing and modifying them. Open
accounts are also watched for their confirmation and spend, though the
spend path remains unhandled.

The commit stands alone by itself, and is not yet integrated into the
server as we still need to satisfy its dependencies, which we'll do in
the following commits.
2020-01-10 16:57:56 -08:00

70 lines
1.7 KiB
Go

package clmscript
import (
"bytes"
"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
)
// AccountScript returns the witness script of an account on-chain.
//
// OP_IF
// <account expiry>
// OP_CHECKLOCKTIMEVERIFY
// OP_DROP
// <trader key>
// OP_CHECKSIG
// OP_ELSE
// OP_2 <trader key> <auctioneer key> OP_2
// OP_CHECKMULTISIG
// OP_ENDIF
func AccountScript(expiry uint32, traderKey, auctioneerKey [33]byte) ([]byte, error) {
builder := txscript.NewScriptBuilder()
builder.AddOp(txscript.OP_IF)
builder.AddInt64(int64(expiry))
builder.AddOp(txscript.OP_CHECKLOCKTIMEVERIFY)
builder.AddOp(txscript.OP_DROP)
builder.AddData(traderKey[:])
builder.AddOp(txscript.OP_CHECKSIG)
builder.AddOp(txscript.OP_ELSE)
builder.AddOp(txscript.OP_2)
builder.AddData(traderKey[:])
builder.AddData(auctioneerKey[:])
builder.AddOp(txscript.OP_2)
builder.AddOp(txscript.OP_CHECKMULTISIG)
builder.AddOp(txscript.OP_ENDIF)
script, err := builder.Script()
if err != nil {
return nil, err
}
return input.WitnessScriptHash(script)
}
// 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
}