From 6f2da422036090f4b61db2d96f5fd98964a15d85 Mon Sep 17 00:00:00 2001 From: Vincent Date: Sat, 16 Dec 2023 17:34:18 +0800 Subject: [PATCH] change mod name && added get private key from mnemonic and hdkey path example --- .../main.go | 42 +++++++++++++++++++ examples/inscribefile/main.go | 4 +- examples/inscribewithoutnoderpc/main.go | 4 +- examples/inscribewithprivatenoderpc/main.go | 2 +- examples/inscribewithpublicnoderpc/main.go | 2 +- go.mod | 3 +- go.sum | 2 + internal/ord/ord.go | 4 +- pkg/btcapi/mempool/addresses.go | 2 +- pkg/btcapi/mempool/client.go | 2 +- 10 files changed, 56 insertions(+), 11 deletions(-) create mode 100644 examples/getprivatekeyfrommnemonicandhdkeypath/main.go diff --git a/examples/getprivatekeyfrommnemonicandhdkeypath/main.go b/examples/getprivatekeyfrommnemonicandhdkeypath/main.go new file mode 100644 index 0000000..1194102 --- /dev/null +++ b/examples/getprivatekeyfrommnemonicandhdkeypath/main.go @@ -0,0 +1,42 @@ +package main + +import ( + "encoding/hex" + "fmt" + "github.com/btcsuite/btcd/btcec/v2/schnorr" + "github.com/btcsuite/btcd/btcutil" + "github.com/btcsuite/btcd/btcutil/hdkeychain" + "github.com/btcsuite/btcd/chaincfg" + "github.com/btcsuite/btcd/txscript" + "github.com/tyler-smith/go-bip39" + "log" +) + +func main() { + // random mnemonic 24 words + entropy, _ := bip39.NewEntropy(256) + randomMnemonic, _ := bip39.NewMnemonic(entropy) + fmt.Println(randomMnemonic) + + mnemonic := randomMnemonic // "your mnemonic" + netParams := &chaincfg.MainNetParams // for generate address + rootKey, err := hdkeychain.NewMaster(bip39.NewSeed(mnemonic, ""), netParams) + if err != nil { + log.Fatal(err) + } + + // for sparrow wallet receive addresses a path is start "m/86'/0'/0'/0/0"; change addresses a path is start "m/86'/0'/0'/1/0" + childKey, _ := rootKey.Derive(hdkeychain.HardenedKeyStart + 86) // 86' + childKey, _ = childKey.Derive(hdkeychain.HardenedKeyStart + 0) // 0' + childKey, _ = childKey.Derive(hdkeychain.HardenedKeyStart + 0) // 0' + childKey, _ = childKey.Derive(1) // 1 + senderKey, _ := childKey.Derive(0) // 0 + senderPrivateKey, _ := senderKey.ECPrivKey() + privateKeyHex := hex.EncodeToString(senderPrivateKey.Serialize()) + log.Printf("priviate key: %s \n", privateKeyHex) + taprootAddress, err := btcutil.NewAddressTaproot(schnorr.SerializePubKey(txscript.ComputeTaprootKeyNoScript(senderPrivateKey.PubKey())), netParams) + if err != nil { + log.Fatal(err) + } + log.Printf("taproot address: %s \n", taprootAddress.EncodeAddress()) +} diff --git a/examples/inscribefile/main.go b/examples/inscribefile/main.go index 1446438..e3743a0 100644 --- a/examples/inscribefile/main.go +++ b/examples/inscribefile/main.go @@ -9,8 +9,8 @@ import ( "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/wire" - "go-ord-tx/internal/ord" - "go-ord-tx/pkg/btcapi/mempool" + "github.com/vincentdebug/go-ord-tx/internal/ord" + "github.com/vincentdebug/go-ord-tx/pkg/btcapi/mempool" "log" "net/http" "os" diff --git a/examples/inscribewithoutnoderpc/main.go b/examples/inscribewithoutnoderpc/main.go index ebed3fe..b0e44a2 100644 --- a/examples/inscribewithoutnoderpc/main.go +++ b/examples/inscribewithoutnoderpc/main.go @@ -8,8 +8,8 @@ import ( "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/wire" - "go-ord-tx/internal/ord" - "go-ord-tx/pkg/btcapi/mempool" + "github.com/vincentdebug/go-ord-tx/internal/ord" + "github.com/vincentdebug/go-ord-tx/pkg/btcapi/mempool" "log" ) diff --git a/examples/inscribewithprivatenoderpc/main.go b/examples/inscribewithprivatenoderpc/main.go index 14d2064..958775a 100644 --- a/examples/inscribewithprivatenoderpc/main.go +++ b/examples/inscribewithprivatenoderpc/main.go @@ -6,7 +6,7 @@ import ( "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/rpcclient" "github.com/btcsuite/btcd/wire" - "go-ord-tx/internal/ord" + "github.com/vincentdebug/go-ord-tx/internal/ord" "log" ) diff --git a/examples/inscribewithpublicnoderpc/main.go b/examples/inscribewithpublicnoderpc/main.go index bc90769..d4a7f93 100644 --- a/examples/inscribewithpublicnoderpc/main.go +++ b/examples/inscribewithpublicnoderpc/main.go @@ -10,7 +10,7 @@ import ( "github.com/btcsuite/btcd/rpcclient" "github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/wire" - "go-ord-tx/internal/ord" + "github.com/vincentdebug/go-ord-tx/internal/ord" "log" ) diff --git a/go.mod b/go.mod index 2eac811..3231b4f 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module go-ord-tx +module github.com/vincentdebug/go-ord-tx go 1.20 @@ -8,6 +8,7 @@ require ( github.com/btcsuite/btcd/btcutil v1.1.3 github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2 github.com/pkg/errors v0.9.1 + github.com/tyler-smith/go-bip39 v1.1.0 ) require ( diff --git a/go.sum b/go.sum index 317fa40..d4d9b5f 100644 --- a/go.sum +++ b/go.sum @@ -78,6 +78,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 h1:epCh84lMvA70Z7CTTCmYQn2CKbY8j86K7/FAIr141uY= github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7/go.mod h1:q4W45IWZaF22tdD+VEXcAWRA037jwmWEB5VWYORlTpc= +github.com/tyler-smith/go-bip39 v1.1.0 h1:5eUemwrMargf3BSLRRCalXT93Ns6pQJIjYQN2nyfOP8= +github.com/tyler-smith/go-bip39 v1.1.0/go.mod h1:gUYDtqQw1JS3ZJ8UWVcGTGqqr6YIN3CWg+kkNaLt55U= golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= diff --git a/internal/ord/ord.go b/internal/ord/ord.go index ab0906b..003cc4e 100644 --- a/internal/ord/ord.go +++ b/internal/ord/ord.go @@ -16,8 +16,8 @@ import ( "github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/wire" "github.com/pkg/errors" - "go-ord-tx/pkg/btcapi" - extRpcClient "go-ord-tx/pkg/rpcclient" + "github.com/vincentdebug/go-ord-tx/pkg/btcapi" + extRpcClient "github.com/vincentdebug/go-ord-tx/pkg/rpcclient" "log" ) diff --git a/pkg/btcapi/mempool/addresses.go b/pkg/btcapi/mempool/addresses.go index 019cf32..f0e8563 100644 --- a/pkg/btcapi/mempool/addresses.go +++ b/pkg/btcapi/mempool/addresses.go @@ -6,7 +6,7 @@ import ( "github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" - "go-ord-tx/pkg/btcapi" + "github.com/vincentdebug/go-ord-tx/pkg/btcapi" "net/http" ) diff --git a/pkg/btcapi/mempool/client.go b/pkg/btcapi/mempool/client.go index ef85680..ae09670 100644 --- a/pkg/btcapi/mempool/client.go +++ b/pkg/btcapi/mempool/client.go @@ -3,7 +3,7 @@ package mempool import ( "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/wire" - "go-ord-tx/pkg/btcapi" + "github.com/vincentdebug/go-ord-tx/pkg/btcapi" "io" "log" )