本文整理汇总了Golang中github.com/piotrnar/gocoin/lib/btc.Tx.SignWitness方法的典型用法代码示例。如果您正苦于以下问题:Golang Tx.SignWitness方法的具体用法?Golang Tx.SignWitness怎么用?Golang Tx.SignWitness使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/piotrnar/gocoin/lib/btc.Tx
的用法示例。
在下文中一共展示了Tx.SignWitness方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: sign_tx
// prepare a signed transaction
func sign_tx(tx *btc.Tx) (all_signed bool) {
var multisig_done bool
all_signed = true
// go through each input
for in := range tx.TxIn {
if ms, _ := btc.NewMultiSigFromScript(tx.TxIn[in].ScriptSig); ms != nil {
hash := tx.SignatureHash(ms.P2SH(), in, btc.SIGHASH_ALL)
for ki := range ms.PublicKeys {
k := public_to_key(ms.PublicKeys[ki])
if k != nil {
r, s, e := btc.EcdsaSign(k.Key, hash)
if e != nil {
println("ERROR in sign_tx:", e.Error())
all_signed = false
} else {
btcsig := &btc.Signature{HashType: 0x01}
btcsig.R.Set(r)
btcsig.S.Set(s)
ms.Signatures = append(ms.Signatures, btcsig)
tx.TxIn[in].ScriptSig = ms.Bytes()
multisig_done = true
}
}
}
} else {
uo := getUO(&tx.TxIn[in].Input)
if uo == nil {
println("ERROR: Unkown input:", tx.TxIn[in].Input.String(), "- missing balance folder?")
all_signed = false
continue
}
adr := addr_from_pkscr(uo.Pk_script)
if adr == nil {
fmt.Println("WARNING: Don't know how to sign input number", in)
fmt.Println(" Pk_script:", hex.EncodeToString(uo.Pk_script))
all_signed = false
continue
}
k_idx := hash_to_key_idx(adr.Hash160[:])
if k_idx < 0 {
fmt.Println("WARNING: You do not have key for", adr.String(), "at input", in)
all_signed = false
continue
}
var er error
k := keys[k_idx]
if adr.String() == segwit[k_idx].String() {
tx.TxIn[in].ScriptSig = append([]byte{22, 0, 20}, k.BtcAddr.Hash160[:]...)
er = tx.SignWitness(in, k.BtcAddr.OutScript(), uo.Value, btc.SIGHASH_ALL, k.BtcAddr.Pubkey, k.Key)
} else {
er = tx.Sign(in, uo.Pk_script, btc.SIGHASH_ALL, k.BtcAddr.Pubkey, k.Key)
}
if er != nil {
fmt.Println("ERROR: Sign failed for input number", in, er.Error())
all_signed = false
}
}
}
// reorder signatures if we signed any multisig inputs
if multisig_done && !multisig_reorder(tx) {
all_signed = false
}
if !all_signed {
fmt.Println("WARNING: Not all the inputs have been signed")
}
return
}