Skip to content

Commit

Permalink
utxo outidx, fix sign tx
Browse files Browse the repository at this point in the history
  • Loading branch information
glossd committed Nov 14, 2021
1 parent e5bcc0b commit 6024b11
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 14 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func main() {
fmt.Printf("Private Key: %s\nBitcoin address: %s\n", privateKey, address)
}
```
If you just started learning about bitcoins and blockchain, you probably **don't have any testnet bitcoins**, wondering where I can get some.
People on [bitcoin.stackexchange](https://bitcoin.stackexchange.com/questions/17690/is-there-any-where-to-get-free-testnet-bitcoins) provided a lot of links.

## Sending transaction
First we need to create a transaction then broadcast it to blockchain.
Expand Down Expand Up @@ -59,7 +61,12 @@ func main() {
**For testing purposes** I used `netchain.TestNet`. If you want to send real bitcoins to blockchain you need to specify BTC_API_KEY env var for blockcypher or you could pass your own txutil.CreateParams.Fetch function to txutil.Create.

---
You can send your bitcoins from multiple wallets by specifying `CreateParams.PrivateKeys`, an array of private keys.
If you specified only one destination address you can set `CreateParams.SendAll` to `true` to send all your bitcoins from your private key or keys.
You can also send your bitcoins to multiple addresses by specifying `CreateParams.Destinations`.
For the full list of transaction parameters look inside `txutil.CreateParams`.
#### More options
`txutil.CreateParams`:

| Field | Type | Usage |
|:-------------:|:--------------------:|------- |
| PrivateKeys | []string | send your bitcoins from multiple wallets |
| Destinations | []txutil.Destination | send your bitcoins to multiple addresses |
| SendAll | bool | send all your bitcoins from your private key or keys, but it only works if you specified just one destination |
For the full list of the transaction parameters look inside `txutil.CreateParams`.
2 changes: 1 addition & 1 deletion addressinfo/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type UTXO struct {
TxID string
Pbscript string
Balance int64
TxOutIdx uint32
TxOutIdx int
}

type Fetch func(address string, net netchain.Net) (Address, error)
2 changes: 1 addition & 1 deletion addressinfo/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func FetchFromBlockchain(address string, net netchain.Net) (Address, error) {
TxID: output.TxID,
Pbscript: output.Script,
Balance: output.Value,
TxOutIdx: uint32(output.TxOutputN),
TxOutIdx: output.TxOutputN,
})
balance += output.Value
}
Expand Down
2 changes: 1 addition & 1 deletion addressinfo/blockcypher.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func FetchFromBlockcypher(address string, net netchain.Net) (Address, error) {
for outputIdx, output := range tx.Outputs {
if len(output.Addresses) == 1 && output.Addresses[0] == address {
if output.SpentBy == "" {
utxos = append(utxos, UTXO{TxID: tx.Hash, Balance: output.Value.Int64(), Pbscript: output.Script, TxOutIdx: uint32(outputIdx)})
utxos = append(utxos, UTXO{TxID: tx.Hash, Balance: output.Value.Int64(), Pbscript: output.Script, TxOutIdx: outputIdx})
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion addressinfo/mock.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package addressinfo

import (
"github.com/btcsuite/btcd/wire"
"github.com/glossd/btc/netchain"
mathrand "math/rand"
)

func FetchMock(address string, net netchain.Net) (Address, error) {
var utxoMock = UTXO{
TxID: "5dcb1e3a6fcd02e9e216113deda9a914b2b2191a0bb42383817b737c4c3280e2",
TxID: wire.NewMsgTx(wire.TxVersion).TxHash().String(),
Balance: mathrand.Int63n(50000000) + 1000000,
Pbscript: "76a914fee7132bbe9201c4f1a0f846b5f714d9335e263088ac",
TxOutIdx: 1,
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions txutil/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/glossd/btc/netchain"
"github.com/glossd/btc/wallet"
"sort"
"strconv"
)

const defaultMinerFee = 5000
Expand Down Expand Up @@ -208,7 +209,7 @@ func addInputs(tx *wire.MsgTx, utxos []addressinfo.UTXO) error {
if err != nil {
return err
}
outPoint := wire.NewOutPoint(utxoHash, utxo.TxOutIdx)
outPoint := wire.NewOutPoint(utxoHash, uint32(utxo.TxOutIdx))
txIn := wire.NewTxIn(outPoint, nil, nil)
tx.AddTxIn(txIn)
}
Expand Down Expand Up @@ -301,7 +302,6 @@ func toPayAddress(address string, net netchain.Net) ([]byte, error) {
}

func signTx(tx *wire.MsgTx, addresses []address) error {

type utxoWithKey struct {
addressinfo.UTXO
wif *btcutil.WIF
Expand All @@ -318,12 +318,12 @@ func signTx(tx *wire.MsgTx, addresses []address) error {
if err != nil {
return fmt.Errorf("signing transaction failed, could compute hash utxo=%v", u)
}
utxosToSpendMap[h.String()] = utxoWithKey{UTXO: u, wif: wif}
utxosToSpendMap[h.String() + strconv.Itoa(u.TxOutIdx)] = utxoWithKey{UTXO: u, wif: wif}
}
}

for i, in := range tx.TxIn {
utxoOfIn := utxosToSpendMap[in.PreviousOutPoint.Hash.String()]
utxoOfIn := utxosToSpendMap[in.PreviousOutPoint.Hash.String() + strconv.Itoa(int(in.PreviousOutPoint.Index))]
sourcePkString, err := hex.DecodeString(utxoOfIn.Pbscript)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions txutil/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func TestCreate_SendAll(t *testing.T) {

func TestCreate_MultiplePrivateKeys(t *testing.T) {
rawTx, err := Create(CreateParams{
PrivateKeys: []string{privateKey1, "cMvRbsVJKjRkZTV7tosWEYEu1x8tQcnLEbC64RiKwPeeEz29j8QZ"},
Destination: "mwRL1TpsRSFy5KXbxEd2KrHiD16VvbbAdj",
PrivateKeys: []string{privateKey1, privateKey2},
Destination: destination3,
SendAll: true,
Net: netchain.TestNet,
})
Expand Down

0 comments on commit 6024b11

Please sign in to comment.