From 30523d7db378ccc898ad923422a3dcddba4c2852 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Sun, 20 Dec 2015 17:10:24 -0600 Subject: [PATCH] lnwallet: import badge's script utils in new file --- lnwallet/script_utils.go | 85 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 lnwallet/script_utils.go diff --git a/lnwallet/script_utils.go b/lnwallet/script_utils.go new file mode 100644 index 00000000..6de56906 --- /dev/null +++ b/lnwallet/script_utils.go @@ -0,0 +1,85 @@ +package lnwallet + +import ( + "bytes" + "fmt" + + "github.com/btcsuite/btcd/txscript" + "github.com/btcsuite/btcd/wire" + "github.com/btcsuite/btcutil" +) + +// scriptHashPkScript... +func scriptHashPkScript(scriptBytes []byte) ([]byte, error) { + bldr := txscript.NewScriptBuilder() + bldr.AddOp(txscript.OP_HASH160) + bldr.AddData(btcutil.Hash160(scriptBytes)) + bldr.AddOp(txscript.OP_EQUAL) + return bldr.Script() +} + +// getFundingPkScript generates the non-p2sh'd multisig script for 2 of 2 +// pubkeys. +func genFundingPkScript(aPub, bPub []byte) ([]byte, error) { + if len(aPub) != 33 || len(bPub) != 33 { + return nil, fmt.Errorf("Pubkey size error. Compressed pubkeys only") + } + + if bytes.Compare(aPub, bPub) == -1 { // swap to sort pubkeys if needed + aPub, bPub = bPub, aPub + } + + bldr := txscript.NewScriptBuilder() + // Require 2 signatures, so from both of the pubkeys + bldr.AddOp(txscript.OP_2) + // add both pubkeys (sorted) + bldr.AddData(aPub) + bldr.AddData(bPub) + // 2 keys total. In case that wasn't obvious. + bldr.AddOp(txscript.OP_2) + // Good ol OP_CHECKMULTISIG. Don't forget the zero! + bldr.AddOp(txscript.OP_CHECKMULTISIG) + // get byte slice + return bldr.Script() +} + +// fundMultiSigOut creates a TxOut for the funding transaction. +// Give it the two pubkeys and it'll give you the p2sh'd txout. +// You don't have to remember the p2sh preimage, as long as you remember the +// pubkeys involved. +func fundMultiSigOut(aPub, bPub []byte, amt int64) ([]byte, *wire.TxOut, error) { + if amt < 0 { + return nil, nil, fmt.Errorf("Can't create FundTx script with negative coins") + } + + // p2shify + redeemScript, err := genFundingPkScript(aPub, bPub) + if err != nil { + return nil, nil, err + } + pkScript, err := scriptHashPkScript(redeemScript) + if err != nil { + return nil, nil, err + } + + return redeemScript, wire.NewTxOut(amt, pkScript), nil +} + +// the scriptsig to put on a P2SH input +func spendMultiSig(redeemScript, sigA, sigB []byte) ([]byte, error) { + bldr := txscript.NewScriptBuilder() + + // add a 0 for some multisig fun + bldr.AddOp(txscript.OP_0) + + // add sigA + bldr.AddData(sigA) + // add sigB + bldr.AddData(sigB) + + // preimage goes on AT THE ENDDDD + bldr.AddData(redeemScript) + + // that's all, get bytes + return bldr.Script() +}