-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet.go
83 lines (69 loc) · 1.85 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"fmt"
"math/big"
"github.com/blockcypher/gobcy"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcutil"
)
type Wallet struct {
name string
balance big.Int
addressPubKey btcutil.AddressPubKey
wif btcutil.WIF
}
func (wallet *Wallet) updateBalance(bc gobcy.API) {
addr, err := bc.GetAddrBal(wallet.addressPubKey.AddressPubKeyHash().String(), nil)
if err != nil {
fmt.Println(err)
}
wallet.balance = addr.Balance
}
func (wallet *Wallet) Create(name string) error {
privateKey, err := btcec.NewPrivateKey(btcec.S256())
if err != nil {
return err
}
wif, err := btcutil.NewWIF(privateKey, chainParam, true)
if err != nil {
return err
}
publicKey, err := btcutil.NewAddressPubKey(privateKey.PubKey().SerializeCompressed(), chainParam)
if err != nil {
return err
}
wallet.name = name
wallet.balance = big.Int{}
wallet.addressPubKey = *publicKey
wallet.wif = *wif
fmt.Println("name: ", wallet.name)
fmt.Println("balance: ", wallet.balance.Int64())
fmt.Println("public key: ", wallet.addressPubKey.AddressPubKeyHash())
fmt.Println("wif: ", wallet.wif.String())
return nil
}
func (wallet *Wallet) Import(name, inputWIF string) error {
wif, err := btcutil.DecodeWIF(inputWIF)
if err != nil {
fmt.Println(err)
return err
}
publicKey, err := btcutil.NewAddressPubKey(wif.PrivKey.PubKey().SerializeCompressed(), chainParam)
if err != nil {
return err
}
wallet.name = name
wallet.balance = big.Int{}
wallet.addressPubKey = *publicKey
wallet.wif = *wif
return nil
}
func (wallet *Wallet) GetInfo() {
fmt.Println("name: ", wallet.name)
fmt.Println("wif: ", wallet.wif.String())
fmt.Println("pub addr: ", wallet.addressPubKey.AddressPubKeyHash().String())
}
func (wallet *Wallet) GetBalance(bc gobcy.API) {
wallet.updateBalance(bc)
fmt.Println("balance:", wallet.balance.Int64())
}