本文整理汇总了Golang中github.com/piotrnar/gocoin/lib/btc.Tx.SignatureHash方法的典型用法代码示例。如果您正苦于以下问题:Golang Tx.SignatureHash方法的具体用法?Golang Tx.SignatureHash怎么用?Golang Tx.SignatureHash使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/piotrnar/gocoin/lib/btc.Tx
的用法示例。
在下文中一共展示了Tx.SignatureHash方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: multisig_reorder
// reorder signatures to meet order of the keys
// remove signatuers made by the same keys
// remove exessive signatures (keeps transaction size down)
func multisig_reorder(tx *btc.Tx) (all_signed bool) {
all_signed = true
for i := range tx.TxIn {
ms, _ := btc.NewMultiSigFromScript(tx.TxIn[i].ScriptSig)
if ms == nil {
continue
}
hash := tx.SignatureHash(ms.P2SH(), i, btc.SIGHASH_ALL)
var sigs []*btc.Signature
for ki := range ms.PublicKeys {
var sig *btc.Signature
for si := range ms.Signatures {
if btc.EcdsaVerify(ms.PublicKeys[ki], ms.Signatures[si].Bytes(), hash) {
//fmt.Println("Key number", ki, "has signature number", si)
sig = ms.Signatures[si]
break
}
}
if sig != nil {
sigs = append(sigs, sig)
} else if *verbose {
fmt.Println("WARNING: Key number", ki, "has no matching signature")
}
if !*allowextramsigns && uint(len(sigs)) >= ms.SigsNeeded {
break
}
}
if *verbose {
if len(ms.Signatures) > len(sigs) {
fmt.Println("WARNING: Some signatures are obsolete and will be removed", len(ms.Signatures), "=>", len(sigs))
} else if len(ms.Signatures) < len(sigs) {
fmt.Println("It appears that same key is re-used.", len(sigs)-len(ms.Signatures), "more signatures were added")
}
}
ms.Signatures = sigs
tx.TxIn[i].ScriptSig = ms.Bytes()
if len(sigs) < int(ms.SigsNeeded) {
all_signed = false
}
}
return
}
示例2: dump_hashes_to_sign
// dump hashes to be signed
func dump_hashes_to_sign(tx *btc.Tx) {
for in := range tx.TxIn {
uo := UO(unspentOuts[in])
if uo == nil {
println("Unknown content of unspent input number", in)
os.Exit(1)
}
var pubad *btc.BtcAddr
if litecoin {
pubad = ltc.NewAddrFromPkScript(uo.Pk_script, testnet)
} else {
pubad = btc.NewAddrFromPkScript(uo.Pk_script, testnet)
}
if pubad != nil {
hash := tx.SignatureHash(uo.Pk_script, in, btc.SIGHASH_ALL)
fmt.Printf("Input #%d:\n\tHash : %s\n\tAddr : %s\n", in, hex.EncodeToString(hash), pubad.String())
} else {
println("Cannot decode pkscript of unspent input number", in)
os.Exit(1)
}
}
}
示例3: 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 := hash_to_key(adr.Hash160)
if k == nil {
fmt.Println("WARNING: You do not have key for", adr.String(), "at input", in)
all_signed = false
continue
}
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
}
示例4: evalScript
//.........这里部分代码省略.........
if stack.size() < 1 {
if DBG_ERR {
fmt.Println("Stack too short for opcode", opcode)
}
return false
}
h := btc.Sha2Sum(stack.pop())
stack.push(h[:])
case opcode == 0xab: // OP_CODESEPARATOR
sta = idx
case opcode == 0xac || opcode == 0xad: // OP_CHECKSIG || OP_CHECKSIGVERIFY
if stack.size() < 2 {
if DBG_ERR {
fmt.Println("Stack too short for opcode", opcode)
}
return false
}
var ok bool
pk := stack.pop()
si := stack.pop()
// BIP-0066
if !CheckSignatureEncoding(si, ver_flags) {
if DBG_ERR {
fmt.Println("Invalid Signature Encoding A")
}
return false
}
if len(si) > 0 {
sh := tx.SignatureHash(delSig(p[sta:], si), inp, int32(si[len(si)-1]))
ok = btc.EcdsaVerify(pk, si, sh)
}
if !ok && DBG_ERR {
if DBG_ERR {
fmt.Println("EcdsaVerify fail 1")
}
}
if DBG_SCR {
fmt.Println("ver:", ok)
}
if opcode == 0xad {
if !ok { // OP_CHECKSIGVERIFY
return false
}
} else { // OP_CHECKSIG
stack.pushBool(ok)
}
case opcode == 0xae || opcode == 0xaf: //OP_CHECKMULTISIG || OP_CHECKMULTISIGVERIFY
//fmt.Println("OP_CHECKMULTISIG ...")
//stack.print()
if stack.size() < 1 {
if DBG_ERR {
fmt.Println("OP_CHECKMULTISIG: Stack too short A")
}
return false
}
i := 1
keyscnt := stack.topInt(-i, checkMinVals)
if keyscnt < 0 || keyscnt > 20 {
fmt.Println("OP_CHECKMULTISIG: Wrong number of keys")