本文整理汇总了Golang中github.com/conformal/btcwire.NewMsgTx函数的典型用法代码示例。如果您正苦于以下问题:Golang NewMsgTx函数的具体用法?Golang NewMsgTx怎么用?Golang NewMsgTx使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewMsgTx函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: makeTx
func makeTx(outputs []output, amount, value int64, toAddr, changeAddr string) (*btcwire.MsgTx, error) {
msgTx := btcwire.NewMsgTx()
for _, op := range outputs {
hash, err := btcwire.NewShaHashFromStr(op.TxHash)
if err != nil {
return nil, err
}
b, err := hex.DecodeString(op.Script)
if err != nil {
return nil, err
}
txIn := btcwire.NewTxIn(btcwire.NewOutPoint(hash, op.TxN), b)
msgTx.AddTxIn(txIn)
}
script, err := makeScriptPubKey(toAddr)
if err != nil {
return nil, err
}
txOut := btcwire.NewTxOut(value, script)
msgTx.AddTxOut(txOut)
if amount > value {
script, err = makeScriptPubKey(changeAddr)
if err != nil {
return nil, err
}
txOut := btcwire.NewTxOut(amount-value, script)
msgTx.AddTxOut(txOut)
}
return msgTx, nil
}
开发者ID:shevilangle,项目名称:sports,代码行数:33,代码来源:sign.go
示例2: createCoinbaseTx
// createCoinbaseTx returns a coinbase transaction paying an appropriate subsidy
// based on the passed block height to the provided address. When the address
// is nil, the coinbase transaction will instead be redeemable by anyone.
//
// See the comment for NewBlockTemplate for more information about why the nil
// address handling is useful.
func createCoinbaseTx(coinbaseScript []byte, nextBlockHeight int64, addr btcutil.Address) (*btcutil.Tx, error) {
// Create the script to pay to the provided payment address if one was
// specified. Otherwise create a script that allows the coinbase to be
// redeemable by anyone.
var pkScript []byte
if addr != nil {
var err error
pkScript, err = btcscript.PayToAddrScript(addr)
if err != nil {
return nil, err
}
} else {
scriptBuilder := btcscript.NewScriptBuilder()
pkScript = scriptBuilder.AddOp(btcscript.OP_TRUE).Script()
}
tx := btcwire.NewMsgTx()
tx.AddTxIn(&btcwire.TxIn{
// Coinbase transactions have no inputs, so previous outpoint is
// zero hash and max index.
PreviousOutPoint: *btcwire.NewOutPoint(&btcwire.ShaHash{},
btcwire.MaxPrevOutIndex),
SignatureScript: coinbaseScript,
Sequence: btcwire.MaxTxInSequenceNum,
})
tx.AddTxOut(&btcwire.TxOut{
Value: btcchain.CalcBlockSubsidy(nextBlockHeight,
activeNetParams.Params),
PkScript: pkScript,
})
return btcutil.NewTx(tx), nil
}
开发者ID:stoiclabs,项目名称:blockchainr,代码行数:38,代码来源:mining.go
示例3: TestTxSerialize
// TestTxSerialize tests MsgTx serialize and deserialize.
func TestTxSerialize(t *testing.T) {
noTx := btcwire.NewMsgTx()
noTx.Version = 1
noTxEncoded := []byte{
0x01, 0x00, 0x00, 0x00, // Version
0x00, // Varint for number of input transactions
0x00, // Varint for number of output transactions
0x00, 0x00, 0x00, 0x00, // Lock time
}
tests := []struct {
in *btcwire.MsgTx // Message to encode
out *btcwire.MsgTx // Expected decoded message
buf []byte // Serialized data
}{
// No transactions.
{
noTx,
noTx,
noTxEncoded,
},
// Multiple transactions.
{
multiTx,
multiTx,
multiTxEncoded,
},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
// Serialize the transaction.
var buf bytes.Buffer
err := test.in.Serialize(&buf)
if err != nil {
t.Errorf("Serialize #%d error %v", i, err)
continue
}
if !bytes.Equal(buf.Bytes(), test.buf) {
t.Errorf("Serialize #%d\n got: %s want: %s", i,
spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
continue
}
// Deserialize the transaction.
var tx btcwire.MsgTx
rbuf := bytes.NewReader(test.buf)
err = tx.Deserialize(rbuf)
if err != nil {
t.Errorf("Deserialize #%d error %v", i, err)
continue
}
if !reflect.DeepEqual(&tx, test.out) {
t.Errorf("Deserialize #%d\n got: %s want: %s", i,
spew.Sdump(&tx), spew.Sdump(test.out))
continue
}
}
}
开发者ID:kac-,项目名称:btcwire,代码行数:61,代码来源:msgtx_test.go
示例4: TestTxSerializeSize
// TestTxSerializeSize performs tests to ensure the serialize size for various
// transactions is accurate.
func TestTxSerializeSize(t *testing.T) {
// Empty tx message.
noTx := btcwire.NewMsgTx()
noTx.Version = 1
tests := []struct {
in *btcwire.MsgTx // Tx to encode
size int // Expected serialized size
}{
// No inputs or outpus.
{noTx, 10},
// Transcaction with an input and an output.
{multiTx, 134},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
serializedSize := test.in.SerializeSize()
if serializedSize != test.size {
t.Errorf("MsgTx.SerializeSize: #%d got: %d, want: %d", i,
serializedSize, test.size)
continue
}
}
}
开发者ID:kac-,项目名称:btcwire,代码行数:28,代码来源:msgtx_test.go
示例5: Build
func (bltnB *BulletinBuilder) Build() (*btcwire.MsgTx, error) {
utxo, err := selectUnspent(bltnB.SatNeeded(), bltnB.Params)
if err != nil {
return nil, err
}
msgtx := btcwire.NewMsgTx()
// Add data storing txouts.
txouts, err := bltnB.Bulletin.TxOuts(bltnB.BurnAmnt, bltnB.Params.NetParams)
if err != nil {
return nil, err
}
msgtx.TxOut = txouts
txin := btcwire.NewTxIn(utxo.OutPoint, []byte{})
msgtx.AddTxIn(txin)
// Deal with change
changeAmnt := utxo.TxOut.Value - bltnB.SatNeeded()
if changeAmnt > bltnB.Params.DustAmnt {
changeOut, err := makeChange(changeAmnt, bltnB.Params)
if err != nil {
return nil, err
}
msgtx.AddTxOut(changeOut)
}
// Sign the Bulletin
privkey := utxo.Wif.PrivKey
scriptSig, err := btcscript.SignatureScript(msgtx, 0, utxo.TxOut.PkScript, btcscript.SigHashAll, privkey, true)
if err != nil {
return nil, err
}
txin.SignatureScript = scriptSig
return msgtx, nil
}
开发者ID:bclermont,项目名称:btcbuilder,代码行数:35,代码来源:buildbulletin.go
示例6: Build
func (fanB *FanOutBuilder) Build() (*btcwire.MsgTx, error) {
totalSpent := fanB.SatNeeded()
// Compose a set of Txins with enough to fund this transactions needs
inParamSet, totalIn, err := composeUnspents(
totalSpent,
fanB.Params)
if err != nil {
return nil, err
}
msgtx := btcwire.NewMsgTx()
// funding inputs speced out with blank
for _, inpParam := range inParamSet {
txin := btcwire.NewTxIn(inpParam.OutPoint, []byte{})
msgtx.AddTxIn(txin)
}
for i := range fanB.Builders {
builder := fanB.Builders[i]
amnt := builder.SatNeeded()
for j := int64(0); j < fanB.Copies; j++ {
addr, err := newAddr(fanB.Params.Client)
if err != nil {
return nil, err
}
script, _ := btcscript.PayToAddrScript(addr)
txout := btcwire.NewTxOut(amnt, script)
msgtx.AddTxOut(txout)
}
}
changeAddr, err := newAddr(fanB.Params.Client)
if err != nil {
return nil, err
}
// change to solve unevenness
change, ok := changeOutput(totalIn-totalSpent, fanB.Params.DustAmnt, changeAddr)
if ok {
msgtx.AddTxOut(change)
}
// sign msgtx for each input
for i, inpParam := range inParamSet {
privkey := inpParam.Wif.PrivKey
subscript := inpParam.TxOut.PkScript
sigflag := btcscript.SigHashAll
scriptSig, err := btcscript.SignatureScript(msgtx, i, subscript,
sigflag, privkey, true)
if err != nil {
return nil, err
}
msgtx.TxIn[i].SignatureScript = scriptSig
}
fanB.Log(fmt.Sprintf("InVal: %d\n", sumInputs(inParamSet)))
fanB.Log(fmt.Sprintf("OutVal: %d\n", sumOutputs(msgtx)))
return msgtx, nil
}
开发者ID:bclermont,项目名称:btcbuilder,代码行数:59,代码来源:buildfanout.go
示例7: TestTxSha
func TestTxSha(t *testing.T) {
pver := btcwire.ProtocolVersion
// Hash of first transaction from block 113875.
hashStr := "f051e59b5e2503ac626d03aaeac8ab7be2d72ba4b7e97119c5852d70d52dcb86"
wantHash, err := btcwire.NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
return
}
// First transaction from block 113875.
msgTx := btcwire.NewMsgTx()
txIn := btcwire.TxIn{
PreviousOutpoint: btcwire.OutPoint{
Hash: btcwire.ShaHash{0x00},
Index: 0xffffffff,
},
SignatureScript: []byte{0x04, 0x31, 0xdc, 0x00, 0x1b, 0x01, 0x62},
Sequence: 0xffffffff,
}
txOut := btcwire.TxOut{
Value: 5000000000,
PkScript: []byte{
0x41, // OP_DATA_65
0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5,
0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42,
0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1,
0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24,
0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97,
0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78,
0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20,
0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63,
0xa6, // 65-byte signature
0xac, // OP_CHECKSIG
},
}
msgTx.AddTxIn(&txIn)
msgTx.AddTxOut(&txOut)
msgTx.LockTime = 0
// Ensure the hash produced is expected.
txHash, err := msgTx.TxSha(pver)
if err != nil {
t.Errorf("TxSha: %v", err)
}
if !txHash.IsEqual(wantHash) {
t.Errorf("TxSha: wrong hash - got %v, want %v",
spew.Sprint(txHash), spew.Sprint(wantHash))
}
}
开发者ID:h00gs,项目名称:btcwire,代码行数:51,代码来源:msgtx_test.go
示例8: NewMsgTxWithInputCoins
// NewMsgTxWithInputCoins takes the coins in the CoinSet and makes them
// the inputs to a new btcwire.MsgTx which is returned.
func NewMsgTxWithInputCoins(inputCoins Coins) *btcwire.MsgTx {
msgTx := btcwire.NewMsgTx()
coins := inputCoins.Coins()
msgTx.TxIn = make([]*btcwire.TxIn, len(coins))
for i, coin := range coins {
msgTx.TxIn[i] = &btcwire.TxIn{
PreviousOutPoint: btcwire.OutPoint{
Hash: *coin.Hash(),
Index: coin.Index(),
},
SignatureScript: nil,
Sequence: btcwire.MaxTxInSequenceNum,
}
}
return msgTx
}
开发者ID:stoiclabs,项目名称:blockchainr,代码行数:18,代码来源:coins.go
示例9: handleSendRawTransaction
// handleSendRawTransaction implements the sendrawtransaction command.
func handleSendRawTransaction(s *rpcServer, cmd btcjson.Cmd) (interface{}, error) {
c := cmd.(*btcjson.SendRawTransactionCmd)
// Deserialize and send off to tx relay
serializedTx, err := hex.DecodeString(c.HexTx)
if err != nil {
return nil, btcjson.ErrDecodeHexString
}
msgtx := btcwire.NewMsgTx()
err = msgtx.Deserialize(bytes.NewBuffer(serializedTx))
if err != nil {
err := btcjson.Error{
Code: btcjson.ErrDeserialization.Code,
Message: "TX decode failed",
}
return nil, err
}
tx := btcutil.NewTx(msgtx)
err = s.server.txMemPool.ProcessTransaction(tx, false)
if err != nil {
// When the error is a rule error, it means the transaction was
// simply rejected as opposed to something actually going wrong,
// so log it as such. Otherwise, something really did go wrong,
// so log it as an actual error. In both cases, a JSON-RPC
// error is returned to the client with the deserialization
// error code (to match bitcoind behavior).
if _, ok := err.(TxRuleError); ok {
rpcsLog.Debugf("Rejected transaction %v: %v", tx.Sha(),
err)
} else {
rpcsLog.Errorf("Failed to process transaction %v: %v",
tx.Sha(), err)
}
err = btcjson.Error{
Code: btcjson.ErrDeserialization.Code,
Message: fmt.Sprintf("TX rejected: %v", err),
}
return nil, err
}
// We keep track of all the sendrawtransaction request txs so that we
// can rebroadcast them if they don't make their way into a block.
iv := btcwire.NewInvVect(btcwire.InvTypeTx, tx.Sha())
s.server.AddRebroadcastInventory(iv)
return tx.Sha().String(), nil
}
开发者ID:shea256,项目名称:btcd,代码行数:48,代码来源:rpcserver.go
示例10: Build
func (shsB *SigHashSingleBuilder) Build() (*btcwire.MsgTx, error) {
// RPC to setup previous TX
utxo, err := selectUnspent(shsB.SatNeeded()+shsB.Params.DustAmnt, shsB.Params)
if err != nil {
return nil, err
}
oldTxOut := utxo.TxOut
outpoint := utxo.OutPoint
wifkey := utxo.Wif
// Transaction building
txin := btcwire.NewTxIn(outpoint, []byte{})
// notice amount in
total := oldTxOut.Value
changeval := total - (shsB.SatNeeded())
change, ok := changeOutput(changeval, shsB.Params.DustAmnt,
wifToAddr(wifkey, shsB.Params.NetParams))
if !ok {
return nil, errors.New("Not enough for change.")
}
// Blank permutable txout for users to play with
blankval := shsB.Params.InTarget - shsB.Params.Fee
blank := btcwire.NewTxOut(blankval, change.PkScript) //[]byte{})
msgtx := btcwire.NewMsgTx()
msgtx.AddTxIn(txin)
msgtx.AddTxOut(change)
msgtx.AddTxOut(blank)
subscript := oldTxOut.PkScript
privkey := wifkey.PrivKey
scriptSig, err := btcscript.SignatureScript(msgtx, 0, subscript, btcscript.SigHashSingle, privkey, true)
if err != nil {
return nil, err
}
msgtx.TxIn[0].SignatureScript = scriptSig
// This demonstrates that we can sign and then permute a txout
//msgtx.TxOut[1].PkScript = oldTxOut.PkScript
blank.Value = blankval + 1
return msgtx, nil
}
开发者ID:bclermont,项目名称:btcbuilder,代码行数:46,代码来源:buildsighashsingle.go
示例11: handleSendRawTransaction
// handleSendRawTransaction implements the sendrawtransaction command.
func handleSendRawTransaction(s *rpcServer, cmd btcjson.Cmd, walletNotification chan []byte) (interface{}, error) {
c := cmd.(*btcjson.SendRawTransactionCmd)
// Deserialize and send off to tx relay
serializedTx, err := hex.DecodeString(c.HexTx)
if err != nil {
return nil, btcjson.ErrDecodeHexString
}
msgtx := btcwire.NewMsgTx()
err = msgtx.Deserialize(bytes.NewBuffer(serializedTx))
if err != nil {
err := btcjson.Error{
Code: btcjson.ErrDeserialization.Code,
Message: "Unable to deserialize raw tx",
}
return nil, err
}
tx := btcutil.NewTx(msgtx)
err = s.server.txMemPool.ProcessTransaction(tx)
if err != nil {
// When the error is a rule error, it means the transaction was
// simply rejected as opposed to something actually going wrong,
// so log it as such. Otherwise, something really did go wrong,
// so log it as an actual error.
if _, ok := err.(TxRuleError); ok {
log.Debugf("RPCS: Rejected transaction %v: %v", tx.Sha(),
err)
} else {
log.Errorf("RPCS: Failed to process transaction %v: %v",
tx.Sha(), err)
}
err = btcjson.Error{
Code: btcjson.ErrDeserialization.Code,
Message: "Failed to process transaction",
}
return nil, err
}
// If called from websocket code, add a mined tx hashes
// request.
if walletNotification != nil {
s.ws.requests.AddMinedTxRequest(walletNotification, tx.Sha())
}
return tx.Sha().String(), nil
}
开发者ID:Belxjander,项目名称:btcd,代码行数:47,代码来源:rpcserver.go
示例12: TestCheckSerializedHeight
// TestCheckSerializedHeight tests the checkSerializedHeight function with
// various serialized heights and also does negative tests to ensure errors
// and handled properly.
func TestCheckSerializedHeight(t *testing.T) {
// Create an empty coinbase template to be used in the tests below.
coinbaseOutpoint := btcwire.NewOutPoint(&btcwire.ShaHash{}, math.MaxUint32)
coinbaseTx := btcwire.NewMsgTx()
coinbaseTx.Version = 2
coinbaseTx.AddTxIn(btcwire.NewTxIn(coinbaseOutpoint, nil))
//
tests := []struct {
sigScript []byte // Serialized data
wantHeight int64 // Expected height
err error // Expected error type
}{
// No serialized height length.
{[]byte{}, 0, btcchain.RuleError("")},
// Serialized height length with no height bytes.
{[]byte{0x02}, 0, btcchain.RuleError("")},
// Serialized height length with too few height bytes.
{[]byte{0x02, 0x4a}, 0, btcchain.RuleError("")},
// Serialized height that needs 2 bytes to encode.
{[]byte{0x02, 0x4a, 0x52}, 21066, nil},
// Serialized height that needs 2 bytes to encode, but backwards
// endianness.
{[]byte{0x02, 0x4a, 0x52}, 19026, btcchain.RuleError("")},
// Serialized height that needs 3 bytes to encode.
{[]byte{0x03, 0x40, 0x0d, 0x03}, 200000, nil},
// Serialized height that needs 3 bytes to encode, but backwards
// endianness.
{[]byte{0x03, 0x40, 0x0d, 0x03}, 1074594560, btcchain.RuleError("")},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
msgTx := coinbaseTx.Copy()
msgTx.TxIn[0].SignatureScript = test.sigScript
tx := btcutil.NewTx(msgTx)
err := btcchain.TstCheckSerializedHeight(tx, test.wantHeight)
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
t.Errorf("checkSerializedHeight #%d wrong error type "+
"got: %v <%T>, want: %T", i, err, err, test.err)
continue
}
}
}
开发者ID:hsk81,项目名称:btcchain,代码行数:48,代码来源:validate_test.go
示例13: Build
func (builder *ToAddrBuilder) Build() (*btcwire.MsgTx, error) {
utxo, err := selectUnspent(builder.SatNeeded(), builder.Params)
if err != nil {
return nil, err
}
txin := btcwire.NewTxIn(utxo.OutPoint, []byte{})
msgtx := btcwire.NewMsgTx()
msgtx.AddTxIn(txin)
// add send to addr
valout := builder.Params.InTarget - builder.Params.Fee
outscript, _ := btcscript.PayToAddrScript(builder.Addr)
txout := btcwire.NewTxOut(valout, outscript)
msgtx.AddTxOut(txout)
// add send to change addr
total := utxo.TxOut.Value
changeval := total - builder.SatNeeded()
if changeval > builder.Params.DustAmnt {
// Change needed
changeAddr, err := builder.Params.Client.GetNewAddress()
if err != nil {
return nil, err
}
change, ok := changeOutput(changeval, builder.Params.DustAmnt, changeAddr)
if ok {
msgtx.AddTxOut(change)
}
}
subscript := utxo.TxOut.PkScript
privkey := utxo.Wif.PrivKey
scriptSig, err := btcscript.SignatureScript(msgtx, 0, subscript, btcscript.SigHashAll, privkey, true)
if err != nil {
return nil, err
}
txin.SignatureScript = scriptSig
return msgtx, nil
}
开发者ID:bclermont,项目名称:btcbuilder,代码行数:43,代码来源:buildsendtoaddr.go
示例14: Build
func (ndB *NullDataBuilder) Build() (*btcwire.MsgTx, error) {
utxo, err := specificUnspent(ndB.SatNeeded(), ndB.Params)
if err != nil {
return nil, err
}
msgtx := btcwire.NewMsgTx()
if len(ndB.Data) > 40 {
return nil, errors.New("Data is too long to make this a standard tx.")
}
// OP Return output
retbuilder := btcscript.NewScriptBuilder().AddOp(btcscript.OP_RETURN).AddData(ndB.Data)
op_return := btcwire.NewTxOut(0, retbuilder.Script())
msgtx.AddTxOut(op_return)
if ndB.Change {
// change ouput
addr, _ := newAddr(ndB.Params.Client)
change, ok := changeOutput(ndB.SatNeeded()-ndB.Params.Fee, ndB.Params.DustAmnt, addr)
if !ok {
return nil, errors.New("Not enough for change")
}
msgtx.AddTxOut(change)
}
// funding input
txin := btcwire.NewTxIn(utxo.OutPoint, []byte{})
msgtx.AddTxIn(txin)
// sign msgtx
privkey := utxo.Wif.PrivKey
scriptSig, err := btcscript.SignatureScript(msgtx, 0, utxo.TxOut.PkScript, btcscript.SigHashAll, privkey, true)
if err != nil {
return nil, err
}
txin.SignatureScript = scriptSig
return msgtx, nil
}
开发者ID:bclermont,项目名称:btcbuilder,代码行数:42,代码来源:buildnulldata.go
示例15: Build
// A transaction that contains only dust ouputs and obeys the TxBuilder interface
func (builder *DustBuilder) Build() (*btcwire.MsgTx, error) {
var inparams *TxInParams
var err error
inparams, err = specificUnspent(
builder.SatNeeded(),
builder.Params)
if err != nil {
return nil, err
}
oldTxOut := inparams.TxOut
outpoint := inparams.OutPoint
wifkey := inparams.Wif
msgtx := btcwire.NewMsgTx()
txin := btcwire.NewTxIn(outpoint, []byte{})
msgtx.AddTxIn(txin)
for i := int64(0); i < builder.NumOuts; i++ {
dumb := bytes.Repeat([]byte{66}, 20)
addr := dataAddr(dumb, builder.Params.NetParams)
addrScript, err := btcscript.PayToAddrScript(addr)
if err != nil {
return nil, err
}
txOut := btcwire.NewTxOut(builder.Params.DustAmnt, addrScript)
msgtx.AddTxOut(txOut)
}
// sign as usual
privkey := wifkey.PrivKey
sig, err := btcscript.SignatureScript(msgtx, 0, oldTxOut.PkScript, btcscript.SigHashAll, privkey, true)
if err != nil {
return nil, err
}
txin.SignatureScript = sig
return msgtx, nil
}
开发者ID:bclermont,项目名称:btcbuilder,代码行数:42,代码来源:builddust.go
示例16: handleSendRawTransaction
// handleSendRawTransaction implements the sendrawtransaction command.
func handleSendRawTransaction(s *rpcServer, cmd btcjson.Cmd) (interface{}, error) {
c := cmd.(*btcjson.SendRawTransactionCmd)
// Deserialize and send off to tx relay
serializedTx, err := hex.DecodeString(c.HexTx)
if err != nil {
return nil, btcjson.ErrDecodeHexString
}
msgtx := btcwire.NewMsgTx()
err = msgtx.Deserialize(bytes.NewBuffer(serializedTx))
if err != nil {
err := btcjson.Error{
Code: btcjson.ErrDeserialization.Code,
Message: "TX decode failed",
}
return nil, err
}
tx := btcutil.NewTx(msgtx)
err = s.server.txMemPool.ProcessTransaction(tx)
if err != nil {
// When the error is a rule error, it means the transaction was
// simply rejected as opposed to something actually going wrong,
// so log it as such. Otherwise, something really did go wrong,
// so log it as an actual error.
if _, ok := err.(TxRuleError); ok {
rpcsLog.Debugf("Rejected transaction %v: %v", tx.Sha(),
err)
} else {
rpcsLog.Errorf("Failed to process transaction %v: %v",
tx.Sha(), err)
err = btcjson.Error{
Code: btcjson.ErrDeserialization.Code,
Message: "TX rejected",
}
return nil, err
}
}
return tx.Sha().String(), nil
}
开发者ID:hsk81,项目名称:btcd,代码行数:41,代码来源:rpcserver.go
示例17: Build
func (pkhB *PubKeyHashBuilder) Build() (*btcwire.MsgTx, error) {
inparams, err := specificUnspent(pkhB.SatNeeded(), pkhB.Params)
if err != nil {
return nil, err
}
msgtx := btcwire.NewMsgTx()
txin := btcwire.NewTxIn(inparams.OutPoint, []byte{})
msgtx.AddTxIn(txin)
for i := int64(0); i < pkhB.NumOuts; i++ {
addr, err := newAddr(pkhB.Params.Client)
if err != nil {
return nil, err
}
addrScript, err := btcscript.PayToAddrScript(addr)
amntSend := pkhB.eachOutVal()
if amntSend < pkhB.Params.DustAmnt {
return nil, errors.New("Output would be under the dust limit")
}
txout := btcwire.NewTxOut(pkhB.eachOutVal(), addrScript)
msgtx.AddTxOut(txout)
}
privkey := inparams.Wif.PrivKey
sig, err := btcscript.SignatureScript(msgtx,
0,
inparams.TxOut.PkScript,
btcscript.SigHashAll,
privkey,
true)
if err != nil {
return nil, err
}
txin.SignatureScript = sig
return msgtx, nil
}
开发者ID:bclermont,项目名称:btcbuilder,代码行数:39,代码来源:buildpubkeyhash.go
示例18: createCoinbaseTx
// createCoinbaseTx returns a coinbase transaction paying an appropriate subsidy
// based on the passed block height to the passed public key. It also accepts
// an extra nonce value for the signature script. This extra nonce helps ensure
// the transaction is not a duplicate transaction (paying the same value to the
// same public key address would otherwise be an identical transaction for
// block version 1).
func createCoinbaseTx(coinbaseScript []byte, nextBlockHeight int64, addr btcutil.Address) (*btcutil.Tx, error) {
// Create a script to pay to the specific address.
pkScript, err := btcscript.PayToAddrScript(addr)
if err != nil {
return nil, err
}
tx := btcwire.NewMsgTx()
tx.AddTxIn(&btcwire.TxIn{
// Coinbase transactions have no inputs, so previous outpoint is
// zero hash and max index.
PreviousOutpoint: *btcwire.NewOutPoint(&btcwire.ShaHash{},
btcwire.MaxPrevOutIndex),
SignatureScript: coinbaseScript,
Sequence: btcwire.MaxTxInSequenceNum,
})
tx.AddTxOut(&btcwire.TxOut{
Value: btcchain.CalcBlockSubsidy(nextBlockHeight,
activeNetParams.Params),
PkScript: pkScript,
})
return btcutil.NewTx(tx), nil
}
开发者ID:nixoid,项目名称:btcd,代码行数:29,代码来源:mining.go
示例19: getAuthor
// Returns the "Author" who signed the first txin of the transaction
func getAuthor(tx *btcwire.MsgTx, net *btcnet.Params) (string, error) {
sigScript := tx.TxIn[0].SignatureScript
dummyTx := btcwire.NewMsgTx()
// Setup a script executor to parse the raw bytes of the signature script.
script, err := btcscript.NewScript(sigScript, make([]byte, 0), 0, dummyTx, 0)
if err != nil {
return "", err
}
// Step twice due to <sig> <pubkey> format of pay 2pubkeyhash
script.Step()
script.Step()
// Pull off the <pubkey>
pkBytes := script.GetStack()[1]
addrPubKey, err := btcutil.NewAddressPubKey(pkBytes, net)
if err != nil {
return "", err
}
return addrPubKey.EncodeAddress(), nil
}
开发者ID:bclermont,项目名称:protocol,代码行数:24,代码来源:bulletin.go
示例20: Build
// TODO This will add multisig Txouts to the unspent set be AWARE
func (msB *MultiSigBuilder) Build() (*btcwire.MsgTx, error) {
utxo, err := specificUnspent(msB.SatNeeded(), msB.Params)
if err != nil {
return nil, err
}
msgtx := btcwire.NewMsgTx()
txin := btcwire.NewTxIn(utxo.OutPoint, []byte{})
msgtx.AddTxIn(txin)
for _, pubkeys := range msB.PubKeyList {
// M pubkey pubkey pubkey N OP_CHECKMULTISIG
scriptBuilder := btcscript.NewScriptBuilder().AddInt64(msB.M)
for _, pk := range pubkeys {
scriptBuilder = scriptBuilder.AddData(pk)
}
scriptBuilder = scriptBuilder.AddInt64(msB.N).AddOp(btcscript.OP_CHECKMULTISIG)
PkScript := scriptBuilder.Script()
txout := btcwire.NewTxOut(msB.eachOutVal(), PkScript)
msgtx.AddTxOut(txout)
}
// Sign this puppy
privkey := utxo.Wif.PrivKey
subscript := utxo.TxOut.PkScript
sigflag := btcscript.SigHashAll
scriptSig, err := btcscript.SignatureScript(msgtx, 0, subscript,
sigflag, privkey, true)
if err != nil {
return nil, err
}
msgtx.TxIn[0].SignatureScript = scriptSig
return msgtx, nil
}
开发者ID:bclermont,项目名称:btcbuilder,代码行数:38,代码来源:buildmultisig.go
注:本文中的github.com/conformal/btcwire.NewMsgTx函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论