-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
andOneBasketball
committed
Oct 14, 2024
1 parent
0a68f64
commit 4ad9e77
Showing
7 changed files
with
216 additions
and
66 deletions.
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 |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
# OpenZeppelin contracts | ||
/contracts/@openzeppelin | ||
|
||
/keystore |
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,42 +1,44 @@ | ||
package main | ||
|
||
// 钱包账户转账交易 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"math" | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/ethclient" | ||
"context" | ||
"fmt" | ||
"log" | ||
"math" | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/ethclient" | ||
) | ||
|
||
func main() { | ||
client, err := ethclient.Dial("http://127.0.0.1:8545/") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
account := common.HexToAddress("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266") | ||
balance, err := client.BalanceAt(context.Background(), account, nil) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Println(balance) // 25893180161173005034 | ||
|
||
blockNumber := big.NewInt(37) | ||
balanceAt, err := client.BalanceAt(context.Background(), account, blockNumber) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Println(balanceAt) // 25729324269165216042 | ||
|
||
fbalance := new(big.Float) | ||
fbalance.SetString(balanceAt.String()) | ||
ethValue := new(big.Float).Quo(fbalance, big.NewFloat(math.Pow10(18))) | ||
fmt.Println(ethValue) // 25.729324269165216041 | ||
|
||
// PendingBalanceAt 获取的是挂起的余额 | ||
pendingBalance, err := client.PendingBalanceAt(context.Background(), account) | ||
fmt.Println(pendingBalance) // 25729324269165216042 | ||
} | ||
client, err := ethclient.Dial("http://127.0.0.1:8545/") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
account := common.HexToAddress("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266") | ||
balance, err := client.BalanceAt(context.Background(), account, nil) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Println(balance) // 25893180161173005034 | ||
|
||
blockNumber := big.NewInt(37) | ||
balanceAt, err := client.BalanceAt(context.Background(), account, blockNumber) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Println(balanceAt) // 25729324269165216042 | ||
|
||
fbalance := new(big.Float) | ||
fbalance.SetString(balanceAt.String()) | ||
ethValue := new(big.Float).Quo(fbalance, big.NewFloat(math.Pow10(18))) | ||
fmt.Println(ethValue) // 25.729324269165216041 | ||
|
||
// PendingBalanceAt 获取的是挂起的余额 | ||
pendingBalance, err := client.PendingBalanceAt(context.Background(), account) | ||
fmt.Println(pendingBalance) // 25729324269165216042 | ||
} |
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package main | ||
|
||
// 订阅最新的区块数据 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/ethclient" | ||
) | ||
|
||
func main() { | ||
client, err := ethclient.Dial("wss://sepolia.infura.io/ws/v3/2609a5dca17044dcafeb303333027af1") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
headers := make(chan *types.Header) | ||
sub, err := client.SubscribeNewHead(context.Background(), headers) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
for { | ||
select { | ||
case err := <-sub.Err(): | ||
log.Fatal(err) | ||
case header := <-headers: | ||
fmt.Println(header.Hash().Hex()) // 0xbc10defa8dda384c96a17640d84de5578804945d347072e091b4e5f390ddea7f | ||
|
||
block, err := client.BlockByHash(context.Background(), header.Hash()) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fmt.Println(block.Hash().Hex()) // 0xbc10defa8dda384c96a17640d84de5578804945d347072e091b4e5f390ddea7f | ||
fmt.Println(block.Number().Uint64()) // 3477413 | ||
fmt.Println(block.Time(), time.Unix(int64(block.Time()), 0).Format(time.DateTime)) // 1529525947 | ||
fmt.Println(block.Nonce()) // 130524141876765836 | ||
fmt.Println(len(block.Transactions())) // 7 | ||
} | ||
} | ||
} |
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
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,69 @@ | ||
package main | ||
|
||
// 通过助记词生成私钥和账户地址,以及生成对应的keystore文件并保存到本地 | ||
// 分层确定性(HD)钱包,主私钥可以派生无限个子私钥,子私钥可以派生无限个子地址。恢复钱包后依次创建账户即可恢复所有账户 | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"encoding/hex" | ||
"fmt" | ||
"log" | ||
"path/filepath" | ||
|
||
"github.com/ethereum/go-ethereum/accounts/keystore" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
"github.com/tyler-smith/go-bip32" | ||
"github.com/tyler-smith/go-bip39" | ||
) | ||
|
||
func main() { | ||
// 助记词存在12(128位熵、主流)、15(160)、18(192)、21(224)、24(256位熵、安全性最强)个单词 | ||
// 生成 BIP-39 助记词 | ||
entropy, err := bip39.NewEntropy(128) // 128位熵,生成12个单词的助记词 | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
mnemonic, err := bip39.NewMnemonic(entropy) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Println("助记词:", mnemonic) | ||
|
||
// 使用助记词生成种子 | ||
seed := bip39.NewSeed(mnemonic, "") | ||
|
||
// 从种子生成主私钥 | ||
masterKey, err := bip32.NewMasterKey(seed) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Println("主私钥:", masterKey.String()) | ||
|
||
// 从主私钥派生账户的私钥(根据BIP-44路径) | ||
// 以太坊 BIP-44 路径: m/44'/60'/0'/0/0 | ||
purpose, _ := masterKey.NewChildKey(bip32.FirstHardenedChild + 44) | ||
coinType, _ := purpose.NewChildKey(bip32.FirstHardenedChild + 60) | ||
account, _ := coinType.NewChildKey(bip32.FirstHardenedChild + 0) | ||
change, _ := account.NewChildKey(0) | ||
addressIndex, _ := change.NewChildKey(0) | ||
|
||
privateKey, err := crypto.ToECDSA(addressIndex.Key) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Println("私钥:", hex.EncodeToString(crypto.FromECDSA(privateKey))) | ||
|
||
// 生成公钥并获取地址 | ||
publicKey := privateKey.Public().(*ecdsa.PublicKey) | ||
address := crypto.PubkeyToAddress(*publicKey).Hex() | ||
fmt.Println("地址:", address) | ||
|
||
store := keystore.NewKeyStore("./keystore", keystore.StandardScryptN, keystore.StandardScryptP) | ||
key, err := store.ImportECDSA(privateKey, "your-password") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fmt.Println("keystore 文件已保存:", filepath.Join("./keystore", key.Address.Hex())) | ||
} |