-
Notifications
You must be signed in to change notification settings - Fork 17
/
multisigauthority_test.go
186 lines (151 loc) · 6.27 KB
/
multisigauthority_test.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//go:build bdd
package emoney_test
import (
"fmt"
"io/ioutil"
"os"
sdk "github.com/cosmos/cosmos-sdk/types"
nt "github.com/e-money/em-ledger/networktest"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
var _ = Describe("Authority", func() {
var (
keystore = testnet.Keystore
emcli = testnet.NewEmcli()
key1 = testnet.Keystore.Key1
key2 = testnet.Keystore.Key2
Authority = keystore.MultiKey
)
Describe("Authority is a multisig account", func() {
It("starts a new testnet", func() {
awaitReady, err := testnet.RestartWithModifications(
func(bz []byte) []byte {
bz, _ = sjson.SetBytes(bz, "app_state.authority.key", Authority.GetAddress())
return bz
})
Expect(err).ShouldNot(HaveOccurred())
Expect(awaitReady()).To(BeTrue())
})
It("Same key signs twice", func() {
_, _ = nt.IncChain(1) // Avoid querying while block height is 1
jsonPath, err := ioutil.TempDir("", "")
Expect(err).To(BeNil())
defer os.RemoveAll(jsonPath)
authorityaddress := sdk.AccAddress(Authority.GetPublicKey().Address()).String()
newMinGasPrices, _ := sdk.ParseDecCoins("0.0006eeur")
tx, err := emcli.CustomCommand("tx", "authority", "set-gas-prices", authorityaddress, newMinGasPrices.String(), "--generate-only", "--from", authorityaddress)
Expect(err).To(BeNil())
transactionPath := fmt.Sprintf("%v/transaction.json", jsonPath)
ioutil.WriteFile(transactionPath, []byte(tx), 0o777)
sigPaths := make([]string, 0)
// Sign twice with key1. Signature count is above threshold, but ...
for i := 0; i < 2; i++ {
tx, err = emcli.SignTranscation(transactionPath, key1.GetAddress(), authorityaddress)
signaturePath := fmt.Sprintf("%v/sign%v.json", jsonPath, i)
ioutil.WriteFile(signaturePath, []byte(tx), 0o777)
sigPaths = append(sigPaths, signaturePath)
}
// Combine the two signatures
tx, err = emcli.CustomCommand("tx", "multisign", transactionPath, "multikey", sigPaths[0], sigPaths[1])
Expect(err).To(BeNil())
ioutil.WriteFile(transactionPath, []byte(tx), 0o777)
tx, err = emcli.CustomCommand("tx", "broadcast", transactionPath)
Expect(err).NotTo(BeNil())
Expect(gjson.Parse(tx).Get("logs.0.success").Exists()).To(Equal(false))
})
It("Replace Authority", func() {
// authority switches from multisig to singlesig
authorityAddress := sdk.AccAddress(Authority.GetPublicKey().Address()).String()
newAuthorityAddress := sdk.AccAddress(keystore.Authority.GetPublicKey().Address()).String()
execAuthMSigTx(authorityAddress, []nt.Key{key1, key2}, "tx", "authority", "replace", authorityAddress,
newAuthorityAddress, "--generate-only", "--from",
authorityAddress, "--fees", "1.0ungm")
// create/revoke issuer with the new authority
ok := nt.AuthCreatesIssuer(emcli, keystore.Authority, key1)
Expect(ok).To(BeTrue())
_, success, err := emcli.AuthorityDestroyIssuer(keystore.Authority, key1)
Expect(success).To(BeTrue())
Expect(err).ToNot(HaveOccurred())
// authority switches from singlesig back to original multisig
authorityAddress, newAuthorityAddress = newAuthorityAddress, authorityAddress
_, err = emcli.CustomCommand(
"tx", "authority", "replace", authorityAddress,
newAuthorityAddress, "--from", authorityAddress,
)
Expect(err).To(BeNil())
// current multisig authority now
// create-issuer
execAuthMSigTx(
newAuthorityAddress, []nt.Key{key1, key2}, "tx", "authority",
"create-issuer", newAuthorityAddress,
key1.GetAddress(), "-d", "eeur", "-d", "ejpy", "--generate-only", "--from",
newAuthorityAddress, "--fees", "1.0ungm",
)
// destroy-issuer with current multisig authority
execAuthMSigTx(
newAuthorityAddress, []nt.Key{key1, key2}, "tx", "authority",
"destroy-issuer", newAuthorityAddress,
key1.GetAddress(), "--generate-only", "--from",
newAuthorityAddress, "--fees", "1.0ungm",
)
// try with a non-authority account
ok = nt.AuthCreatesIssuer(emcli, keystore.Key2, key1)
Expect(ok).To(BeFalse())
})
It("set global gas prices", func() {
// former authority is still in effect
authorityAddress := sdk.AccAddress(Authority.GetPublicKey().Address()).String()
newMinGasPrices, _ := sdk.ParseDecCoins("0.0006eeur")
execAuthMSigTx(authorityAddress, []nt.Key{key1, key2}, "tx", "authority", "set-gas-prices", authorityAddress,
newMinGasPrices.String(), "--generate-only", "--from", authorityAddress)
bz, err := emcli.QueryMinGasPrices()
Expect(err).To(BeNil())
jsonGP := gjson.GetBytes(bz, "min_gas_prices")
Expect(jsonGP.IsArray()).To(BeTrue())
Expect(jsonGP.Array()).To(HaveLen(1))
jsonGP = jsonGP.Get("0")
Expect(jsonGP.Get("denom").Str).To(Equal("eeur"))
amount, err := sdk.NewDecFromStr(jsonGP.Get("amount").Str)
Expect(err).To(BeNil())
Expect(amount).To(Equal(sdk.NewDecWithPrec(6, 4)))
})
})
})
// execAuthMSigTx signs and broadcasts a multisig trx using the emcli.CustomCommand.
func execAuthMSigTx(authorityAddress string, keys []nt.Key, cmdArgs ...string) {
emcli := testnet.NewEmcli()
jsonPath, err := ioutil.TempDir("", "")
Expect(err).To(BeNil())
defer os.RemoveAll(jsonPath)
cmdTx, err := emcli.CustomCommand(cmdArgs...)
Expect(err).To(BeNil())
transactionPath := fmt.Sprintf("%v/transaction.json", jsonPath)
err = ioutil.WriteFile(transactionPath, []byte(cmdTx), 0o777)
Expect(err).To(BeNil())
tx, err := emcli.SignTranscation(
transactionPath, keys[0].GetAddress(), authorityAddress,
)
signaturePath1 := fmt.Sprintf("%v/sign%v.json", jsonPath, 0)
err = ioutil.WriteFile(signaturePath1, []byte(tx), 0o777)
Expect(err).To(BeNil())
tx, err = emcli.SignTranscation(
transactionPath, keys[1].GetAddress(), authorityAddress,
)
signaturePath2 := fmt.Sprintf("%v/sign%v.json", jsonPath, 1)
err = ioutil.WriteFile(signaturePath2, []byte(tx), 0o777)
Expect(err).To(BeNil())
// Combine the two signatures
tx, err = emcli.CustomCommand(
"tx", "multisign", transactionPath, "multikey", signaturePath1,
signaturePath2,
)
Expect(err).To(BeNil())
err = ioutil.WriteFile(transactionPath, []byte(tx), 0o777)
Expect(err).To(BeNil())
tx, err = emcli.CustomCommand("tx", "broadcast", transactionPath)
Expect(err).To(BeNil())
Expect(gjson.Parse(tx).Get("logs.0.success").Exists()).To(Equal(false))
}