forked from fbsobreira/gotron-sdk
/
export.go
45 lines (41 loc) · 1.09 KB
/
export.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
package account
import (
"fmt"
"path/filepath"
"github.com/fighterlyt/gotron-sdk/pkg/keystore"
"github.com/fighterlyt/gotron-sdk/pkg/store"
)
// ExportPrivateKey from account
func ExportPrivateKey(address, passphrase string) error {
ks := store.FromAddress(address)
allAccounts := ks.Accounts()
for _, account := range allAccounts {
_, key, err := ks.GetDecryptedKey(keystore.Account{Address: account.Address}, passphrase)
if err != nil {
return err
}
fmt.Printf("%064x\n", key.PrivateKey.D)
}
return nil
}
// ExportKeystore to file
func ExportKeystore(address, path, passphrase string) (string, error) {
ks := store.FromAddress(address)
allAccounts := ks.Accounts()
dirPath, err := filepath.Abs(path)
if err != nil {
return "", err
}
outFile := filepath.Join(dirPath, fmt.Sprintf("%s.key", address))
for _, account := range allAccounts {
keyFile, err := ks.Export(keystore.Account{Address: account.Address}, passphrase, passphrase)
if err != nil {
return "", err
}
e := writeToFile(outFile, string(keyFile))
if e != nil {
return "", e
}
}
return outFile, nil
}