-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct.go
68 lines (57 loc) · 1.63 KB
/
struct.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
package main
import (
"encoding/json"
"fmt"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcutil/hdkeychain"
"io/ioutil"
"reflect"
)
type KEYPATH struct {
ACCOUNT uint32
CHAIN uint32
ADDRESS uint32
}
type BIP32PARAM struct {
SEED string
PATH KEYPATH
}
// Clear clear the data of a instance especially the importance data like a seed, reduce the possibilities of the malware attack
func Clear(v interface{}) {
p := reflect.ValueOf(v).Elem()
p.Set(reflect.Zero(p.Type()))
}
// GenerateSegwitAddress Generate segwit address using for the bitcoin mainnet by the public key
func GenerateSegwitAddress(key *[]byte) (*string, error) {
witnessProg := btcutil.Hash160(*key)
addressWitnessPubKeyHash, err := btcutil.NewAddressWitnessPubKeyHash(witnessProg, &chaincfg.MainNetParams)
if err != nil {
return nil, err
}
segwitAddress := addressWitnessPubKeyHash.EncodeAddress()
return &segwitAddress, nil
}
// ConvertPublicKey Serialize the HD key struct to a compressed public key data represent by a byte array
func ConvertPublicKey(key *hdkeychain.ExtendedKey) (*[]byte, error) {
ecPubKey, err := key.ECPubKey()
if err != nil {
return nil, err
}
compressed := ecPubKey.SerializeCompressed()
return &compressed, nil
}
// ReadSeedFromJsonFile a helper function to read the json file to a BIP32PARAM instance
func ReadSeedFromJsonFile(file *string) (*BIP32PARAM, error) {
data, err := ioutil.ReadFile(*file)
if err != nil {
fmt.Println("File reading error", err)
return nil, err
}
obj := BIP32PARAM{}
err = json.Unmarshal(data, &obj)
if err != nil {
return nil, err
}
return &obj, nil
}