-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Release 0.0.7 #7
Merged
Merged
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c4cde11
add function to build rfb transaction
tok-kkk aa811ab
adding new address types
tok-kkk 07722f6
change to pointer
tok-kkk 6bc53f0
fix address parsing for p2wpkh
tok-kkk 9eff0d2
update the indexer client to include tx output
tok-kkk 23087a9
add new rpc to client
tok-kkk e7a6e5b
add function get the transaciton total fee
tok-kkk b561f32
change the type from int64 to int
tok-kkk bd49ac6
adding test for rbf error
tok-kkk b529341
update the tests
tok-kkk 5d42bff
add values field for output
tok-kkk 113daa3
update the indexer client to support lastSeenTxid
tok-kkk 096bd5d
fix comiplation issue
tok-kkk 29e6793
add block_hash to status of tx
Revantark 6184586
Merge pull request #8 from catalogfi/blockHash
tok-kkk d66f17b
add address_test file
tok-kkk 1f88c90
Merge branch 'release/0.0.7' of github.com:catalogfi/blockchain into …
tok-kkk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package btc | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/btcsuite/btcd/btcec/v2" | ||
"github.com/btcsuite/btcd/btcutil" | ||
"github.com/btcsuite/btcd/chaincfg" | ||
) | ||
|
||
type AddressType string | ||
|
||
var ( | ||
AddressP2PK AddressType = "P2PK" | ||
AddressP2PKH AddressType = "P2PKH" | ||
AddressP2SH AddressType = "P2SH" | ||
AddressP2WPKH AddressType = "P2WPKH" | ||
AddressSegwit AddressType = "Bech32" | ||
AddressTaproot AddressType = "P2TR" | ||
) | ||
|
||
func PublicKeyAddress(pub *btcec.PublicKey, network *chaincfg.Params, addressType AddressType) (btcutil.Address, error) { | ||
switch addressType { | ||
case AddressP2PK: | ||
return btcutil.NewAddressPubKey(pub.SerializeCompressed(), network) | ||
case AddressP2PKH: | ||
return btcutil.NewAddressPubKeyHash(btcutil.Hash160(pub.SerializeCompressed()), network) | ||
case AddressP2WPKH: | ||
return btcutil.NewAddressWitnessPubKeyHash(btcutil.Hash160(pub.SerializeCompressed()), network) | ||
case AddressTaproot: | ||
// todo: add taproot support | ||
panic("todo") | ||
default: | ||
return nil, fmt.Errorf("unsupported address type") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,150 @@ | ||
package btc_test | ||
|
||
import ( | ||
"bytes" | ||
"testing/quick" | ||
"context" | ||
"errors" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/btcsuite/btcd/btcec/v2" | ||
"github.com/btcsuite/btcd/chaincfg" | ||
"github.com/btcsuite/btcd/txscript" | ||
"github.com/catalogfi/blockchain/btc" | ||
"github.com/tyler-smith/go-bip39" | ||
"github.com/catalogfi/blockchain/btc/btctest" | ||
"github.com/catalogfi/blockchain/testutil" | ||
"github.com/fatih/color" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Bitcoin", func() { | ||
Context("keys", func() { | ||
It("should generate deterministic keys from mnemonic and user public key", func() { | ||
test := func() bool { | ||
entropy, err := bip39.NewEntropy(256) | ||
Context("RBF", func() { | ||
It("should be able to build transaction which support rbf", func(ctx context.Context) { | ||
By("Initialization (Update these fields if testing on testnet/mainnet)") | ||
network := &chaincfg.RegressionNetParams | ||
privKey1, p2pkhAddr1, err := btctest.NewBtcKey(network) | ||
Expect(err).To(BeNil()) | ||
_, p2pkhAddr2, err := btctest.NewBtcKey(network) | ||
Expect(err).To(BeNil()) | ||
indexer := btctest.RegtestIndexer() | ||
|
||
By("Funding the addresses") | ||
txhash1, err := testutil.NigiriFaucet(p2pkhAddr1.EncodeAddress()) | ||
Expect(err).To(BeNil()) | ||
By(fmt.Sprintf("Funding address1 %v , txid = %v", p2pkhAddr1.EncodeAddress(), txhash1)) | ||
time.Sleep(5 * time.Second) | ||
|
||
By("Construct a RBF tx which sends money from p2pkhAddr1 to p2pkhAddr2") | ||
utxos, err := indexer.GetUTXOs(ctx, p2pkhAddr1) | ||
Expect(err).To(BeNil()) | ||
amount, feeRate := int64(1e7), 4 | ||
recipients := []btc.Recipient{ | ||
{ | ||
To: p2pkhAddr2.EncodeAddress(), | ||
Amount: amount, | ||
}, | ||
} | ||
transaction, err := btc.BuildRbfTransaction(network, feeRate, btc.NewRawInputs(), utxos, btc.P2pkhUpdater, recipients, p2pkhAddr1) | ||
Expect(err).To(BeNil()) | ||
|
||
By("Sign and submit the fund tx") | ||
for i := range transaction.TxIn { | ||
pkScript, err := txscript.PayToAddrScript(p2pkhAddr1) | ||
Expect(err).To(BeNil()) | ||
|
||
sigScript, err := txscript.SignatureScript(transaction, i, pkScript, txscript.SigHashAll, privKey1, true) | ||
Expect(err).To(BeNil()) | ||
mnemonic, err := bip39.NewMnemonic(entropy) | ||
transaction.TxIn[i].SignatureScript = sigScript | ||
} | ||
Expect(indexer.SubmitTx(ctx, transaction)).Should(Succeed()) | ||
By(color.GreenString("RBF tx hash = %v", transaction.TxHash().String())) | ||
|
||
By("Construct a tx with higher fees") | ||
transaction1, err := btc.BuildTransaction(network, feeRate+5, btc.NewRawInputs(), utxos, btc.P2pkhUpdater, recipients, p2pkhAddr1) | ||
Expect(err).To(BeNil()) | ||
transaction1.TxOut[1].Value++ | ||
|
||
By("Sign and submit replacement tx") | ||
for i := range transaction1.TxIn { | ||
pkScript, err := txscript.PayToAddrScript(p2pkhAddr1) | ||
Expect(err).To(BeNil()) | ||
key, err := btcec.NewPrivateKey() | ||
|
||
sigScript, err := txscript.SignatureScript(transaction1, i, pkScript, txscript.SigHashAll, privKey1, true) | ||
Expect(err).To(BeNil()) | ||
transaction1.TxIn[i].SignatureScript = sigScript | ||
} | ||
Expect(indexer.SubmitTx(ctx, transaction1)).Should(Succeed()) | ||
color.Green("Replacement tx = %v", transaction1.TxHash().String()) | ||
}) | ||
|
||
It("should get an error when trying to replace a mined tx", func() { | ||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) | ||
defer cancel() | ||
|
||
By("Initialise a local regnet client") | ||
network := &chaincfg.RegressionNetParams | ||
client, err := btctest.RegtestClient() | ||
Expect(err).To(BeNil()) | ||
indexer := btctest.RegtestIndexer() | ||
By("New address") | ||
privKey, pkAddr, err := btctest.NewBtcKey(network) | ||
Expect(err).To(BeNil()) | ||
_, toAddr, err := btctest.NewBtcKey(network) | ||
Expect(err).To(BeNil()) | ||
|
||
By("funding the addresses") | ||
txhash, err := testutil.NigiriFaucet(pkAddr.EncodeAddress()) | ||
Expect(err).To(BeNil()) | ||
By(fmt.Sprintf("Funding address1 %v , txid = %v", pkAddr.EncodeAddress(), txhash)) | ||
time.Sleep(5 * time.Second) | ||
|
||
By("Construct a new tx") | ||
utxos, err := indexer.GetUTXOs(ctx, pkAddr) | ||
Expect(err).To(BeNil()) | ||
amount, feeRate := int64(1e5), 5 | ||
recipients := []btc.Recipient{ | ||
{ | ||
To: toAddr.EncodeAddress(), | ||
Amount: amount, | ||
}, | ||
} | ||
transaction, err := btc.BuildRbfTransaction(network, feeRate, btc.NewRawInputs(), utxos, btc.P2pkhUpdater, recipients, pkAddr) | ||
Expect(err).To(BeNil()) | ||
|
||
By("Sign the transaction inputs") | ||
for i := range transaction.TxIn { | ||
pkScript, err := txscript.PayToAddrScript(pkAddr) | ||
Expect(err).To(BeNil()) | ||
|
||
extendedKey1, err := btc.GenerateSystemPrivKey(mnemonic, key.PubKey().SerializeCompressed()) | ||
sigScript, err := txscript.SignatureScript(transaction, i, pkScript, txscript.SigHashAll, privKey, true) | ||
Expect(err).To(BeNil()) | ||
extendedKey2, err := btc.GenerateSystemPrivKey(mnemonic, key.PubKey().SerializeCompressed()) | ||
transaction.TxIn[i].SignatureScript = sigScript | ||
} | ||
|
||
By("Submit the transaction") | ||
Expect(client.SubmitTx(ctx, transaction)).Should(Succeed()) | ||
By(fmt.Sprintf("Funding tx hash = %v", color.YellowString(transaction.TxHash().String()))) | ||
time.Sleep(time.Second) | ||
|
||
By("Build a new tx with higher fee") | ||
feeRate += 2 | ||
transaction, err = btc.BuildRbfTransaction(network, feeRate, btc.NewRawInputs(), utxos, btc.P2pkhUpdater, recipients, pkAddr) | ||
Expect(err).To(BeNil()) | ||
|
||
By("Sign the transaction inputs") | ||
for i := range transaction.TxIn { | ||
pkScript, err := txscript.PayToAddrScript(pkAddr) | ||
Expect(err).To(BeNil()) | ||
|
||
Expect(bytes.Equal(extendedKey1.Serialize(), extendedKey2.Serialize())).Should(BeTrue()) | ||
return true | ||
sigScript, err := txscript.SignatureScript(transaction, i, pkScript, txscript.SigHashAll, privKey, true) | ||
Expect(err).To(BeNil()) | ||
transaction.TxIn[i].SignatureScript = sigScript | ||
Expect(testutil.NigiriNewBlock()).Should(Succeed()) | ||
} | ||
|
||
Expect(quick.Check(test, nil)).NotTo(HaveOccurred()) | ||
By("Submit the transaction again and it should be rejected") | ||
err = client.SubmitTx(ctx, transaction) | ||
Expect(errors.Is(err, btc.ErrTxInputsMissingOrSpent)).Should(BeTrue()) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AddressSegwit is not being used anywhere and p2wpkh is already a segwit address. Any reason to keep it ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AddressSegwit is mostly for future proof and currently is not used anywhere. The whole address file is still experimental. And yeah p2wpkh is a special type address of segwit. Since it is commonly used in a lot places, so it's worth to have a separate type for it, so we know how to how to estimate fees/sign sigature/decode address from key.