-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet.go
58 lines (51 loc) · 1.31 KB
/
wallet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package iconsdk
import (
"crypto/ecdsa"
"encoding/hex"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"golang.org/x/crypto/sha3"
"hash"
"log"
)
type Wallet struct {
PrivateKey string
PublicKey string
PublicAddress string
}
func NewWallet(existingKey *string) *Wallet {
var privateKeyBytes []byte
var publicKeyBytes []byte
var pubAddressHash hash.Hash
var privateKey *ecdsa.PrivateKey
var err error
if existingKey != nil {
privateKeyBytes, err := hex.DecodeString(*existingKey)
if err != nil {
log.Fatalf("Failed to decode hex string: %v", err)
}
privateKey, err = crypto.ToECDSA(privateKeyBytes)
if err != nil {
return nil
}
} else {
privateKey, err = crypto.GenerateKey()
if err != nil {
log.Fatal(err)
}
}
privateKeyBytes = crypto.FromECDSA(privateKey)
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
log.Fatal("error casting public key to ECDSA")
}
publicKeyBytes = crypto.FromECDSAPub(publicKeyECDSA)
pubAddressHash = sha3.New256()
pubAddressHash.Write(publicKeyBytes[1:])
return &Wallet{
PrivateKey: hexutil.Encode(privateKeyBytes)[2:],
PublicKey: hexutil.Encode(publicKeyBytes)[4:],
PublicAddress: "hx" + hexutil.Encode(pubAddressHash.Sum(nil)[12:])[2:],
}
}