Skip to content

Commit ff42d0c

Browse files
committed
feat(add wallet related logic): add wallet related logic
add wallet related logic
1 parent ec4fc08 commit ff42d0c

File tree

5 files changed

+68
-1
lines changed

5 files changed

+68
-1
lines changed

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
.PHONY=build
22

33
build-client:
4-
@CGO_ENABLED=0 GOOS=linux go build -o bin/go-client learn/go-client.go
4+
@CGO_ENABLED=0 GOOS=linux go build -o bin/go-client learn/go-client/go-client.go
5+
6+
build-wallet:
7+
@CGO_ENABLED=0 GOOS=linux go build -o bin/go-wallet learn/go-wallet/go-wallet.go
58

69
run-client: build-client
710
@./bin/go-client
811

12+
run-wallet: build-wallet
13+
@./bin/go-wallet
14+
915
coverage:
1016
@go test -v -cover ./...
1117

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,39 @@ ETH_JSON_RPC_URL=http://localhost:8545
7676
fBalance.SetString(balance.String())
7777
balanceEther := new(big.Float).Quo(fBalance, big.NewFloat(math.Pow10(18)))
7878
fmt.Println("address:", config.AppConfig.EthAddress, "has", balanceEther, "ether")
79+
```
80+
81+
## wallet concept
82+
83+
![wallet-address](wallet-address.png)
84+
85+
## write logic to generate private key, public key and address
86+
87+
```golang
88+
package main
89+
90+
import (
91+
"fmt"
92+
"log"
93+
94+
"github.com/ethereum/go-ethereum/common/hexutil"
95+
"github.com/ethereum/go-ethereum/crypto"
96+
)
97+
98+
func main() {
99+
pvk, err := crypto.GenerateKey()
100+
if err != nil {
101+
log.Fatal(err)
102+
}
103+
// hex value
104+
pData := crypto.FromECDSA(pvk)
105+
// private key
106+
fmt.Println(hexutil.Encode(pData))
107+
108+
pubData := crypto.FromECDSAPub(&pvk.PublicKey)
109+
// public key
110+
fmt.Println(hexutil.Encode(pubData))
111+
// address
112+
fmt.Println(crypto.PubkeyToAddress(pvk.PublicKey).Hex())
113+
}
79114
```
File renamed without changes.

learn/go-wallet/go-wallet.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/ethereum/go-ethereum/common/hexutil"
8+
"github.com/ethereum/go-ethereum/crypto"
9+
)
10+
11+
func main() {
12+
pvk, err := crypto.GenerateKey()
13+
if err != nil {
14+
log.Fatal(err)
15+
}
16+
// hex value
17+
pData := crypto.FromECDSA(pvk)
18+
// private key
19+
fmt.Println(hexutil.Encode(pData))
20+
21+
pubData := crypto.FromECDSAPub(&pvk.PublicKey)
22+
// public key
23+
fmt.Println(hexutil.Encode(pubData))
24+
// address
25+
fmt.Println(crypto.PubkeyToAddress(pvk.PublicKey).Hex())
26+
}

wallet-address.png

779 KB
Loading

0 commit comments

Comments
 (0)