Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use btcd/txscript to build a custom locking and unlocking script. #1515

Open
iamgamelover opened this issue Jan 9, 2020 · 2 comments

Comments

@iamgamelover
Copy link

  1. First, generate a P2SH multisig address including some custom content.

      script := txscript.NewScriptBuilder()
    
      // custom content
    

    script.AddOp(txscript.OP_HASH160)
    script.AddData(Hash_Preimage_R)
    script.AddOp(txscript.OP_EQUALVERIFY)

      // standard content
    

    script.AddOp(txscript.OP_2)
    script.AddData(aPubKey)
    script.AddData(bPubKey)
    script.AddOp(txscript.OP_2)
    script.AddOp(txscript.OP_CHECKMULTISIG)
    return script.Script()

Then got an address like 2Mv12R1VNdiSzeFiDxs2WVxeBZzTScrs8Q6

  1. Sent some test bitcoin to the address above.
  2. Construct a transaction with btcd/txscript and btcd/wire to spend a UTXO of the address above.

4. How to write the unlocking script?
Currently, I'm using txscript.SignTxOutput reference from https://sourcegraph.com/github.com/btcsuite/btcd/-/blob/txscript/sign_test.go#L344

     // signature ...
sigScript, err := txscript.SignTxOutput(&chaincfg.TestNet3Params,
	tx, 0, scriptPkScript, txscript.SigHashAll,
	mkGetKey(map[string]addressToKey{
		address1.EncodeAddress(): {key1.PrivKey, true},
		address2.EncodeAddress(): {key2.PrivKey, true},
	 }), mkGetScript(map[string][]byte{
		scriptAddr.EncodeAddress(): custom_pkScript,
	}), nil)

if err != nil {
	fmt.Println("Sign has wrong : ", err)
	return
}

tx.TxIn[0].SignatureScript = sigScript
  1. But got a error: Sign has wrong : can't sign unknown transactions

Anybody have some idea? Thank you very much!

@mahdiidarabi
Copy link

read https://developer.bitcoin.org/devguide/transactions.html#p2pkh-script-validation to get the idea behind bitcoin script language, you should provide some op-codes result in true on stack after completely be doen.

@neocarmack
Copy link

import(
    "github.com/btcsuite/btcd/txscript"
    "github.com/btcsuite/btcd/wire"
)

tx := wire.NewMsgTx(2)
tx.AddTxOut(wire.NewTxOut(int64(*outputvalueFlag*1e8), script()))

func script() ([]byte) {
    script := txscript.NewScriptBuilder()
    script.AddInt64(1)
    script.AddData(bytes[0:65])
    script.AddInt64(1)
    script.AddOp(txscript.OP_CHECKMULTISIG)
    return script.Script()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants