/
policy_script.go
132 lines (108 loc) · 2.88 KB
/
policy_script.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package core
import (
"encoding/json"
"os"
"path"
"strings"
)
type keyHashSig struct {
Type string `json:"type"`
KeyHash string `json:"keyHash"`
}
type PolicyScript struct {
PolicyScript []byte `json:"ps"`
Count int `json:"cnt"`
}
func NewPolicyScript(keyHashes []string, atLeastSignersCount int) (*PolicyScript, error) {
policyScript, err := createPolicyScript(keyHashes, atLeastSignersCount)
if err != nil {
return nil, err
}
return &PolicyScript{
PolicyScript: policyScript,
Count: len(keyHashes),
}, nil
}
func NewPolicyScriptFromKeyHash(keyHash string) (*PolicyScript, error) {
policyScript, err := json.MarshalIndent(keyHashSig{
Type: "sig",
KeyHash: keyHash,
}, "", " ")
if err != nil {
return nil, err
}
return &PolicyScript{
PolicyScript: policyScript,
Count: 1,
}, nil
}
func (ps PolicyScript) CreateMultiSigAddress(testNetMagic uint) (string, error) {
baseDirectory, err := os.MkdirTemp("", "cardano-multisig-addr")
if err != nil {
return "", err
}
defer func() {
os.RemoveAll(baseDirectory)
os.Remove(baseDirectory)
}()
policyScriptFilePath := path.Join(baseDirectory, "policy-script.json")
if err := os.WriteFile(policyScriptFilePath, ps.PolicyScript, FilePermission); err != nil {
return "", err
}
response, err := runCommand(resolveCardanoCliBinary(), append([]string{
"address", "build",
"--payment-script-file", policyScriptFilePath,
}, getTestNetMagicArgs(testNetMagic)...))
if err != nil {
return "", err
}
return strings.Trim(response, "\n"), nil
}
func (ps PolicyScript) GetPolicyID() (string, error) {
baseDirectory, err := os.MkdirTemp("", "cardano-policy-id")
if err != nil {
return "", err
}
defer func() {
os.RemoveAll(baseDirectory)
os.Remove(baseDirectory)
}()
policyScriptFilePath := path.Join(baseDirectory, "policy-script.json")
if err := os.WriteFile(policyScriptFilePath, ps.PolicyScript, FilePermission); err != nil {
return "", err
}
return getPolicyID(policyScriptFilePath)
}
func (ps PolicyScript) GetPolicyScript() []byte {
return ps.PolicyScript
}
func (ps PolicyScript) GetCount() int {
return ps.Count
}
func createPolicyScript(keyHashes []string, atLeastSignersCount int) ([]byte, error) {
type policyScript struct {
Type string `json:"type"`
Required int `json:"required"`
Scripts []keyHashSig `json:"scripts"`
}
p := policyScript{
Type: "atLeast",
Required: atLeastSignersCount,
}
for _, keyHash := range keyHashes {
p.Scripts = append(p.Scripts, keyHashSig{
Type: "sig",
KeyHash: keyHash,
})
}
return json.MarshalIndent(p, "", " ")
}
func getPolicyID(policyScriptFilePath string) (string, error) {
response, err := runCommand(resolveCardanoCliBinary(), []string{
"transaction", "policyid", "--script-file", policyScriptFilePath,
})
if err != nil {
return "", err
}
return strings.Trim(response, "\n"), nil
}