Skip to content

Instantly share code, notes, and snippets.

@Jwata
Created October 24, 2018 08:21
Show Gist options
  • Select an option

  • Save Jwata/45730b33390f15aaced047e58a66b57d to your computer and use it in GitHub Desktop.

Select an option

Save Jwata/45730b33390f15aaced047e58a66b57d to your computer and use it in GitHub Desktop.
package main
import (
"testing"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcutil/hdkeychain"
"github.com/stretchr/testify/assert"
)
func TestScriptEngine(t *testing.T) {
assert := assert.New(t)
assert.True(true)
_, pub := randKeys()
txVersion := int32(2)
tx := wire.NewMsgTx(txVersion)
// prepare fake txin
prevOut := wire.NewOutPoint(&chainhash.Hash{}, ^uint32(0))
txIn := wire.NewTxIn(prevOut, []byte{txscript.OP_0, txscript.OP_0}, nil)
tx.AddTxIn(txIn)
pkScript, _ := P2WPKHpkScript(pub)
txOut := wire.NewTxOut(100000000, pkScript)
tx.AddTxOut(txOut)
txHash := tx.TxHash()
redeemTx := wire.NewMsgTx(txVersion)
prevOut = wire.NewOutPoint(&txHash, 0)
txIn = wire.NewTxIn(prevOut, nil, nil)
redeemTx.AddTxIn(txIn)
txOut = wire.NewTxOut(0, nil)
redeemTx.AddTxOut(txOut)
flags := txscript.ScriptBip16 | txscript.ScriptVerifyWitness
vm, err := txscript.NewEngine(pkScript, redeemTx, 0, flags, nil, nil, -1)
assert.Nil(err)
err = vm.Execute()
assert.Nil(err)
}
func randKeys() (*btcec.PrivateKey, *btcec.PublicKey) {
seed, _ := hdkeychain.GenerateSeed(hdkeychain.MinSeedBytes)
extKey, _ := hdkeychain.NewMaster(seed, &chaincfg.RegressionNetParams)
pub, _ := extKey.ECPubKey()
priv, _ := extKey.ECPrivKey()
return priv, pub
}
// P2WPKHpkScript creates pk script is OP_0 + HASH160(<public key>)
func P2WPKHpkScript(pub *btcec.PublicKey) ([]byte, error) {
builder := txscript.NewScriptBuilder()
builder.AddOp(txscript.OP_0)
builder.AddData(btcutil.Hash160(pub.SerializeCompressed()))
return builder.Script()
}
@Jwata
Copy link
Author

Jwata commented Oct 24, 2018

WIP, but seems executing script.

Expected nil, but got: txscript.Error{ErrorCode:60, Description:"should have exactly two items in witness, instead have 0"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment