// This example demonstrates creating a script which pays to a bitcoin address. // It also prints the created script hex and uses the DisasmString function to // display the disassembled script. func ExamplePayToAddrScript() { // Parse the address to send the coins to into a btcutil.Address // which is useful to ensure the accuracy of the address and determine // the address type. It is also required for the upcoming call to // PayToAddrScript. addressStr := "12gpXQVcCL2qhTNQgyLVdCFG2Qs2px98nV" address, err := btcutil.DecodeAddress(addressStr, &btcnet.MainNetParams) if err != nil { fmt.Println(err) return } // Create a public key script that pays to the address. script, err := btcscript.PayToAddrScript(address) if err != nil { fmt.Println(err) return } fmt.Printf("Script Hex: %x\n", script) disasm, err := btcscript.DisasmString(script) if err != nil { fmt.Println(err) return } fmt.Println("Script Disassembly:", disasm) // Output: // Script Hex: 76a914128004ff2fcaf13b2b91eb654b1dc2b674f7ec6188ac // Script Disassembly: OP_DUP OP_HASH160 128004ff2fcaf13b2b91eb654b1dc2b674f7ec61 OP_EQUALVERIFY OP_CHECKSIG }
func CreateRawTx2(outputs []output, amount, value int64, toAddr, changeAddr string) (rawtx string, err error) { var inputs []btcjson.TransactionInput var rawInputs []btcjson.RawTxInput var amounts = make(map[btcutil.Address]btcutil.Amount) var privKeys []string for _, op := range outputs { inputs = append(inputs, btcjson.TransactionInput{Txid: op.TxHash, Vout: op.TxN}) rawInputs = append(rawInputs, btcjson.RawTxInput{ Txid: op.TxHash, Vout: op.TxN, ScriptPubKey: op.Script, }) privKeys = append(privKeys, op.PrivKey) } addr, err := btcutil.DecodeAddress(toAddr, &btcnet.MainNetParams) if err != nil { return } amounts[addr] = btcutil.Amount(value) if amount > value { addr, err = btcutil.DecodeAddress(changeAddr, &btcnet.MainNetParams) if err != nil { return } amounts[addr] = btcutil.Amount(amount - value) } client, err := btcRpcClient() if err != nil { return } txMsg, err := client.CreateRawTransaction(inputs, amounts) if err != nil { return } txMsg, complete, err := client.SignRawTransaction3(txMsg, rawInputs, privKeys) if err != nil { return } if !complete { return "", errors.New("not complete") } buffer := &bytes.Buffer{} if err = txMsg.BtcEncode(buffer, 1); err != nil { return } return hex.EncodeToString(buffer.Bytes()), nil }
func handler(w http.ResponseWriter, req *http.Request) { log.Printf("Request!: %s\n", req.Method) if req.Method != "POST" { http.Error(w, "Method not allowed", 405) return } if 0 > req.ContentLength || req.ContentLength > 1024 { http.Error(w, "GTFO", 411) return } buf, err := ioutil.ReadAll(req.Body) if err != nil { log.Printf("failed to read body: %s\n", err) http.Error(w, "bad buffer", 500) return } logger.Printf("POST: %s\n", buf) var proof ProofMessage if err := json.Unmarshal(buf, &proof); err != nil { logger.Printf("failed to parse json: %s\n", err) http.Error(w, "bad json", 500) return } if !check(proof) { logger.Printf("Did not pass test\n") http.Error(w, "bad proof", 405) return } _, err = btcutil.DecodeAddress(proof.Address, params.NetParams) if err != nil { logger.Printf("Bad address: %s\n", err) http.Error(w, "bad addr", 405) return } singleBuilder := btcbuilder.NewToAddrBuilder(params, proof.Address) // use builder interface // Generate the anonymous tx fundingtx, err := singleBuilder.Build() if err != nil { logger.Println(err) return http.Error(w, "Bad", 500) } logger.Println(btcbuilder.ToHex(fundingtx)) message := AnonTxMessage{Tx: btcbuilder.ToHex(fundingtx)} bytes, err := json.Marshal(message) if err != nil { http.Error(w, "Cannot serialize the tx", 500) return } w.Header().Set("Content-Type", "application/json") w.Write(bytes) logger.Println("Message sent") }
func NewToAddrBuilder(params BuilderParams, addr string) *ToAddrBuilder { btcaddr, _ := btcutil.DecodeAddress(addr, params.NetParams) taB := ToAddrBuilder{ Params: params, Addr: btcaddr, } return &taB }
func makeScriptPubKey(toAddr string) ([]byte, error) { addr, err := btcutil.DecodeAddress(toAddr, &btcnet.MainNetParams) if err != nil { return nil, err } log.Println("script addr:", hex.EncodeToString(addr.ScriptAddress())) builder := btcscript.NewScriptBuilder() builder.AddOp(btcscript.OP_DUP).AddOp(btcscript.OP_HASH160) builder.AddData(addr.ScriptAddress()) builder.AddOp(btcscript.OP_EQUALVERIFY).AddOp(btcscript.OP_CHECKSIG) //script := "76" + "a9" + "14" + hex.EncodeToString(addr.ScriptAddress()) + "88" + "ac" return builder.Script(), nil }
func PayToAddrScript(addressStr string) { // Parse the address to send the coins to into a btcutil.Address // which is useful to ensure the accuracy of the address and determine // the address type. It is also required for the upcoming call to // PayToAddrScript. address, err := btcutil.DecodeAddress(addressStr, &btcnet.MainNetParams) handle(err) // Create a public key script that pays to the address. script, err := btcscript.PayToAddrScript(address) handle(err) fmt.Printf("Script Hex: %x\n", script) disasm, err := btcscript.DisasmString(script) handle(err) fmt.Println("Script Disassembly:", disasm) }
func rpcTxPick(exact bool, targetAmnt int64, params BuilderParams) (*TxInParams, error) { // selects an unspent outpoint that is funded over the minAmount list, err := params.Client.ListUnspent() if err != nil { log.Println("list unpsent threw") return nil, err } if len(list) < 1 { return nil, errors.New("No unspent outputs at all.") } for _, prevJson := range list { _amnt, _ := btcutil.NewAmount(prevJson.Amount) amnt := int64(_amnt) txid := prevJson.TxId prevHash, _ := btcwire.NewShaHashFromStr(txid) outPoint := btcwire.NewOutPoint(prevHash, prevJson.Vout) _, contained := params.PendingSet[outPointStr(outPoint)] // This unpsent is in the pending set and it either exactly equals the target or // has a value above that target if !contained && (exact && targetAmnt == amnt || !exact && targetAmnt <= amnt) { // Found one, lets use it script, _ := hex.DecodeString(prevJson.ScriptPubKey) // None of the above ~should~ ever throw errors txOut := btcwire.NewTxOut(amnt, script) prevAddress, _ := btcutil.DecodeAddress(prevJson.Address, params.NetParams) wifkey, err := params.Client.DumpPrivKey(prevAddress) if err != nil { return nil, err } inParams := TxInParams{ TxOut: txOut, OutPoint: outPoint, Wif: wifkey, } params.PendingSet[outPointStr(outPoint)] = struct{}{} return &inParams, nil } } // Never found a good outpoint return nil, errors.New("No txout with the right funds") }
func TestDecodeAddresses(t *testing.T) { for i := range decodeTests { res, net, err := btcutil.DecodeAddress(decodeTests[i].addr) if err != decodeTests[i].err { t.Error(err) } if err != nil { continue } if !bytes.Equal(res, decodeTests[i].res) { t.Errorf("Results differ: Expected '%v', returned '%v'", decodeTests[i].res, res) } if net != decodeTests[i].net { t.Errorf("Networks differ: Expected '%v', returned '%v'", decodeTests[i].net, net) } } }
// txToPairs creates a raw transaction sending the amounts for each // address/amount pair and fee to each address and the miner. minconf // specifies the minimum number of confirmations required before an // unspent output is eligible for spending. Leftover input funds not sent // to addr or as a fee for the miner are sent to a newly generated // address. If change is needed to return funds back to an owned // address, changeUtxo will point to a unconfirmed (height = -1, zeroed // block hash) Utxo. ErrInsufficientFunds is returned if there are not // enough eligible unspent outputs to create the transaction. func (a *Account) txToPairs(pairs map[string]btcutil.Amount, minconf int) (*CreatedTx, error) { // Wallet must be unlocked to compose transaction. if a.IsLocked() { return nil, wallet.ErrWalletLocked } // Create a new transaction which will include all input scripts. msgtx := btcwire.NewMsgTx() // Calculate minimum amount needed for inputs. var amt btcutil.Amount for _, v := range pairs { // Error out if any amount is negative. if v <= 0 { return nil, ErrNonPositiveAmount } amt += v } // Add outputs to new tx. for addrStr, amt := range pairs { addr, err := btcutil.DecodeAddress(addrStr, cfg.Net()) if err != nil { return nil, fmt.Errorf("cannot decode address: %s", err) } // Add output to spend amt to addr. pkScript, err := btcscript.PayToAddrScript(addr) if err != nil { return nil, fmt.Errorf("cannot create txout script: %s", err) } txout := btcwire.NewTxOut(int64(amt), pkScript) msgtx.AddTxOut(txout) } // Get current block's height and hash. bs, err := GetCurBlock() if err != nil { return nil, err } // Make a copy of msgtx before any inputs are added. This will be // used as a starting point when trying a fee and starting over with // a higher fee if not enough was originally chosen. txNoInputs := msgtx.Copy() unspent, err := a.TxStore.UnspentOutputs() if err != nil { return nil, err } var selectedInputs []*txstore.Credit // These are nil/zeroed until a change address is needed, and reused // again in case a change utxo has already been chosen. var changeAddr btcutil.Address // Get the number of satoshis to increment fee by when searching for // the minimum tx fee needed. fee := btcutil.Amount(0) for { msgtx = txNoInputs.Copy() // Select unspent outputs to be used in transaction based on the amount // neededing to sent, and the current fee estimation. inputs, btcin, err := selectInputs(unspent, amt+fee, minconf) if err != nil { return nil, err } // Check if there are leftover unspent outputs, and return coins back to // a new address we own. // // TODO: change needs to be inserted into a random txout index, or else // this is a privacy risk. change := btcin - amt - fee if change > 0 { // Get a new change address if one has not already been found. if changeAddr == nil { changeAddr, err = a.ChangeAddress(&bs, cfg.KeypoolSize) if err != nil { return nil, fmt.Errorf("failed to get next address: %s", err) } // Mark change address as belonging to this account. AcctMgr.MarkAddressForAccount(changeAddr, a) } // Spend change. pkScript, err := btcscript.PayToAddrScript(changeAddr) if err != nil { return nil, fmt.Errorf("cannot create txout script: %s", err) } msgtx.AddTxOut(btcwire.NewTxOut(int64(change), pkScript)) } // Selected unspent outputs become new transaction's inputs. for _, ip := range inputs { msgtx.AddTxIn(btcwire.NewTxIn(ip.OutPoint(), nil)) } for i, input := range inputs { _, addrs, _, _ := input.Addresses(cfg.Net()) if len(addrs) != 1 { continue } apkh, ok := addrs[0].(*btcutil.AddressPubKeyHash) if !ok { continue // don't handle inputs to this yes } ai, err := a.Address(apkh) if err != nil { return nil, fmt.Errorf("cannot get address info: %v", err) } pka := ai.(wallet.PubKeyAddress) privkey, err := pka.PrivKey() if err == wallet.ErrWalletLocked { return nil, wallet.ErrWalletLocked } else if err != nil { return nil, fmt.Errorf("cannot get address key: %v", err) } sigscript, err := btcscript.SignatureScript(msgtx, i, input.TxOut().PkScript, btcscript.SigHashAll, privkey, ai.Compressed()) if err != nil { return nil, fmt.Errorf("cannot create sigscript: %s", err) } msgtx.TxIn[i].SignatureScript = sigscript } noFeeAllowed := false if !cfg.DisallowFree { noFeeAllowed = allowFree(bs.Height, inputs, msgtx.SerializeSize()) } if minFee := minimumFee(msgtx, noFeeAllowed); fee < minFee { fee = minFee } else { selectedInputs = inputs break } } // Validate msgtx before returning the raw transaction. flags := btcscript.ScriptCanonicalSignatures bip16 := time.Now().After(btcscript.Bip16Activation) if bip16 { flags |= btcscript.ScriptBip16 } for i, txin := range msgtx.TxIn { engine, err := btcscript.NewScript(txin.SignatureScript, selectedInputs[i].TxOut().PkScript, i, msgtx, flags) if err != nil { return nil, fmt.Errorf("cannot create script engine: %s", err) } if err = engine.Execute(); err != nil { return nil, fmt.Errorf("cannot validate transaction: %s", err) } } buf := bytes.NewBuffer(nil) buf.Grow(msgtx.SerializeSize()) msgtx.BtcEncode(buf, btcwire.ProtocolVersion) info := &CreatedTx{ tx: btcutil.NewTx(msgtx), inputs: selectedInputs, changeAddr: changeAddr, } return info, nil }
func TestAddresses(t *testing.T) { tests := []struct { name string addr string encoded string valid bool result btcutil.Address f func() (btcutil.Address, error) net *btcnet.Params }{ // Positive P2PKH tests. { name: "mainnet p2pkh", addr: "1MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX", encoded: "1MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX", valid: true, result: btcutil.TstAddressPubKeyHash( [ripemd160.Size]byte{ 0xe3, 0x4c, 0xce, 0x70, 0xc8, 0x63, 0x73, 0x27, 0x3e, 0xfc, 0xc5, 0x4c, 0xe7, 0xd2, 0xa4, 0x91, 0xbb, 0x4a, 0x0e, 0x84}, btcnet.MainNetParams.PubKeyHashAddrID), f: func() (btcutil.Address, error) { pkHash := []byte{ 0xe3, 0x4c, 0xce, 0x70, 0xc8, 0x63, 0x73, 0x27, 0x3e, 0xfc, 0xc5, 0x4c, 0xe7, 0xd2, 0xa4, 0x91, 0xbb, 0x4a, 0x0e, 0x84} return btcutil.NewAddressPubKeyHash(pkHash, &btcnet.MainNetParams) }, net: &btcnet.MainNetParams, }, { name: "mainnet p2pkh 2", addr: "12MzCDwodF9G1e7jfwLXfR164RNtx4BRVG", encoded: "12MzCDwodF9G1e7jfwLXfR164RNtx4BRVG", valid: true, result: btcutil.TstAddressPubKeyHash( [ripemd160.Size]byte{ 0x0e, 0xf0, 0x30, 0x10, 0x7f, 0xd2, 0x6e, 0x0b, 0x6b, 0xf4, 0x05, 0x12, 0xbc, 0xa2, 0xce, 0xb1, 0xdd, 0x80, 0xad, 0xaa}, btcnet.MainNetParams.PubKeyHashAddrID), f: func() (btcutil.Address, error) { pkHash := []byte{ 0x0e, 0xf0, 0x30, 0x10, 0x7f, 0xd2, 0x6e, 0x0b, 0x6b, 0xf4, 0x05, 0x12, 0xbc, 0xa2, 0xce, 0xb1, 0xdd, 0x80, 0xad, 0xaa} return btcutil.NewAddressPubKeyHash(pkHash, &btcnet.MainNetParams) }, net: &btcnet.MainNetParams, }, { name: "testnet p2pkh", addr: "mrX9vMRYLfVy1BnZbc5gZjuyaqH3ZW2ZHz", encoded: "mrX9vMRYLfVy1BnZbc5gZjuyaqH3ZW2ZHz", valid: true, result: btcutil.TstAddressPubKeyHash( [ripemd160.Size]byte{ 0x78, 0xb3, 0x16, 0xa0, 0x86, 0x47, 0xd5, 0xb7, 0x72, 0x83, 0xe5, 0x12, 0xd3, 0x60, 0x3f, 0x1f, 0x1c, 0x8d, 0xe6, 0x8f}, btcnet.TestNet3Params.PubKeyHashAddrID), f: func() (btcutil.Address, error) { pkHash := []byte{ 0x78, 0xb3, 0x16, 0xa0, 0x86, 0x47, 0xd5, 0xb7, 0x72, 0x83, 0xe5, 0x12, 0xd3, 0x60, 0x3f, 0x1f, 0x1c, 0x8d, 0xe6, 0x8f} return btcutil.NewAddressPubKeyHash(pkHash, &btcnet.TestNet3Params) }, net: &btcnet.TestNet3Params, }, // Negative P2PKH tests. { name: "p2pkh wrong hash length", addr: "", valid: false, f: func() (btcutil.Address, error) { pkHash := []byte{ 0x00, 0x0e, 0xf0, 0x30, 0x10, 0x7f, 0xd2, 0x6e, 0x0b, 0x6b, 0xf4, 0x05, 0x12, 0xbc, 0xa2, 0xce, 0xb1, 0xdd, 0x80, 0xad, 0xaa} return btcutil.NewAddressPubKeyHash(pkHash, &btcnet.MainNetParams) }, }, { name: "p2pkh bad checksum", addr: "1MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gY", valid: false, }, // Positive P2SH tests. { // Taken from transactions: // output: 3c9018e8d5615c306d72397f8f5eef44308c98fb576a88e030c25456b4f3a7ac // input: 837dea37ddc8b1e3ce646f1a656e79bbd8cc7f558ac56a169626d649ebe2a3ba. name: "mainnet p2sh", addr: "3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC", encoded: "3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC", valid: true, result: btcutil.TstAddressScriptHash( [ripemd160.Size]byte{ 0xf8, 0x15, 0xb0, 0x36, 0xd9, 0xbb, 0xbc, 0xe5, 0xe9, 0xf2, 0xa0, 0x0a, 0xbd, 0x1b, 0xf3, 0xdc, 0x91, 0xe9, 0x55, 0x10}, btcnet.MainNetParams.ScriptHashAddrID), f: func() (btcutil.Address, error) { script := []byte{ 0x52, 0x41, 0x04, 0x91, 0xbb, 0xa2, 0x51, 0x09, 0x12, 0xa5, 0xbd, 0x37, 0xda, 0x1f, 0xb5, 0xb1, 0x67, 0x30, 0x10, 0xe4, 0x3d, 0x2c, 0x6d, 0x81, 0x2c, 0x51, 0x4e, 0x91, 0xbf, 0xa9, 0xf2, 0xeb, 0x12, 0x9e, 0x1c, 0x18, 0x33, 0x29, 0xdb, 0x55, 0xbd, 0x86, 0x8e, 0x20, 0x9a, 0xac, 0x2f, 0xbc, 0x02, 0xcb, 0x33, 0xd9, 0x8f, 0xe7, 0x4b, 0xf2, 0x3f, 0x0c, 0x23, 0x5d, 0x61, 0x26, 0xb1, 0xd8, 0x33, 0x4f, 0x86, 0x41, 0x04, 0x86, 0x5c, 0x40, 0x29, 0x3a, 0x68, 0x0c, 0xb9, 0xc0, 0x20, 0xe7, 0xb1, 0xe1, 0x06, 0xd8, 0xc1, 0x91, 0x6d, 0x3c, 0xef, 0x99, 0xaa, 0x43, 0x1a, 0x56, 0xd2, 0x53, 0xe6, 0x92, 0x56, 0xda, 0xc0, 0x9e, 0xf1, 0x22, 0xb1, 0xa9, 0x86, 0x81, 0x8a, 0x7c, 0xb6, 0x24, 0x53, 0x2f, 0x06, 0x2c, 0x1d, 0x1f, 0x87, 0x22, 0x08, 0x48, 0x61, 0xc5, 0xc3, 0x29, 0x1c, 0xcf, 0xfe, 0xf4, 0xec, 0x68, 0x74, 0x41, 0x04, 0x8d, 0x24, 0x55, 0xd2, 0x40, 0x3e, 0x08, 0x70, 0x8f, 0xc1, 0xf5, 0x56, 0x00, 0x2f, 0x1b, 0x6c, 0xd8, 0x3f, 0x99, 0x2d, 0x08, 0x50, 0x97, 0xf9, 0x97, 0x4a, 0xb0, 0x8a, 0x28, 0x83, 0x8f, 0x07, 0x89, 0x6f, 0xba, 0xb0, 0x8f, 0x39, 0x49, 0x5e, 0x15, 0xfa, 0x6f, 0xad, 0x6e, 0xdb, 0xfb, 0x1e, 0x75, 0x4e, 0x35, 0xfa, 0x1c, 0x78, 0x44, 0xc4, 0x1f, 0x32, 0x2a, 0x18, 0x63, 0xd4, 0x62, 0x13, 0x53, 0xae} return btcutil.NewAddressScriptHash(script, &btcnet.MainNetParams) }, net: &btcnet.MainNetParams, }, { // Taken from transactions: // output: b0539a45de13b3e0403909b8bd1a555b8cbe45fd4e3f3fda76f3a5f52835c29d // input: (not yet redeemed at time test was written) name: "mainnet p2sh 2", addr: "3NukJ6fYZJ5Kk8bPjycAnruZkE5Q7UW7i8", encoded: "3NukJ6fYZJ5Kk8bPjycAnruZkE5Q7UW7i8", valid: true, result: btcutil.TstAddressScriptHash( [ripemd160.Size]byte{ 0xe8, 0xc3, 0x00, 0xc8, 0x79, 0x86, 0xef, 0xa8, 0x4c, 0x37, 0xc0, 0x51, 0x99, 0x29, 0x01, 0x9e, 0xf8, 0x6e, 0xb5, 0xb4}, btcnet.MainNetParams.ScriptHashAddrID), f: func() (btcutil.Address, error) { hash := []byte{ 0xe8, 0xc3, 0x00, 0xc8, 0x79, 0x86, 0xef, 0xa8, 0x4c, 0x37, 0xc0, 0x51, 0x99, 0x29, 0x01, 0x9e, 0xf8, 0x6e, 0xb5, 0xb4} return btcutil.NewAddressScriptHashFromHash(hash, &btcnet.MainNetParams) }, net: &btcnet.MainNetParams, }, { // Taken from bitcoind base58_keys_valid. name: "testnet p2sh", addr: "2NBFNJTktNa7GZusGbDbGKRZTxdK9VVez3n", encoded: "2NBFNJTktNa7GZusGbDbGKRZTxdK9VVez3n", valid: true, result: btcutil.TstAddressScriptHash( [ripemd160.Size]byte{ 0xc5, 0x79, 0x34, 0x2c, 0x2c, 0x4c, 0x92, 0x20, 0x20, 0x5e, 0x2c, 0xdc, 0x28, 0x56, 0x17, 0x04, 0x0c, 0x92, 0x4a, 0x0a}, btcnet.TestNet3Params.ScriptHashAddrID), f: func() (btcutil.Address, error) { hash := []byte{ 0xc5, 0x79, 0x34, 0x2c, 0x2c, 0x4c, 0x92, 0x20, 0x20, 0x5e, 0x2c, 0xdc, 0x28, 0x56, 0x17, 0x04, 0x0c, 0x92, 0x4a, 0x0a} return btcutil.NewAddressScriptHashFromHash(hash, &btcnet.TestNet3Params) }, net: &btcnet.TestNet3Params, }, // Negative P2SH tests. { name: "p2sh wrong hash length", addr: "", valid: false, f: func() (btcutil.Address, error) { hash := []byte{ 0x00, 0xf8, 0x15, 0xb0, 0x36, 0xd9, 0xbb, 0xbc, 0xe5, 0xe9, 0xf2, 0xa0, 0x0a, 0xbd, 0x1b, 0xf3, 0xdc, 0x91, 0xe9, 0x55, 0x10} return btcutil.NewAddressScriptHashFromHash(hash, &btcnet.MainNetParams) }, }, // Positive P2PK tests. { name: "mainnet p2pk compressed (0x02)", addr: "02192d74d0cb94344c9569c2e77901573d8d7903c3ebec3a957724895dca52c6b4", encoded: "13CG6SJ3yHUXo4Cr2RY4THLLJrNFuG3gUg", valid: true, result: btcutil.TstAddressPubKey( []byte{ 0x02, 0x19, 0x2d, 0x74, 0xd0, 0xcb, 0x94, 0x34, 0x4c, 0x95, 0x69, 0xc2, 0xe7, 0x79, 0x01, 0x57, 0x3d, 0x8d, 0x79, 0x03, 0xc3, 0xeb, 0xec, 0x3a, 0x95, 0x77, 0x24, 0x89, 0x5d, 0xca, 0x52, 0xc6, 0xb4}, btcutil.PKFCompressed, btcnet.MainNetParams.PubKeyHashAddrID), f: func() (btcutil.Address, error) { serializedPubKey := []byte{ 0x02, 0x19, 0x2d, 0x74, 0xd0, 0xcb, 0x94, 0x34, 0x4c, 0x95, 0x69, 0xc2, 0xe7, 0x79, 0x01, 0x57, 0x3d, 0x8d, 0x79, 0x03, 0xc3, 0xeb, 0xec, 0x3a, 0x95, 0x77, 0x24, 0x89, 0x5d, 0xca, 0x52, 0xc6, 0xb4} return btcutil.NewAddressPubKey(serializedPubKey, &btcnet.MainNetParams) }, net: &btcnet.MainNetParams, }, { name: "mainnet p2pk compressed (0x03)", addr: "03b0bd634234abbb1ba1e986e884185c61cf43e001f9137f23c2c409273eb16e65", encoded: "15sHANNUBSh6nDp8XkDPmQcW6n3EFwmvE6", valid: true, result: btcutil.TstAddressPubKey( []byte{ 0x03, 0xb0, 0xbd, 0x63, 0x42, 0x34, 0xab, 0xbb, 0x1b, 0xa1, 0xe9, 0x86, 0xe8, 0x84, 0x18, 0x5c, 0x61, 0xcf, 0x43, 0xe0, 0x01, 0xf9, 0x13, 0x7f, 0x23, 0xc2, 0xc4, 0x09, 0x27, 0x3e, 0xb1, 0x6e, 0x65}, btcutil.PKFCompressed, btcnet.MainNetParams.PubKeyHashAddrID), f: func() (btcutil.Address, error) { serializedPubKey := []byte{ 0x03, 0xb0, 0xbd, 0x63, 0x42, 0x34, 0xab, 0xbb, 0x1b, 0xa1, 0xe9, 0x86, 0xe8, 0x84, 0x18, 0x5c, 0x61, 0xcf, 0x43, 0xe0, 0x01, 0xf9, 0x13, 0x7f, 0x23, 0xc2, 0xc4, 0x09, 0x27, 0x3e, 0xb1, 0x6e, 0x65} return btcutil.NewAddressPubKey(serializedPubKey, &btcnet.MainNetParams) }, net: &btcnet.MainNetParams, }, { name: "mainnet p2pk uncompressed (0x04)", addr: "0411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2" + "e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3", encoded: "12cbQLTFMXRnSzktFkuoG3eHoMeFtpTu3S", valid: true, result: btcutil.TstAddressPubKey( []byte{ 0x04, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a, 0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc, 0x1e, 0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48, 0x2e, 0xca, 0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a, 0x5c, 0xb2, 0xe0, 0xea, 0xdd, 0xfb, 0x84, 0xcc, 0xf9, 0x74, 0x44, 0x64, 0xf8, 0x2e, 0x16, 0x0b, 0xfa, 0x9b, 0x8b, 0x64, 0xf9, 0xd4, 0xc0, 0x3f, 0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56, 0xb4, 0x12, 0xa3}, btcutil.PKFUncompressed, btcnet.MainNetParams.PubKeyHashAddrID), f: func() (btcutil.Address, error) { serializedPubKey := []byte{ 0x04, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a, 0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc, 0x1e, 0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48, 0x2e, 0xca, 0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a, 0x5c, 0xb2, 0xe0, 0xea, 0xdd, 0xfb, 0x84, 0xcc, 0xf9, 0x74, 0x44, 0x64, 0xf8, 0x2e, 0x16, 0x0b, 0xfa, 0x9b, 0x8b, 0x64, 0xf9, 0xd4, 0xc0, 0x3f, 0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56, 0xb4, 0x12, 0xa3} return btcutil.NewAddressPubKey(serializedPubKey, &btcnet.MainNetParams) }, net: &btcnet.MainNetParams, }, { name: "mainnet p2pk hybrid (0x06)", addr: "06192d74d0cb94344c9569c2e77901573d8d7903c3ebec3a957724895dca52c6b4" + "0d45264838c0bd96852662ce6a847b197376830160c6d2eb5e6a4c44d33f453e", encoded: "1Ja5rs7XBZnK88EuLVcFqYGMEbBitzchmX", valid: true, result: btcutil.TstAddressPubKey( []byte{ 0x06, 0x19, 0x2d, 0x74, 0xd0, 0xcb, 0x94, 0x34, 0x4c, 0x95, 0x69, 0xc2, 0xe7, 0x79, 0x01, 0x57, 0x3d, 0x8d, 0x79, 0x03, 0xc3, 0xeb, 0xec, 0x3a, 0x95, 0x77, 0x24, 0x89, 0x5d, 0xca, 0x52, 0xc6, 0xb4, 0x0d, 0x45, 0x26, 0x48, 0x38, 0xc0, 0xbd, 0x96, 0x85, 0x26, 0x62, 0xce, 0x6a, 0x84, 0x7b, 0x19, 0x73, 0x76, 0x83, 0x01, 0x60, 0xc6, 0xd2, 0xeb, 0x5e, 0x6a, 0x4c, 0x44, 0xd3, 0x3f, 0x45, 0x3e}, btcutil.PKFHybrid, btcnet.MainNetParams.PubKeyHashAddrID), f: func() (btcutil.Address, error) { serializedPubKey := []byte{ 0x06, 0x19, 0x2d, 0x74, 0xd0, 0xcb, 0x94, 0x34, 0x4c, 0x95, 0x69, 0xc2, 0xe7, 0x79, 0x01, 0x57, 0x3d, 0x8d, 0x79, 0x03, 0xc3, 0xeb, 0xec, 0x3a, 0x95, 0x77, 0x24, 0x89, 0x5d, 0xca, 0x52, 0xc6, 0xb4, 0x0d, 0x45, 0x26, 0x48, 0x38, 0xc0, 0xbd, 0x96, 0x85, 0x26, 0x62, 0xce, 0x6a, 0x84, 0x7b, 0x19, 0x73, 0x76, 0x83, 0x01, 0x60, 0xc6, 0xd2, 0xeb, 0x5e, 0x6a, 0x4c, 0x44, 0xd3, 0x3f, 0x45, 0x3e} return btcutil.NewAddressPubKey(serializedPubKey, &btcnet.MainNetParams) }, net: &btcnet.MainNetParams, }, { name: "mainnet p2pk hybrid (0x07)", addr: "07b0bd634234abbb1ba1e986e884185c61cf43e001f9137f23c2c409273eb16e65" + "37a576782eba668a7ef8bd3b3cfb1edb7117ab65129b8a2e681f3c1e0908ef7b", encoded: "1ExqMmf6yMxcBMzHjbj41wbqYuqoX6uBLG", valid: true, result: btcutil.TstAddressPubKey( []byte{ 0x07, 0xb0, 0xbd, 0x63, 0x42, 0x34, 0xab, 0xbb, 0x1b, 0xa1, 0xe9, 0x86, 0xe8, 0x84, 0x18, 0x5c, 0x61, 0xcf, 0x43, 0xe0, 0x01, 0xf9, 0x13, 0x7f, 0x23, 0xc2, 0xc4, 0x09, 0x27, 0x3e, 0xb1, 0x6e, 0x65, 0x37, 0xa5, 0x76, 0x78, 0x2e, 0xba, 0x66, 0x8a, 0x7e, 0xf8, 0xbd, 0x3b, 0x3c, 0xfb, 0x1e, 0xdb, 0x71, 0x17, 0xab, 0x65, 0x12, 0x9b, 0x8a, 0x2e, 0x68, 0x1f, 0x3c, 0x1e, 0x09, 0x08, 0xef, 0x7b}, btcutil.PKFHybrid, btcnet.MainNetParams.PubKeyHashAddrID), f: func() (btcutil.Address, error) { serializedPubKey := []byte{ 0x07, 0xb0, 0xbd, 0x63, 0x42, 0x34, 0xab, 0xbb, 0x1b, 0xa1, 0xe9, 0x86, 0xe8, 0x84, 0x18, 0x5c, 0x61, 0xcf, 0x43, 0xe0, 0x01, 0xf9, 0x13, 0x7f, 0x23, 0xc2, 0xc4, 0x09, 0x27, 0x3e, 0xb1, 0x6e, 0x65, 0x37, 0xa5, 0x76, 0x78, 0x2e, 0xba, 0x66, 0x8a, 0x7e, 0xf8, 0xbd, 0x3b, 0x3c, 0xfb, 0x1e, 0xdb, 0x71, 0x17, 0xab, 0x65, 0x12, 0x9b, 0x8a, 0x2e, 0x68, 0x1f, 0x3c, 0x1e, 0x09, 0x08, 0xef, 0x7b} return btcutil.NewAddressPubKey(serializedPubKey, &btcnet.MainNetParams) }, net: &btcnet.MainNetParams, }, { name: "testnet p2pk compressed (0x02)", addr: "02192d74d0cb94344c9569c2e77901573d8d7903c3ebec3a957724895dca52c6b4", encoded: "mhiDPVP2nJunaAgTjzWSHCYfAqxxrxzjmo", valid: true, result: btcutil.TstAddressPubKey( []byte{ 0x02, 0x19, 0x2d, 0x74, 0xd0, 0xcb, 0x94, 0x34, 0x4c, 0x95, 0x69, 0xc2, 0xe7, 0x79, 0x01, 0x57, 0x3d, 0x8d, 0x79, 0x03, 0xc3, 0xeb, 0xec, 0x3a, 0x95, 0x77, 0x24, 0x89, 0x5d, 0xca, 0x52, 0xc6, 0xb4}, btcutil.PKFCompressed, btcnet.TestNet3Params.PubKeyHashAddrID), f: func() (btcutil.Address, error) { serializedPubKey := []byte{ 0x02, 0x19, 0x2d, 0x74, 0xd0, 0xcb, 0x94, 0x34, 0x4c, 0x95, 0x69, 0xc2, 0xe7, 0x79, 0x01, 0x57, 0x3d, 0x8d, 0x79, 0x03, 0xc3, 0xeb, 0xec, 0x3a, 0x95, 0x77, 0x24, 0x89, 0x5d, 0xca, 0x52, 0xc6, 0xb4} return btcutil.NewAddressPubKey(serializedPubKey, &btcnet.TestNet3Params) }, net: &btcnet.TestNet3Params, }, { name: "testnet p2pk compressed (0x03)", addr: "03b0bd634234abbb1ba1e986e884185c61cf43e001f9137f23c2c409273eb16e65", encoded: "mkPETRTSzU8MZLHkFKBmbKppxmdw9qT42t", valid: true, result: btcutil.TstAddressPubKey( []byte{ 0x03, 0xb0, 0xbd, 0x63, 0x42, 0x34, 0xab, 0xbb, 0x1b, 0xa1, 0xe9, 0x86, 0xe8, 0x84, 0x18, 0x5c, 0x61, 0xcf, 0x43, 0xe0, 0x01, 0xf9, 0x13, 0x7f, 0x23, 0xc2, 0xc4, 0x09, 0x27, 0x3e, 0xb1, 0x6e, 0x65}, btcutil.PKFCompressed, btcnet.TestNet3Params.PubKeyHashAddrID), f: func() (btcutil.Address, error) { serializedPubKey := []byte{ 0x03, 0xb0, 0xbd, 0x63, 0x42, 0x34, 0xab, 0xbb, 0x1b, 0xa1, 0xe9, 0x86, 0xe8, 0x84, 0x18, 0x5c, 0x61, 0xcf, 0x43, 0xe0, 0x01, 0xf9, 0x13, 0x7f, 0x23, 0xc2, 0xc4, 0x09, 0x27, 0x3e, 0xb1, 0x6e, 0x65} return btcutil.NewAddressPubKey(serializedPubKey, &btcnet.TestNet3Params) }, net: &btcnet.TestNet3Params, }, { name: "testnet p2pk uncompressed (0x04)", addr: "0411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5" + "cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3", encoded: "mh8YhPYEAYs3E7EVyKtB5xrcfMExkkdEMF", valid: true, result: btcutil.TstAddressPubKey( []byte{ 0x04, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a, 0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc, 0x1e, 0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48, 0x2e, 0xca, 0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a, 0x5c, 0xb2, 0xe0, 0xea, 0xdd, 0xfb, 0x84, 0xcc, 0xf9, 0x74, 0x44, 0x64, 0xf8, 0x2e, 0x16, 0x0b, 0xfa, 0x9b, 0x8b, 0x64, 0xf9, 0xd4, 0xc0, 0x3f, 0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56, 0xb4, 0x12, 0xa3}, btcutil.PKFUncompressed, btcnet.TestNet3Params.PubKeyHashAddrID), f: func() (btcutil.Address, error) { serializedPubKey := []byte{ 0x04, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a, 0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc, 0x1e, 0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48, 0x2e, 0xca, 0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a, 0x5c, 0xb2, 0xe0, 0xea, 0xdd, 0xfb, 0x84, 0xcc, 0xf9, 0x74, 0x44, 0x64, 0xf8, 0x2e, 0x16, 0x0b, 0xfa, 0x9b, 0x8b, 0x64, 0xf9, 0xd4, 0xc0, 0x3f, 0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56, 0xb4, 0x12, 0xa3} return btcutil.NewAddressPubKey(serializedPubKey, &btcnet.TestNet3Params) }, net: &btcnet.TestNet3Params, }, { name: "testnet p2pk hybrid (0x06)", addr: "06192d74d0cb94344c9569c2e77901573d8d7903c3ebec3a957724895dca52c6b" + "40d45264838c0bd96852662ce6a847b197376830160c6d2eb5e6a4c44d33f453e", encoded: "my639vCVzbDZuEiX44adfTUg6anRomZLEP", valid: true, result: btcutil.TstAddressPubKey( []byte{ 0x06, 0x19, 0x2d, 0x74, 0xd0, 0xcb, 0x94, 0x34, 0x4c, 0x95, 0x69, 0xc2, 0xe7, 0x79, 0x01, 0x57, 0x3d, 0x8d, 0x79, 0x03, 0xc3, 0xeb, 0xec, 0x3a, 0x95, 0x77, 0x24, 0x89, 0x5d, 0xca, 0x52, 0xc6, 0xb4, 0x0d, 0x45, 0x26, 0x48, 0x38, 0xc0, 0xbd, 0x96, 0x85, 0x26, 0x62, 0xce, 0x6a, 0x84, 0x7b, 0x19, 0x73, 0x76, 0x83, 0x01, 0x60, 0xc6, 0xd2, 0xeb, 0x5e, 0x6a, 0x4c, 0x44, 0xd3, 0x3f, 0x45, 0x3e}, btcutil.PKFHybrid, btcnet.TestNet3Params.PubKeyHashAddrID), f: func() (btcutil.Address, error) { serializedPubKey := []byte{ 0x06, 0x19, 0x2d, 0x74, 0xd0, 0xcb, 0x94, 0x34, 0x4c, 0x95, 0x69, 0xc2, 0xe7, 0x79, 0x01, 0x57, 0x3d, 0x8d, 0x79, 0x03, 0xc3, 0xeb, 0xec, 0x3a, 0x95, 0x77, 0x24, 0x89, 0x5d, 0xca, 0x52, 0xc6, 0xb4, 0x0d, 0x45, 0x26, 0x48, 0x38, 0xc0, 0xbd, 0x96, 0x85, 0x26, 0x62, 0xce, 0x6a, 0x84, 0x7b, 0x19, 0x73, 0x76, 0x83, 0x01, 0x60, 0xc6, 0xd2, 0xeb, 0x5e, 0x6a, 0x4c, 0x44, 0xd3, 0x3f, 0x45, 0x3e} return btcutil.NewAddressPubKey(serializedPubKey, &btcnet.TestNet3Params) }, net: &btcnet.TestNet3Params, }, { name: "testnet p2pk hybrid (0x07)", addr: "07b0bd634234abbb1ba1e986e884185c61cf43e001f9137f23c2c409273eb16e6" + "537a576782eba668a7ef8bd3b3cfb1edb7117ab65129b8a2e681f3c1e0908ef7b", encoded: "muUnepk5nPPrxUTuTAhRqrpAQuSWS5fVii", valid: true, result: btcutil.TstAddressPubKey( []byte{ 0x07, 0xb0, 0xbd, 0x63, 0x42, 0x34, 0xab, 0xbb, 0x1b, 0xa1, 0xe9, 0x86, 0xe8, 0x84, 0x18, 0x5c, 0x61, 0xcf, 0x43, 0xe0, 0x01, 0xf9, 0x13, 0x7f, 0x23, 0xc2, 0xc4, 0x09, 0x27, 0x3e, 0xb1, 0x6e, 0x65, 0x37, 0xa5, 0x76, 0x78, 0x2e, 0xba, 0x66, 0x8a, 0x7e, 0xf8, 0xbd, 0x3b, 0x3c, 0xfb, 0x1e, 0xdb, 0x71, 0x17, 0xab, 0x65, 0x12, 0x9b, 0x8a, 0x2e, 0x68, 0x1f, 0x3c, 0x1e, 0x09, 0x08, 0xef, 0x7b}, btcutil.PKFHybrid, btcnet.TestNet3Params.PubKeyHashAddrID), f: func() (btcutil.Address, error) { serializedPubKey := []byte{ 0x07, 0xb0, 0xbd, 0x63, 0x42, 0x34, 0xab, 0xbb, 0x1b, 0xa1, 0xe9, 0x86, 0xe8, 0x84, 0x18, 0x5c, 0x61, 0xcf, 0x43, 0xe0, 0x01, 0xf9, 0x13, 0x7f, 0x23, 0xc2, 0xc4, 0x09, 0x27, 0x3e, 0xb1, 0x6e, 0x65, 0x37, 0xa5, 0x76, 0x78, 0x2e, 0xba, 0x66, 0x8a, 0x7e, 0xf8, 0xbd, 0x3b, 0x3c, 0xfb, 0x1e, 0xdb, 0x71, 0x17, 0xab, 0x65, 0x12, 0x9b, 0x8a, 0x2e, 0x68, 0x1f, 0x3c, 0x1e, 0x09, 0x08, 0xef, 0x7b} return btcutil.NewAddressPubKey(serializedPubKey, &btcnet.TestNet3Params) }, net: &btcnet.TestNet3Params, }, } for _, test := range tests { // Decode addr and compare error against valid. decoded, err := btcutil.DecodeAddress(test.addr, test.net) if (err == nil) != test.valid { t.Errorf("%v: decoding test failed: %v", test.name, err) return } if err == nil { // Ensure the stringer returns the same address as the // original. if decodedStringer, ok := decoded.(fmt.Stringer); ok { if test.addr != decodedStringer.String() { t.Errorf("%v: String on decoded value does not match expected value: %v != %v", test.name, test.addr, decodedStringer.String()) return } } // Encode again and compare against the original. encoded := decoded.EncodeAddress() if test.encoded != encoded { t.Errorf("%v: decoding and encoding produced different addressess: %v != %v", test.name, test.encoded, encoded) return } // Perform type-specific calculations. var saddr []byte switch d := decoded.(type) { case *btcutil.AddressPubKeyHash: saddr = btcutil.TstAddressSAddr(encoded) case *btcutil.AddressScriptHash: saddr = btcutil.TstAddressSAddr(encoded) case *btcutil.AddressPubKey: // Ignore the error here since the script // address is checked below. saddr, _ = hex.DecodeString(d.String()) } // Check script address, as well as the Hash160 method for P2PKH and // P2SH addresses. if !bytes.Equal(saddr, decoded.ScriptAddress()) { t.Errorf("%v: script addresses do not match:\n%x != \n%x", test.name, saddr, decoded.ScriptAddress()) return } switch a := decoded.(type) { case *btcutil.AddressPubKeyHash: if h := a.Hash160()[:]; !bytes.Equal(saddr, h) { t.Errorf("%v: hashes do not match:\n%x != \n%x", test.name, saddr, h) return } case *btcutil.AddressScriptHash: if h := a.Hash160()[:]; !bytes.Equal(saddr, h) { t.Errorf("%v: hashes do not match:\n%x != \n%x", test.name, saddr, h) return } } // Ensure the address is for the expected network. if !decoded.IsForNet(test.net) { t.Errorf("%v: calculated network does not match expected", test.name) return } } if !test.valid { // If address is invalid, but a creation function exists, // verify that it returns a nil addr and non-nil error. if test.f != nil { _, err := test.f() if err == nil { t.Errorf("%v: address is invalid but creating new address succeeded", test.name) return } } continue } // Valid test, compare address created with f against expected result. addr, err := test.f() if err != nil { t.Errorf("%v: address is valid but creating new address failed with error %v", test.name, err) return } if !reflect.DeepEqual(addr, test.result) { t.Errorf("%v: created address does not match expected result", test.name) return } } }
// handleCreateRawTransaction handles createrawtransaction commands. func handleCreateRawTransaction(s *rpcServer, cmd btcjson.Cmd) (interface{}, error) { c := cmd.(*btcjson.CreateRawTransactionCmd) // Add all transaction inputs to a new transaction after performing // some validity checks. mtx := btcwire.NewMsgTx() for _, input := range c.Inputs { txHash, err := btcwire.NewShaHashFromStr(input.Txid) if err != nil { return nil, btcjson.ErrDecodeHexString } if input.Vout < 0 { return nil, btcjson.Error{ Code: btcjson.ErrInvalidParameter.Code, Message: "Invalid parameter, vout must be positive", } } prevOut := btcwire.NewOutPoint(txHash, uint32(input.Vout)) txIn := btcwire.NewTxIn(prevOut, []byte{}) mtx.AddTxIn(txIn) } // Add all transaction outputs to the transaction after performing // some validity checks. for encodedAddr, amount := range c.Amounts { // Ensure amount is in the valid range for monetary amounts. if amount <= 0 || amount > btcutil.MaxSatoshi { return nil, btcjson.Error{ Code: btcjson.ErrType.Code, Message: "Invalid amount", } } // Decode the provided address. addr, err := btcutil.DecodeAddress(encodedAddr, activeNetParams.btcnet) if err != nil { return nil, btcjson.Error{ Code: btcjson.ErrInvalidAddressOrKey.Code, Message: btcjson.ErrInvalidAddressOrKey.Message + ": " + err.Error(), } } // Ensure the address is one of the supported types and that // the network encoded with the address matches the network the // server is currently on. switch addr.(type) { case *btcutil.AddressPubKeyHash: case *btcutil.AddressScriptHash: default: return nil, btcjson.ErrInvalidAddressOrKey } if !addr.IsForNet(s.server.btcnet) { return nil, btcjson.Error{ Code: btcjson.ErrInvalidAddressOrKey.Code, Message: fmt.Sprintf("%s: %q", btcjson.ErrInvalidAddressOrKey.Message, encodedAddr), } } // Create a new script which pays to the provided address. pkScript, err := btcscript.PayToAddrScript(addr) if err != nil { return nil, btcjson.Error{ Code: btcjson.ErrInternal.Code, Message: err.Error(), } } txOut := btcwire.NewTxOut(amount, pkScript) mtx.AddTxOut(txOut) } // Return the serialized and hex-encoded transaction. mtxHex, err := messageToHex(mtx) if err != nil { return nil, err } return mtxHex, nil }
// loadConfig initializes and parses the config using a config file and command // line options. // // The configuration proceeds as follows: // 1) Start with a default config with sane settings // 2) Pre-parse the command line to check for an alternative config file // 3) Load configuration file overwriting defaults with any specified options // 4) Parse CLI options and overwrite/add any specified options // // The above results in btcd functioning properly without any config settings // while still allowing the user to override settings with config files and // command line options. Command line options always take precedence. func loadConfig() (*config, []string, error) { // Default config. cfg := config{ ConfigFile: defaultConfigFile, DebugLevel: defaultLogLevel, MaxPeers: defaultMaxPeers, BanDuration: defaultBanDuration, RPCMaxClients: defaultMaxRPCClients, RPCMaxWebsockets: defaultMaxRPCWebsockets, DataDir: defaultDataDir, LogDir: defaultLogDir, DbType: defaultDbType, RPCKey: defaultRPCKeyFile, RPCCert: defaultRPCCertFile, FreeTxRelayLimit: defaultFreeTxRelayLimit, BlockMinSize: defaultBlockMinSize, BlockMaxSize: defaultBlockMaxSize, BlockPrioritySize: defaultBlockPrioritySize, Generate: defaultGenerate, } // Service options which are only added on Windows. serviceOpts := serviceOptions{} // Create the home directory if it doesn't already exist. err := os.MkdirAll(btcdHomeDir, 0700) if err != nil { btcdLog.Errorf("%v", err) return nil, nil, err } // Pre-parse the command line options to see if an alternative config // file or the version flag was specified. Any errors can be ignored // here since they will be caught be the final parse below. preCfg := cfg preParser := newConfigParser(&preCfg, &serviceOpts, flags.None) preParser.Parse() // Show the version and exit if the version flag was specified. if preCfg.ShowVersion { appName := filepath.Base(os.Args[0]) appName = strings.TrimSuffix(appName, filepath.Ext(appName)) fmt.Println(appName, "version", version()) os.Exit(0) } // Perform service command and exit if specified. Invalid service // commands show an appropriate error. Only runs on Windows since // the runServiceCommand function will be nil when not on Windows. if serviceOpts.ServiceCommand != "" && runServiceCommand != nil { err := runServiceCommand(serviceOpts.ServiceCommand) if err != nil { fmt.Fprintln(os.Stderr, err) } os.Exit(0) } // Load additional config from file. var configFileError error parser := newConfigParser(&cfg, &serviceOpts, flags.Default) if !(preCfg.RegressionTest || preCfg.SimNet) || preCfg.ConfigFile != defaultConfigFile { err := flags.NewIniParser(parser).ParseFile(preCfg.ConfigFile) if err != nil { if _, ok := err.(*os.PathError); !ok { fmt.Fprintln(os.Stderr, err) parser.WriteHelp(os.Stderr) return nil, nil, err } configFileError = err } } // Don't add peers from the config file when in regression test mode. if preCfg.RegressionTest && len(cfg.AddPeers) > 0 { cfg.AddPeers = nil } // Parse command line options again to ensure they take precedence. remainingArgs, err := parser.Parse() if err != nil { if e, ok := err.(*flags.Error); !ok || e.Type != flags.ErrHelp { parser.WriteHelp(os.Stderr) } return nil, nil, err } // Multiple networks can't be selected simultaneously. funcName := "loadConfig" numNets := 0 if cfg.TestNet3 { numNets++ } if cfg.RegressionTest { numNets++ } if cfg.SimNet { numNets++ } if numNets > 1 { str := "%s: The testnet, regtest, and simnet params can't be " + "used together -- choose one of the three" err := fmt.Errorf(str, funcName) fmt.Fprintln(os.Stderr, err) parser.WriteHelp(os.Stderr) return nil, nil, err } // Choose the active network params based on the selected network. switch { case cfg.TestNet3: activeNetParams = &testNet3Params case cfg.RegressionTest: activeNetParams = ®ressionNetParams case cfg.SimNet: // Also disable dns seeding on the simulation test network. activeNetParams = &simNetParams cfg.DisableDNSSeed = true } // Append the network type to the data directory so it is "namespaced" // per network. In addition to the block database, there are other // pieces of data that are saved to disk such as address manager state. // All data is specific to a network, so namespacing the data directory // means each individual piece of serialized data does not have to // worry about changing names per network and such. cfg.DataDir = cleanAndExpandPath(cfg.DataDir) cfg.DataDir = filepath.Join(cfg.DataDir, netName(activeNetParams)) // Append the network type to the log directory so it is "namespaced" // per network in the same fashion as the data directory. cfg.LogDir = cleanAndExpandPath(cfg.LogDir) cfg.LogDir = filepath.Join(cfg.LogDir, netName(activeNetParams)) // Special show command to list supported subsystems and exit. if cfg.DebugLevel == "show" { fmt.Println("Supported subsystems", supportedSubsystems()) os.Exit(0) } // Initialize logging at the default logging level. initSeelogLogger(filepath.Join(cfg.LogDir, defaultLogFilename)) setLogLevels(defaultLogLevel) // Parse, validate, and set debug log level(s). if err := parseAndSetDebugLevels(cfg.DebugLevel); err != nil { err := fmt.Errorf("%s: %v", funcName, err.Error()) fmt.Fprintln(os.Stderr, err) parser.WriteHelp(os.Stderr) return nil, nil, err } // Validate database type. if !validDbType(cfg.DbType) { str := "%s: The specified database type [%v] is invalid -- " + "supported types %v" err := fmt.Errorf(str, funcName, cfg.DbType, knownDbTypes) fmt.Fprintln(os.Stderr, err) parser.WriteHelp(os.Stderr) return nil, nil, err } // Validate profile port number if cfg.Profile != "" { profilePort, err := strconv.Atoi(cfg.Profile) if err != nil || profilePort < 1024 || profilePort > 65535 { str := "%s: The profile port must be between 1024 and 65535" err := fmt.Errorf(str, funcName) fmt.Fprintln(os.Stderr, err) parser.WriteHelp(os.Stderr) return nil, nil, err } } // Don't allow ban durations that are too short. if cfg.BanDuration < time.Duration(time.Second) { str := "%s: The banduration option may not be less than 1s -- parsed [%v]" err := fmt.Errorf(str, funcName, cfg.BanDuration) fmt.Fprintln(os.Stderr, err) parser.WriteHelp(os.Stderr) return nil, nil, err } // --addPeer and --connect do not mix. if len(cfg.AddPeers) > 0 && len(cfg.ConnectPeers) > 0 { str := "%s: the --addpeer and --connect options can not be " + "mixed" err := fmt.Errorf(str, funcName) fmt.Fprintln(os.Stderr, err) parser.WriteHelp(os.Stderr) return nil, nil, err } // --proxy or --connect without --listen disables listening. if (cfg.Proxy != "" || len(cfg.ConnectPeers) > 0) && len(cfg.Listeners) == 0 { cfg.DisableListen = true } // Connect means no DNS seeding. if len(cfg.ConnectPeers) > 0 { cfg.DisableDNSSeed = true } // Add the default listener if none were specified. The default // listener is all addresses on the listen port for the network // we are to connect to. if len(cfg.Listeners) == 0 { cfg.Listeners = []string{ net.JoinHostPort("", activeNetParams.DefaultPort), } } // The RPC server is disabled if no username or password is provided. if cfg.RPCUser == "" || cfg.RPCPass == "" { cfg.DisableRPC = true } // Default RPC to listen on localhost only. if !cfg.DisableRPC && len(cfg.RPCListeners) == 0 { addrs, err := net.LookupHost("localhost") if err != nil { return nil, nil, err } cfg.RPCListeners = make([]string, 0, len(addrs)) for _, addr := range addrs { addr = net.JoinHostPort(addr, activeNetParams.rpcPort) cfg.RPCListeners = append(cfg.RPCListeners, addr) } } // Limit the max block size to a sane value. if cfg.BlockMaxSize < blockMaxSizeMin || cfg.BlockMaxSize > blockMaxSizeMax { str := "%s: The blockmaxsize option must be in between %d " + "and %d -- parsed [%d]" err := fmt.Errorf(str, funcName, blockMaxSizeMin, blockMaxSizeMax, cfg.BlockMaxSize) fmt.Fprintln(os.Stderr, err) parser.WriteHelp(os.Stderr) return nil, nil, err } // Limit the block priority and minimum block sizes to max block size. cfg.BlockPrioritySize = minUint32(cfg.BlockPrioritySize, cfg.BlockMaxSize) cfg.BlockMinSize = minUint32(cfg.BlockMinSize, cfg.BlockMaxSize) // Check getwork keys are valid and saved parsed versions. cfg.miningAddrs = make([]btcutil.Address, 0, len(cfg.GetWorkKeys)+ len(cfg.MiningAddrs)) for _, strAddr := range cfg.GetWorkKeys { addr, err := btcutil.DecodeAddress(strAddr, activeNetParams.Params) if err != nil { str := "%s: getworkkey '%s' failed to decode: %v" err := fmt.Errorf(str, funcName, strAddr, err) fmt.Fprintln(os.Stderr, err) parser.WriteHelp(os.Stderr) return nil, nil, err } if !addr.IsForNet(activeNetParams.Params) { str := "%s: getworkkey '%s' is on the wrong network" err := fmt.Errorf(str, funcName, strAddr) fmt.Fprintln(os.Stderr, err) parser.WriteHelp(os.Stderr) return nil, nil, err } cfg.miningAddrs = append(cfg.miningAddrs, addr) } // Check mining addresses are valid and saved parsed versions. for _, strAddr := range cfg.MiningAddrs { addr, err := btcutil.DecodeAddress(strAddr, activeNetParams.Params) if err != nil { str := "%s: mining address '%s' failed to decode: %v" err := fmt.Errorf(str, funcName, strAddr, err) fmt.Fprintln(os.Stderr, err) parser.WriteHelp(os.Stderr) return nil, nil, err } if !addr.IsForNet(activeNetParams.Params) { str := "%s: mining address '%s' is on the wrong network" err := fmt.Errorf(str, funcName, strAddr) fmt.Fprintln(os.Stderr, err) parser.WriteHelp(os.Stderr) return nil, nil, err } cfg.miningAddrs = append(cfg.miningAddrs, addr) } // Ensure there is at least one mining address when the generate flag is // set. if cfg.Generate && len(cfg.MiningAddrs) == 0 { str := "%s: the generate flag is set, but there are no mining " + "addresses specified " err := fmt.Errorf(str, funcName) fmt.Fprintln(os.Stderr, err) parser.WriteHelp(os.Stderr) return nil, nil, err } // Add default port to all listener addresses if needed and remove // duplicate addresses. cfg.Listeners = normalizeAddresses(cfg.Listeners, activeNetParams.DefaultPort) // Add default port to all rpc listener addresses if needed and remove // duplicate addresses. cfg.RPCListeners = normalizeAddresses(cfg.RPCListeners, activeNetParams.rpcPort) // Add default port to all added peer addresses if needed and remove // duplicate addresses. cfg.AddPeers = normalizeAddresses(cfg.AddPeers, activeNetParams.DefaultPort) cfg.ConnectPeers = normalizeAddresses(cfg.ConnectPeers, activeNetParams.DefaultPort) // Setup dial and DNS resolution (lookup) functions depending on the // specified options. The default is to use the standard net.Dial // function as well as the system DNS resolver. When a proxy is // specified, the dial function is set to the proxy specific dial // function and the lookup is set to use tor (unless --noonion is // specified in which case the system DNS resolver is used). cfg.dial = net.Dial cfg.lookup = net.LookupIP if cfg.Proxy != "" { proxy := &socks.Proxy{ Addr: cfg.Proxy, Username: cfg.ProxyUser, Password: cfg.ProxyPass, } cfg.dial = proxy.Dial if !cfg.NoOnion { cfg.lookup = func(host string) ([]net.IP, error) { return torLookupIP(host, cfg.Proxy) } } } // Setup onion address dial and DNS resolution (lookup) functions // depending on the specified options. The default is to use the // same dial and lookup functions selected above. However, when an // onion-specific proxy is specified, the onion address dial and // lookup functions are set to use the onion-specific proxy while // leaving the normal dial and lookup functions as selected above. // This allows .onion address traffic to be routed through a different // proxy than normal traffic. if cfg.OnionProxy != "" { cfg.oniondial = func(a, b string) (net.Conn, error) { proxy := &socks.Proxy{ Addr: cfg.OnionProxy, Username: cfg.OnionProxyUser, Password: cfg.OnionProxyPass, } return proxy.Dial(a, b) } cfg.onionlookup = func(host string) ([]net.IP, error) { return torLookupIP(host, cfg.OnionProxy) } } else { cfg.oniondial = cfg.dial cfg.onionlookup = cfg.lookup } // Specifying --noonion means the onion address dial and DNS resolution // (lookup) functions result in an error. if cfg.NoOnion { cfg.oniondial = func(a, b string) (net.Conn, error) { return nil, errors.New("tor has been disabled") } cfg.onionlookup = func(a string) ([]net.IP, error) { return nil, errors.New("tor has been disabled") } } // Warn about missing config file only after all other configuration is // done. This prevents the warning on help messages and invalid // options. Note this should go directly before the return. if configFileError != nil { btcdLog.Warnf("%v", configFileError) } return &cfg, remainingArgs, nil }
// txToPairs creates a raw transaction sending the amounts for each // address/amount pair and fee to each address and the miner. minconf // specifies the minimum number of confirmations required before an // unspent output is eligible for spending. Leftover input funds not sent // to addr or as a fee for the miner are sent to a newly generated // address. If change is needed to return funds back to an owned // address, changeUtxo will point to a unconfirmed (height = -1, zeroed // block hash) Utxo. ErrInsufficientFunds is returned if there are not // enough eligible unspent outputs to create the transaction. func (a *Account) txToPairs(pairs map[string]btcutil.Amount, minconf int) (*CreatedTx, error) { // Wallet must be unlocked to compose transaction. if a.IsLocked() { return nil, wallet.ErrWalletLocked } // Create a new transaction which will include all input scripts. msgtx := btcwire.NewMsgTx() // Calculate minimum amount needed for inputs. var amt btcutil.Amount for _, v := range pairs { // Error out if any amount is negative. if v <= 0 { return nil, ErrNonPositiveAmount } amt += v } // Add outputs to new tx. for addrStr, amt := range pairs { addr, err := btcutil.DecodeAddress(addrStr, activeNet.Params) if err != nil { return nil, fmt.Errorf("cannot decode address: %s", err) } // Add output to spend amt to addr. pkScript, err := btcscript.PayToAddrScript(addr) if err != nil { return nil, fmt.Errorf("cannot create txout script: %s", err) } txout := btcwire.NewTxOut(int64(amt), pkScript) msgtx.AddTxOut(txout) } // Get current block's height and hash. bs, err := GetCurBlock() if err != nil { return nil, err } // Make a copy of msgtx before any inputs are added. This will be // used as a starting point when trying a fee and starting over with // a higher fee if not enough was originally chosen. txNoInputs := msgtx.Copy() unspent, err := a.TxStore.UnspentOutputs() if err != nil { return nil, err } // Filter out unspendable outputs, that is, remove those that (at this // time) are not P2PKH outputs. Other inputs must be manually included // in transactions and sent (for example, using createrawtransaction, // signrawtransaction, and sendrawtransaction). eligible := make([]txstore.Credit, 0, len(unspent)) for i := range unspent { switch btcscript.GetScriptClass(unspent[i].TxOut().PkScript) { case btcscript.PubKeyHashTy: if !unspent[i].Confirmed(minconf, bs.Height) { continue } // Coinbase transactions must have have reached maturity // before their outputs may be spent. if unspent[i].IsCoinbase() { target := btcchain.CoinbaseMaturity if !unspent[i].Confirmed(target, bs.Height) { continue } } // Locked unspent outputs are skipped. if a.LockedOutpoint(*unspent[i].OutPoint()) { continue } eligible = append(eligible, unspent[i]) } } // Sort eligible inputs, as selectInputs expects these to be sorted // by amount in reverse order. sort.Sort(sort.Reverse(ByAmount(eligible))) var selectedInputs []txstore.Credit // changeAddr is nil/zeroed until a change address is needed, and reused // again in case a change utxo has already been chosen. var changeAddr btcutil.Address // Get the number of satoshis to increment fee by when searching for // the minimum tx fee needed. fee := btcutil.Amount(0) for { msgtx = txNoInputs.Copy() // Select eligible outputs to be used in transaction based on the amount // neededing to sent, and the current fee estimation. inputs, btcin, err := selectInputs(eligible, amt, fee, minconf) if err != nil { return nil, err } // Check if there are leftover unspent outputs, and return coins back to // a new address we own. change := btcin - amt - fee if change > 0 { // Get a new change address if one has not already been found. if changeAddr == nil { changeAddr, err = a.ChangeAddress(&bs, cfg.KeypoolSize) if err != nil { return nil, fmt.Errorf("failed to get next address: %s", err) } // Mark change address as belonging to this account. AcctMgr.MarkAddressForAccount(changeAddr, a) } // Spend change. pkScript, err := btcscript.PayToAddrScript(changeAddr) if err != nil { return nil, fmt.Errorf("cannot create txout script: %s", err) } msgtx.AddTxOut(btcwire.NewTxOut(int64(change), pkScript)) // Randomize index of the change output. rng := badrand.New(badrand.NewSource(time.Now().UnixNano())) r := rng.Int31n(int32(len(msgtx.TxOut))) // random index c := len(msgtx.TxOut) - 1 // change index msgtx.TxOut[r], msgtx.TxOut[c] = msgtx.TxOut[c], msgtx.TxOut[r] } // Selected unspent outputs become new transaction's inputs. for _, ip := range inputs { msgtx.AddTxIn(btcwire.NewTxIn(ip.OutPoint(), nil)) } for i, input := range inputs { // Errors don't matter here, as we only consider the // case where len(addrs) == 1. _, addrs, _, _ := input.Addresses(activeNet.Params) if len(addrs) != 1 { continue } apkh, ok := addrs[0].(*btcutil.AddressPubKeyHash) if !ok { continue // don't handle inputs to this yes } ai, err := a.Address(apkh) if err != nil { return nil, fmt.Errorf("cannot get address info: %v", err) } pka := ai.(wallet.PubKeyAddress) privkey, err := pka.PrivKey() if err == wallet.ErrWalletLocked { return nil, wallet.ErrWalletLocked } else if err != nil { return nil, fmt.Errorf("cannot get address key: %v", err) } sigscript, err := btcscript.SignatureScript(msgtx, i, input.TxOut().PkScript, btcscript.SigHashAll, privkey, ai.Compressed()) if err != nil { return nil, fmt.Errorf("cannot create sigscript: %s", err) } msgtx.TxIn[i].SignatureScript = sigscript } noFeeAllowed := false if !cfg.DisallowFree { noFeeAllowed = allowFree(bs.Height, inputs, msgtx.SerializeSize()) } if minFee := minimumFee(msgtx, noFeeAllowed); fee < minFee { fee = minFee } else { selectedInputs = inputs break } } // Validate msgtx before returning the raw transaction. flags := btcscript.ScriptCanonicalSignatures bip16 := time.Now().After(btcscript.Bip16Activation) if bip16 { flags |= btcscript.ScriptBip16 } for i, txin := range msgtx.TxIn { engine, err := btcscript.NewScript(txin.SignatureScript, selectedInputs[i].TxOut().PkScript, i, msgtx, flags) if err != nil { return nil, fmt.Errorf("cannot create script engine: %s", err) } if err = engine.Execute(); err != nil { return nil, fmt.Errorf("cannot validate transaction: %s", err) } } buf := bytes.Buffer{} buf.Grow(msgtx.SerializeSize()) if err := msgtx.BtcEncode(&buf, btcwire.ProtocolVersion); err != nil { // Hitting OOM by growing or writing to a bytes.Buffer already // panics, and all returned errors are unexpected. panic(err) } info := &CreatedTx{ tx: btcutil.NewTx(msgtx), inputs: selectedInputs, changeAddr: changeAddr, } return info, nil }
func jsonWSRead(walletNotification chan []byte, replychan chan *btcjson.Reply, body []byte, s *rpcServer) error { var message btcjson.Message err := json.Unmarshal(body, &message) if err != nil { reply := btcjson.Reply{ Result: nil, Error: &btcjson.ErrParse, Id: nil, } log.Tracef("RPCS: reply: %v", reply) replychan <- &reply return fmt.Errorf("RPCS: Error unmarshalling json message: %v", err) } log.Tracef("RPCS: received: %v", message) var rawReply btcjson.Reply defer func() { replychan <- &rawReply close(replychan) }() // Deal with commands switch message.Method { case "getcurrentnet": var net btcwire.BitcoinNet if cfg.TestNet3 { net = btcwire.TestNet3 } else { net = btcwire.MainNet } rawReply = btcjson.Reply{ Result: float64(net), Id: &message.Id, } case "getbestblock": // All other "get block" commands give either the height, the // hash, or both but require the block SHA. This gets both for // the best block. sha, height, err := s.server.db.NewestSha() if err != nil { log.Errorf("RPCS: Error getting newest block: %v", err) rawReply = btcjson.Reply{ Result: nil, Error: &btcjson.ErrBestBlockHash, Id: &message.Id, } return err } rawReply = btcjson.Reply{ Result: map[string]interface{}{ "hash": sha.String(), "height": height, }, Id: &message.Id, } case "rescan": minblock, maxblock := int64(0), btcdb.AllShas params, ok := message.Params.([]interface{}) if !ok || len(params) < 2 { rawReply = btcjson.Reply{ Result: nil, Error: &btcjson.ErrInvalidParams, Id: &message.Id, } return ErrBadParamsField } fminblock, ok := params[0].(float64) if !ok { rawReply = btcjson.Reply{ Result: nil, Error: &btcjson.ErrInvalidParams, Id: &message.Id, } return ErrBadParamsField } minblock = int64(fminblock) iaddrs, ok := params[1].([]interface{}) if !ok { rawReply = btcjson.Reply{ Result: nil, Error: &btcjson.ErrInvalidParams, Id: &message.Id, } return ErrBadParamsField } // addrHashes holds a set of string-ified address hashes. addrHashes := make(map[string]bool, len(iaddrs)) for i := range iaddrs { addr, ok := iaddrs[i].(string) if !ok { rawReply = btcjson.Reply{ Result: nil, Error: &btcjson.ErrInvalidParams, Id: &message.Id, } return ErrBadParamsField } addrhash, _, err := btcutil.DecodeAddress(addr) if err != nil { rawReply = btcjson.Reply{ Result: nil, Error: &btcjson.ErrInvalidParams, Id: &message.Id, } return ErrBadParamsField } addrHashes[string(addrhash)] = true } if len(params) > 2 { fmaxblock, ok := params[2].(float64) if !ok { rawReply = btcjson.Reply{ Result: nil, Error: &btcjson.ErrInvalidParams, Id: &message.Id, } return ErrBadParamsField } maxblock = int64(fmaxblock) } log.Debugf("RPCS: Begining rescan") // FetchHeightRange may not return a complete list of block shas for // the given range, so fetch range as many times as necessary. for { blkshalist, err := s.server.db.FetchHeightRange(minblock, maxblock) if err != nil { return err } if len(blkshalist) == 0 { break } for i := range blkshalist { blk, err := s.server.db.FetchBlockBySha(&blkshalist[i]) if err != nil { return err } txShaList, err := blk.TxShas() if err != nil { return err } txList := s.server.db.FetchTxByShaList(txShaList) for _, txReply := range txList { if txReply.Err != nil || txReply.Tx == nil { continue } for txOutIdx, txout := range txReply.Tx.TxOut { st, txaddrhash, err := btcscript.ScriptToAddrHash(txout.PkScript) if st != btcscript.ScriptAddr || err != nil { continue } txaddr, err := btcutil.EncodeAddress(txaddrhash, s.server.btcnet) if err != nil { log.Errorf("Error encoding address: %v", err) return err } if ok := addrHashes[string(txaddrhash)]; ok { reply := btcjson.Reply{ Result: struct { Sender string `json:"sender"` Receiver string `json:"receiver"` BlockHash string `json:"blockhash"` Height int64 `json:"height"` TxHash string `json:"txhash"` Index uint32 `json:"index"` Amount int64 `json:"amount"` PkScript string `json:"pkscript"` Spent bool `json:"spent"` }{ Sender: "Unknown", // TODO(jrick) Receiver: txaddr, BlockHash: blkshalist[i].String(), Height: blk.Height(), TxHash: txReply.Sha.String(), Index: uint32(txOutIdx), Amount: txout.Value, PkScript: btcutil.Base58Encode(txout.PkScript), Spent: txReply.TxSpent[txOutIdx], }, Error: nil, Id: &message.Id, } replychan <- &reply } } } } if maxblock-minblock > int64(len(blkshalist)) { minblock += int64(len(blkshalist)) } else { break } } rawReply = btcjson.Reply{ Result: nil, Error: nil, Id: &message.Id, } log.Debug("RPCS: Finished rescan") case "notifynewtxs": params, ok := message.Params.([]interface{}) if !ok || len(params) != 1 { rawReply = btcjson.Reply{ Result: nil, Error: &btcjson.ErrInvalidParams, Id: &message.Id, } return ErrBadParamsField } addr, ok := params[0].(string) if !ok { rawReply = btcjson.Reply{ Result: nil, Error: &btcjson.ErrInvalidParams, Id: &message.Id, } return ErrBadParamsField } addrhash, _, err := btcutil.DecodeAddress(addr) if err != nil { jsonError := btcjson.Error{ Code: btcjson.ErrInvalidParams.Code, Message: "Cannot decode address", } rawReply = btcjson.Reply{ Result: nil, Error: &jsonError, Id: &message.Id, } return ErrBadParamsField } var hash addressHash copy(hash[:], addrhash) s.ws.requests.AddTxRequest(walletNotification, hash, message.Id) rawReply = btcjson.Reply{ Result: nil, Error: nil, Id: &message.Id, } case "notifyspent": params, ok := message.Params.([]interface{}) if !ok || len(params) != 2 { rawReply = btcjson.Reply{ Result: nil, Error: &btcjson.ErrInvalidParams, Id: &message.Id, } return ErrBadParamsField } hashBE, ok1 := params[0].(string) index, ok2 := params[1].(float64) if !ok1 || !ok2 { rawReply = btcjson.Reply{ Result: nil, Error: &btcjson.ErrInvalidParams, Id: &message.Id, } return ErrBadParamsField } hash, err := btcwire.NewShaHashFromStr(hashBE) if err != nil { jsonError := btcjson.Error{ Code: btcjson.ErrInvalidParams.Code, Message: "Hash string cannot be parsed.", } rawReply = btcjson.Reply{ Result: nil, Error: &jsonError, Id: &message.Id, } return ErrBadParamsField } op := btcwire.NewOutPoint(hash, uint32(index)) s.ws.requests.AddSpentRequest(walletNotification, op, message.Id) rawReply = btcjson.Reply{ Result: nil, Error: nil, Id: &message.Id, } default: rawReply = btcjson.Reply{ Result: nil, Error: &btcjson.ErrMethodNotFound, Id: &message.Id, } } return btcjson.ErrMethodNotFound }
func createSendCoins() *gtk.Widget { grid, err := gtk.GridNew() if err != nil { log.Fatal(err) } grid.SetOrientation(gtk.ORIENTATION_VERTICAL) sw, err := gtk.ScrolledWindowNew(nil, nil) if err != nil { log.Fatal(err) } sw.SetPolicy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC) sw.SetHExpand(true) sw.SetVExpand(true) grid.Add(sw) entriesGrid, err := gtk.GridNew() if err != nil { log.Fatal(err) } SendCoins.EntryGrid = entriesGrid entriesGrid.SetOrientation(gtk.ORIENTATION_VERTICAL) sw.Add(entriesGrid) insertSendEntries(entriesGrid) bot, err := gtk.GridNew() if err != nil { log.Fatal(err) } btn, err := gtk.ButtonNewWithLabel("Add Recipient") if err != nil { log.Fatal(err) } btn.SetSizeRequest(150, -1) btn.Connect("clicked", func() { insertSendEntries(entriesGrid) }) bot.Add(btn) l, err := gtk.LabelNew("Balance: ") if err != nil { log.Fatal(err) } bot.Add(l) SendCoins.Balance = l submitBtn, err := gtk.ButtonNewWithLabel("Send") if err != nil { log.Fatal(err) } submitBtn.SetSizeRequest(150, -1) submitBtn.SetHAlign(gtk.ALIGN_END) submitBtn.SetHExpand(true) submitBtn.SetSensitive(false) submitBtn.Connect("clicked", func() { sendTo := make(map[string]float64) for e := recipients.Front(); e != nil; e = e.Next() { r := e.Value.(*recipient) // Get and validate address addr, err := r.payTo.GetText() if err != nil { d := errorDialog("Error getting payment address", err.Error()) d.Run() d.Destroy() return } _, net, err := btcutil.DecodeAddress(addr) if err != nil { d := errorDialog("Invalid payment address", fmt.Sprintf("'%v' is not a valid payment address", addr)) d.Run() d.Destroy() return } switch net { case btcwire.MainNet: if !cfg.MainNet { d := errorDialog("Invalid payment address", fmt.Sprintf("'%v' is a mainnet address", addr)) d.Run() d.Destroy() return } case btcwire.TestNet3: if cfg.MainNet { d := errorDialog("Invalid payment address", fmt.Sprintf("'%v' is a testnet address", addr)) d.Run() d.Destroy() return } } // Get amount and units and convert to float64 amt := r.amount.GetValue() // Combo box isn't used right now. /* switch r.combo.GetActive() { case 0: // BTC // nothing case 1: // mBTC amt /= 1000 case 2: // uBTC amt /= 1000000 } */ sendTo[addr] = amt } go txSenderAndReplyListener(sendTo) }) SendCoins.SendBtn = submitBtn bot.Add(submitBtn) grid.Add(bot) return &grid.Container.Widget }