-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
56 lines (46 loc) · 1.11 KB
/
utils.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
package utils
import (
"crypto/x509"
_ "embed"
"encoding/pem"
"fmt"
"io"
"os"
"github.com/lasthyphen/sims/constants"
"github.com/lasthyphen/dijetalgo/ids"
avalancheContants "github.com/lasthyphen/dijetalgo/utils/constants"
"github.com/lasthyphen/dijetalgo/utils/hashing"
)
func CopyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
// Grant permission to copy
if err := os.Chmod(dst, constants.FilePerms); err != nil {
return err
}
_, err = io.Copy(out, in)
if err != nil {
return err
}
return out.Close()
}
func LoadNodeID(stakeCert []byte) (string, error) {
block, _ := pem.Decode(stakeCert)
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return "", fmt.Errorf("%w: problem parsing staking certificate", err)
}
id, err := ids.ToShortID(hashing.PubkeyBytesToAddress(cert.Raw))
if err != nil {
return "", fmt.Errorf("%w: problem deriving staker ID from certificate", err)
}
return id.PrefixedString(avalancheContants.NodeIDPrefix), nil
}