-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathend-to-end.test.ts
211 lines (180 loc) · 6.26 KB
/
end-to-end.test.ts
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import test from 'tape'
import { readFileSync } from 'fs'
import Entropy, { wasmGlobalsReady } from '../src'
import Keyring from '../src/keys'
import * as util from '@polkadot/util'
import {
promiseRunner,
spinNetworkUp,
jumpStartNetwork,
eveSeed,
eveAddress,
spinNetworkDown,
} from './testing-utils'
import { ProgramInstance } from '../src/programs'
import { MsgParams } from '../src/signing'
const networkType = 'four-nodes'
const msg = Buffer.from('Hello world: signature from entropy!').toString('hex')
test('End To End', async (t) => {
const run = promiseRunner(t)
await run('network up', spinNetworkUp(networkType))
t.teardown(async () => {
await entropy.close()
await spinNetworkDown(networkType).catch((error) =>
console.error('Error while spinning network down', error.message)
)
})
await run('wasm', wasmGlobalsReady())
const keyring = new Keyring({ seed: eveSeed, debug: true })
let store = keyring.getAccount()
t.equal(store.admin.address, keyring.accounts.registration.pair.address, 'admin account should have an address and for now it should match registrations address')
keyring.accounts.on('account-update', (fullAccount) => {
store = fullAccount
})
t.equal(
keyring.accounts.registration.address,
eveAddress,
'got right address'
)
const entropy = new Entropy({
keyring,
endpoint: 'ws://127.0.0.1:9944',
})
await run(
'entropy ready',
entropy.ready
)
await run('jump-start network', jumpStartNetwork(entropy))
/* deploy */
// const bareBones: any = readFileSync(
// './tests/testing-utils/template_barebones.wasm'
// )
// t.equal(typeof bareBones.toString(), 'string', 'got basic program')
// QUESTION: how to launch substrate node with a particular address pre-funded
// const pointer = await run(
// 'deploy program',
// entropy.programs.dev.deploy(bareBones)
// )
// t.equal(typeof pointer, 'string', 'valid pointer')
// register
let verifyingKeyFromRegistration
const emitterTest = run(
'keyring.accounts.once#account-update',
new Promise((res, reject) => {
// TODO remove event listener if it hangs
keyring.accounts.on('account-update', (fullAccountView) => {
if (!verifyingKeyFromRegistration) return
if (!fullAccountView.admin) reject('no admin account')
if (!fullAccountView.registration) reject('no registration account')
if (!fullAccountView.deviceKey) reject('no deviceKey account')
if (!fullAccountView.registration.verifyingKeys)
reject('no registration.verifyingKeys ')
if (!fullAccountView.registration.verifyingKeys[0])
reject(
'no registration.verifyingKeys[0] this means their were no keys added'
)
const last = fullAccountView.registration.verifyingKeys.pop()
if (!last === verifyingKeyFromRegistration)
reject('verifyingKey returned in registration does not match')
if (!last === fullAccountView.deviceKey.verifyingKeys[0])
reject(
'verifyingKey on registration does not match device keys verifyingKeys[0]'
)
return res(fullAccountView)
})
})
)
verifyingKeyFromRegistration = await run('register', entropy.register())
t.equal(
verifyingKeyFromRegistration,
entropy.keyring.accounts.registration.verifyingKeys[0],
'verifyingKeys match after registration'
)
// tests the once function as well as what is
// considered to be necessary changes to the full account after emitting
await emitterTest
//
// sign some data
//
// NEED PRE-REGISTRATION TEST
// const preRegistrationStatusCheck = await run(
// 'checkRegistrationStatus',
// entropy.substrate.query.registry.registered(verifyingKey)
// // entropy.registrationManager.checkRegistrationStatus(eveAddress)
// )
// t.ok(preRegistrationStatusCheck, 'preRegistrationStatusCheck ...') // TODO: better check
// Use the verifyingKey from ProgramManager
const verifyingKey = entropy.programs.verifyingKey
t.ok(verifyingKey, 'verifyingKey exists')
const registrationStatus = await run(
'check registration',
entropy.substrate.query.registry.registered(verifyingKey)
)
t.ok(registrationStatus, 'Verifying key is registered')
// loading second program
const noopProgram: any = readFileSync(
'./tests/testing-utils/program_noop.wasm'
)
const newPointer = await run(
'deploy',
entropy.programs.dev.deploy(noopProgram)
)
const noopProgramInstance: ProgramInstance = {
program_pointer: newPointer,
program_config: '',
}
console.debug('verifyingKey', verifyingKey)
const programsBeforeAdd = await run(
'get programs',
entropy.programs.get(verifyingKey)
)
t.equal(
programsBeforeAdd.length,
1,
'eve has 1 programs' + JSON.stringify(programsBeforeAdd)
)
await run('add program', entropy.programs.add(noopProgramInstance))
// getting eve programs
const programsAfterAdd = await run(
'get programs',
entropy.programs.get(verifyingKey)
)
t.equal(programsAfterAdd.length, 2, 'eve has 2 programs')
const msgParam: MsgParams = { msg }
const signatureDataFromAdapter = await run(
'signWithAdaptersInOrder',
entropy.signWithAdaptersInOrder({
msg: msgParam,
order: ['deviceKeyProxy', 'noop'],
})
)
t.equal(
signatureDataFromAdapter.signature.length,
132,
'got a good sig from adapter'
)
// removing deviceKey
const deviceKeyProxyPointer =
'0x0000000000000000000000000000000000000000000000000000000000000000'
await run(
'remove DeviceKeyProxy program',
entropy.programs.remove(deviceKeyProxyPointer, verifyingKey)
)
const programsAftreRemoveDefault = await run(
'get programs',
entropy.programs.get(verifyingKey)
)
t.equal(programsAftreRemoveDefault.length, 1, 'eve has 1 program')
const signatureData = await run(
'sign',
entropy.sign({
hexMessage: msg,
hash: 'blake2_256',
})
)
const v1 = await run('verified signature blake2_256', entropy.verify(signatureData))
const v2 = await run(`verified signature ${signatureDataFromAdapter.hashType}`, entropy.verify(signatureDataFromAdapter))
t.ok(v1, 'should be valid blake2_256 signature')
t.ok(v2, `should be valid ${signatureDataFromAdapter.hashType} signature`)
t.end()
})