本文整理汇总了Golang中github.com/piotrnar/gocoin/lib/btc.Tx类的典型用法代码示例。如果您正苦于以下问题:Golang Tx类的具体用法?Golang Tx怎么用?Golang Tx使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Tx类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: apply_to_balance
// apply the chnages to the balance folder
func apply_to_balance(tx *btc.Tx) {
f, _ := os.Create("balance/unspent.txt")
if f != nil {
// append new outputs at the end of unspentOuts
ioutil.WriteFile("balance/"+tx.Hash.String()+".tx", tx.Serialize(), 0600)
fmt.Println("Adding", len(tx.TxOut), "new output(s) to the balance/ folder...")
for out := range tx.TxOut {
if k := pkscr_to_key(tx.TxOut[out].Pk_script); k != nil {
uns := new(unspRec)
uns.key = k
uns.TxPrevOut.Hash = tx.Hash.Hash
uns.TxPrevOut.Vout = uint32(out)
uns.label = fmt.Sprint("# ", btc.UintToBtc(tx.TxOut[out].Value), " BTC @ ", k.BtcAddr.String())
//stealth bool TODO: maybe we can fix it...
unspentOuts = append(unspentOuts, uns)
}
}
for j := range unspentOuts {
if !unspentOuts[j].spent {
fmt.Fprintln(f, unspentOuts[j].String())
}
}
f.Close()
} else {
println("ERROR: Cannot create balance/unspent.txt")
}
}
示例2: write_tx_file
func write_tx_file(tx *btc.Tx) {
signedrawtx := tx.Serialize()
tx.Hash = btc.NewSha2Hash(signedrawtx)
hs := tx.Hash.String()
fmt.Println("TxID", hs)
f, _ := os.Create(hs[:8] + ".txt")
if f != nil {
f.Write([]byte(hex.EncodeToString(signedrawtx)))
f.Close()
fmt.Println("Transaction data stored in", hs[:8]+".txt")
}
}
示例3: consensus_verify_script
func consensus_verify_script(pkScr []byte, i int, tx *btc.Tx, ver_flags uint32) bool {
txTo := tx.Serialize()
var pkscr_ptr, pkscr_len uintptr // default to 0/null
if pkScr != nil {
pkscr_ptr = uintptr(unsafe.Pointer(&pkScr[0]))
pkscr_len = uintptr(len(pkScr))
}
r1, _, _ := syscall.Syscall9(bitcoinconsensus_verify_script.Addr(), 7,
pkscr_ptr, pkscr_len,
uintptr(unsafe.Pointer(&txTo[0])), uintptr(len(txTo)),
uintptr(i), uintptr(ver_flags), 0, 0, 0)
return r1 == 1
}
示例4: 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
}
示例5: TxMiner
// return miner ID of the given coinbase transaction
func TxMiner(cbtx *btc.Tx) (string, int) {
txdat := cbtx.Serialize()
for i, m := range MinerIds {
if bytes.Equal(m.Tag, []byte("_p2pool_")) { // P2Pool
if len(cbtx.TxOut) > 10 &&
bytes.Equal(cbtx.TxOut[len(cbtx.TxOut)-1].Pk_script[:2], []byte{0x6A, 0x28}) {
return m.Name, i
}
} else if bytes.Contains(txdat, m.Tag) {
return m.Name, i
}
}
adr := btc.NewAddrFromPkScript(cbtx.TxOut[0].Pk_script, Testnet)
if adr != nil {
return adr.String(), -1
}
return "", -1
}
示例6: mk_spend_tx
func mk_spend_tx(input_tx *btc.Tx, sig_scr []byte, witness [][]byte) (output_tx *btc.Tx) {
output_tx = new(btc.Tx)
output_tx.Version = 1
output_tx.TxIn = []*btc.TxIn{&btc.TxIn{Input: btc.TxPrevOut{Hash: btc.Sha2Sum(input_tx.Serialize()), Vout: 0},
ScriptSig: sig_scr, Sequence: 0xffffffff}}
output_tx.TxOut = []*btc.TxOut{&btc.TxOut{Value: input_tx.TxOut[0].Value}}
// Lock_time = 0
if len(witness) > 0 {
output_tx.SegWit = make([][][]byte, 1)
output_tx.SegWit[0] = witness
if DBG_SCR {
println("tx has", len(witness), "ws")
for xx := range witness {
println("", xx, hex.EncodeToString(witness[xx]))
}
}
}
output_tx.SetHash(output_tx.Serialize())
return
}
示例7: check_consensus
func check_consensus(pkScr []byte, amount uint64, i int, tx *btc.Tx, ver_flags uint32, result bool) {
var tmp []byte
if len(pkScr) != 0 {
tmp = make([]byte, len(pkScr))
copy(tmp, pkScr)
}
tx_raw := tx.Raw
if tx_raw == nil {
tx_raw = tx.Serialize()
}
go func(pkScr []byte, txTo []byte, i int, ver_flags uint32, result bool) {
var pkscr_ptr, pkscr_len uintptr // default to 0/null
if pkScr != nil {
pkscr_ptr = uintptr(unsafe.Pointer(&pkScr[0]))
pkscr_len = uintptr(len(pkScr))
}
r1, _, _ := syscall.Syscall9(bitcoinconsensus_verify_script_with_amount.Addr(), 8,
pkscr_ptr, pkscr_len, uintptr(amount),
uintptr(unsafe.Pointer(&txTo[0])), uintptr(len(txTo)),
uintptr(i), uintptr(ver_flags), 0, 0)
res := r1 == 1
atomic.AddUint64(&ConsensusChecks, 1)
if !result {
atomic.AddUint64(&ConsensusExpErr, 1)
}
if res != result {
atomic.AddUint64(&ConsensusErrors, 1)
common.CountSafe("TxConsensusERR")
mut.Lock()
println("Compare to consensus failed!")
println("Gocoin:", result, " ConsLIB:", res)
println("pkScr", hex.EncodeToString(pkScr))
println("txTo", hex.EncodeToString(txTo))
println("amount:", amount, " input_idx:", i, " ver_flags:", ver_flags)
println()
mut.Unlock()
}
}(tmp, tx_raw, i, ver_flags, result)
}
示例8: 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)
}
}
}
示例9: apply_to_balance
// apply the chnages to the balance folder
func apply_to_balance(tx *btc.Tx) {
fmt.Println("Applying the transaction to the balance/ folder...")
f, _ := os.Create("balance/unspent.txt")
if f != nil {
for j := 0; j < len(unspentOuts); j++ {
if j > len(tx.TxIn) {
fmt.Fprintln(f, unspentOuts[j], unspentOutsLabel[j])
}
}
if *verbose {
fmt.Println(len(tx.TxIn), "spent output(s) removed from 'balance/unspent.txt'")
}
var addback int
for out := range tx.TxOut {
for j := range publ_addrs {
if publ_addrs[j].Owns(tx.TxOut[out].Pk_script) {
fmt.Fprintf(f, "%s-%03d # %.8f / %s\n", tx.Hash.String(), out,
float64(tx.TxOut[out].Value)/1e8, publ_addrs[j].String())
addback++
}
}
}
f.Close()
if addback > 0 {
f, _ = os.Create("balance/" + tx.Hash.String() + ".tx")
if f != nil {
f.Write(tx.Serialize())
f.Close()
}
if *verbose {
fmt.Println(addback, "new output(s) appended to 'balance/unspent.txt'")
}
}
}
}
示例10: skip_broken_tests
// Some tests from the satoshi's json files are not applicable
// ... for our architectre so lets just fake them.
func skip_broken_tests(tx *btc.Tx) bool {
// No inputs
if len(tx.TxIn) == 0 {
return true
}
// Negative output
for i := range tx.TxOut {
if tx.TxOut[i].Value > btc.MAX_MONEY {
return true
}
}
// Duplicate inputs
if len(tx.TxIn) > 1 {
for i := 0; i < len(tx.TxIn)-1; i++ {
for j := i + 1; j < len(tx.TxIn); j++ {
if tx.TxIn[i].Input == tx.TxIn[j].Input {
return true
}
}
}
}
// Coinbase of w wrong size
if tx.IsCoinBase() {
if len(tx.TxIn[0].ScriptSig) < 2 {
return true
}
if len(tx.TxIn[0].ScriptSig) > 100 {
return true
}
}
return false
}
示例11: sign_tx
// prepare a signed transaction
func sign_tx(tx *btc.Tx) (all_signed bool) {
all_signed = true
for in := range tx.TxIn {
uo := UO(unspentOuts[in])
var found bool
for j := range publ_addrs {
if publ_addrs[j].Owns(uo.Pk_script) {
er := tx.Sign(in, uo.Pk_script, btc.SIGHASH_ALL, publ_addrs[j].Pubkey, priv_keys[j][:])
if er == nil {
found = true
} else {
fmt.Println("Error signing input", in, "of", len(tx.TxIn))
fmt.Println("...", er.Error())
}
break
}
}
if !found {
fmt.Println("WARNING: You do not have key for", hex.EncodeToString(uo.Pk_script))
all_signed = false
}
}
return
}
示例12: mk_out_tx
func mk_out_tx(sig_scr, pk_scr []byte) (output_tx *btc.Tx) {
// We build input_tx only to calculate it's hash for output_tx
input_tx := new(btc.Tx)
input_tx.Version = 1
input_tx.TxIn = []*btc.TxIn{&btc.TxIn{Input: btc.TxPrevOut{Vout: 0xffffffff},
ScriptSig: []byte{0, 0}, Sequence: 0xffffffff}}
input_tx.TxOut = []*btc.TxOut{&btc.TxOut{Pk_script: pk_scr}}
// Lock_time = 0
output_tx = new(btc.Tx)
output_tx.Version = 1
output_tx.TxIn = []*btc.TxIn{&btc.TxIn{Input: btc.TxPrevOut{Hash: btc.Sha2Sum(input_tx.Serialize()), Vout: 0},
ScriptSig: sig_scr, Sequence: 0xffffffff}}
output_tx.TxOut = []*btc.TxOut{&btc.TxOut{}}
// Lock_time = 0
return
}
示例13: write_tx_file
func write_tx_file(tx *btc.Tx) {
var signedrawtx []byte
if tx.SegWit != nil {
signedrawtx = tx.SerializeNew()
} else {
signedrawtx = tx.Serialize()
}
tx.SetHash(signedrawtx)
hs := tx.Hash.String()
fmt.Println("TxID", hs)
f, _ := os.Create(hs[:8] + ".txt")
if f != nil {
f.Write([]byte(hex.EncodeToString(signedrawtx)))
f.Close()
fmt.Println("Transaction data stored in", hs[:8]+".txt")
}
}
示例14: 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
}
示例15: make_signed_tx
// prepare a signed transaction
func make_signed_tx() {
// Make an empty transaction
tx := new(btc.Tx)
tx.Version = 1
tx.Lock_time = 0
// Select as many inputs as we need to pay the full amount (with the fee)
var btcsofar uint64
for inpcnt := 0; inpcnt < len(unspentOuts); inpcnt++ {
uo := UO(unspentOuts[inpcnt])
// add the input to our transaction:
tin := new(btc.TxIn)
tin.Input = *unspentOuts[inpcnt]
tin.Sequence = 0xffffffff
tx.TxIn = append(tx.TxIn, tin)
btcsofar += uo.Value
if !*useallinputs && (btcsofar >= spendBtc+feeBtc) {
break
}
}
changeBtc = btcsofar - (spendBtc + feeBtc)
if *verbose {
fmt.Printf("Spending %d out of %d outputs...\n", len(tx.TxIn), len(unspentOuts))
}
// Build transaction outputs:
for o := range sendTo {
outs, er := btc.NewSpendOutputs(sendTo[o].addr, sendTo[o].amount, testnet)
if er != nil {
fmt.Println("ERROR:", er.Error())
os.Exit(1)
}
tx.TxOut = append(tx.TxOut, outs...)
}
if changeBtc > 0 {
// Add one more output (with the change)
chad := get_change_addr()
if *verbose {
fmt.Println("Sending change", changeBtc, "to", chad.String())
}
outs, er := btc.NewSpendOutputs(chad, changeBtc, testnet)
if er != nil {
fmt.Println("ERROR:", er.Error())
os.Exit(1)
}
tx.TxOut = append(tx.TxOut, outs...)
}
if *message != "" {
// Add NULL output with an arbitrary message
scr := new(bytes.Buffer)
scr.WriteByte(0x6a) // OP_RETURN
btc.WritePutLen(scr, uint32(len(*message)))
scr.Write([]byte(*message))
tx.TxOut = append(tx.TxOut, &btc.TxOut{Value: 0, Pk_script: scr.Bytes()})
}
if *hashes {
dump_hashes_to_sign(tx)
} else {
signed := sign_tx(tx)
write_tx_file(tx)
if apply2bal && signed {
apply_to_balance(tx)
}
}
}