-
Notifications
You must be signed in to change notification settings - Fork 88
/
identity.test.ts
300 lines (266 loc) · 10.2 KB
/
identity.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
* @jest-environment node
*/
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Actor, HttpAgent, SignIdentity } from '@dfinity/agent';
import { IDL } from '@dfinity/candid';
import { Principal } from '@dfinity/principal';
import {
DelegationChain,
DelegationIdentity,
Ed25519KeyIdentity,
ECDSAKeyIdentity,
} from '@dfinity/identity';
import { Secp256k1KeyIdentity } from '@dfinity/identity-secp256k1';
import agent from '../utils/agent';
import identityCanister from '../canisters/identity';
function createIdentity(seed: number): SignIdentity {
const seed1 = new Array(32).fill(0);
seed1[0] = seed;
return Ed25519KeyIdentity.generate(new Uint8Array(seed1));
}
function createSecpIdentity(seed: number): SignIdentity {
const seed1 = new Array(32).fill(0);
seed1[0] = seed;
return Secp256k1KeyIdentity.generate(new Uint8Array(seed1));
}
async function createIdentityActor(
seed: number,
canisterId: Principal,
idl: IDL.InterfaceFactory,
): Promise<any> {
const identity = createIdentity(seed);
const agent1 = new HttpAgent({ source: await agent, identity });
return Actor.createActor(idl, {
canisterId,
agent: agent1,
}) as any;
}
async function createSecp256k1IdentityActor(
canisterId: Principal,
idl: IDL.InterfaceFactory,
seed?: number,
): Promise<any> {
let seed1: Uint8Array | undefined;
if (seed) {
seed1 = new Uint8Array(new Array(32).fill(0));
seed1[0] = seed;
}
const identity = Secp256k1KeyIdentity.generate(seed1);
const agent1 = new HttpAgent({ source: await agent, identity });
return Actor.createActor(idl, {
canisterId,
agent: agent1,
}) as any;
}
async function createEcdsaIdentityActor(
canisterId: Principal,
idl: IDL.InterfaceFactory,
identity?: SignIdentity,
): Promise<any> {
let effectiveIdentity: SignIdentity;
if (identity) {
effectiveIdentity = identity;
} else {
effectiveIdentity = await ECDSAKeyIdentity.generate();
}
const agent1 = new HttpAgent({ source: await agent, identity: effectiveIdentity });
return Actor.createActor(idl, {
canisterId,
agent: agent1,
}) as any;
}
async function installIdentityCanister(): Promise<{
canisterId: Principal;
idl: IDL.InterfaceFactory;
}> {
const { canisterId, idl } = await identityCanister();
return {
canisterId,
idl,
};
}
jest.setTimeout(30000);
test('identity: query and call gives same principal', async () => {
const { canisterId, idl } = await installIdentityCanister();
const identity = Actor.createActor(idl, {
canisterId,
agent: await agent,
}) as any;
const callPrincipal = await identity.whoami();
const queryPrincipal = await identity.whoami_query();
expect(callPrincipal).toEqual(queryPrincipal);
});
jest.setTimeout(30000);
test('identity: two different Ed25519 keys should have a different principal', async () => {
const { canisterId, idl } = await installIdentityCanister();
const identity1 = await createIdentityActor(0, canisterId, idl);
const identity2 = await createIdentityActor(1, canisterId, idl);
const principal1 = await identity1.whoami_query();
const principal2 = await identity2.whoami_query();
expect(principal1).not.toEqual(principal2);
});
jest.setTimeout(30000);
test('identity: two different Secp256k1 keys should have a different principal', async () => {
const { canisterId, idl } = await installIdentityCanister();
// Seeded identity
const identity1 = await createSecp256k1IdentityActor(canisterId, idl, 0);
// Unseeded identity
const identity2 = await createSecp256k1IdentityActor(canisterId, idl);
const principal1 = await identity1.whoami_query();
const principal2 = await identity2.whoami_query();
expect(principal1).not.toEqual(principal2);
});
jest.setTimeout(30000);
test('identity: two different Ecdsa keys should have a different principal', async () => {
const { canisterId, idl } = await installIdentityCanister();
const identity1 = await createEcdsaIdentityActor(canisterId, idl);
const identity2 = await createEcdsaIdentityActor(canisterId, idl);
const principal1 = await identity1.whoami_query();
const principal2 = await identity2.whoami_query();
expect(principal1).not.toEqual(principal2);
});
jest.setTimeout(30000);
test('delegation: principal is the same between delegated keys with secp256k1', async () => {
const { canisterId, idl } = await installIdentityCanister();
const masterKey = createSecpIdentity(2);
const sessionKey = createSecpIdentity(3);
const delegation = await DelegationChain.create(masterKey, sessionKey.getPublicKey());
const id3 = DelegationIdentity.fromDelegation(sessionKey, delegation);
const identityActor1 = Actor.createActor(idl, {
canisterId,
agent: new HttpAgent({ source: await agent, identity: masterKey }),
}) as any;
const identityActor2 = Actor.createActor(idl, {
canisterId,
agent: new HttpAgent({ source: await agent, identity: sessionKey }),
}) as any;
const identityActor3 = Actor.createActor(idl, {
canisterId,
agent: new HttpAgent({ source: await agent, identity: id3 }),
}) as any;
const principal1 = await identityActor1.whoami_query();
const principal2 = await identityActor2.whoami_query();
const principal3 = await identityActor3.whoami_query();
expect(principal1).not.toEqual(principal2);
expect(principal1).toEqual(principal3);
expect(principal2).not.toEqual(principal3);
});
jest.setTimeout(30000);
test('delegation: principal is the same between delegated keys', async () => {
const { canisterId, idl } = await installIdentityCanister();
const masterKey = createIdentity(2);
const sessionKey = createIdentity(3);
const delegation = await DelegationChain.create(masterKey, sessionKey.getPublicKey());
const id3 = DelegationIdentity.fromDelegation(sessionKey, delegation);
const identityActor1 = Actor.createActor(idl, {
canisterId,
agent: new HttpAgent({ source: await agent, identity: masterKey }),
}) as any;
const identityActor2 = Actor.createActor(idl, {
canisterId,
agent: new HttpAgent({ source: await agent, identity: sessionKey }),
}) as any;
const identityActor3 = Actor.createActor(idl, {
canisterId,
agent: new HttpAgent({ source: await agent, identity: id3 }),
}) as any;
const principal1 = await identityActor1.whoami_query();
const principal2 = await identityActor2.whoami_query();
const principal3 = await identityActor3.whoami_query();
expect(principal1).not.toEqual(principal2);
expect(principal1).toEqual(principal3);
expect(principal2).not.toEqual(principal3);
});
jest.setTimeout(30000);
test('delegation: works with 3 keys', async () => {
const { canisterId, idl } = await installIdentityCanister();
const rootKey = createIdentity(4);
const middleKey = createIdentity(5);
const bottomKey = createIdentity(6);
const id1D2 = await DelegationChain.create(rootKey, middleKey.getPublicKey());
const idDelegated = DelegationIdentity.fromDelegation(
bottomKey,
await DelegationChain.create(middleKey, bottomKey.getPublicKey(), undefined, {
previous: id1D2,
}),
);
const identityActorBottom = Actor.createActor(idl, {
canisterId,
agent: new HttpAgent({ source: await agent, identity: bottomKey }),
}) as any;
const identityActorMiddle = Actor.createActor(idl, {
canisterId,
agent: new HttpAgent({ source: await agent, identity: middleKey }),
}) as any;
const identityActorRoot = Actor.createActor(idl, {
canisterId,
agent: new HttpAgent({ source: await agent, identity: rootKey }),
}) as any;
const identityActorDelegated = Actor.createActor(idl, {
canisterId,
agent: new HttpAgent({ source: await agent, identity: idDelegated }),
}) as any;
const principalBottom = await identityActorBottom.whoami_query();
const principalMiddle = await identityActorMiddle.whoami_query();
const principalRoot = await identityActorRoot.whoami_query();
const principalDelegated = await identityActorDelegated.whoami_query();
expect(principalBottom).not.toEqual(principalMiddle);
expect(principalMiddle).not.toEqual(principalRoot);
expect(principalBottom).not.toEqual(principalRoot);
expect(principalRoot).toEqual(principalDelegated);
});
jest.setTimeout(30000);
test('delegation: works with 4 keys', async () => {
const { canisterId, idl } = await installIdentityCanister();
const rootKey = createIdentity(7);
const middleKey = createIdentity(8);
const middle2Key = createIdentity(9);
const bottomKey = createIdentity(10);
const rootToMiddle = await DelegationChain.create(rootKey, middleKey.getPublicKey());
const middleToMiddle2 = await DelegationChain.create(
middleKey,
middle2Key.getPublicKey(),
undefined,
{
previous: rootToMiddle,
},
);
const idDelegated = DelegationIdentity.fromDelegation(
bottomKey,
await DelegationChain.create(middle2Key, bottomKey.getPublicKey(), undefined, {
previous: middleToMiddle2,
}),
);
const identityActorBottom = Actor.createActor(idl, {
canisterId,
agent: new HttpAgent({ source: await agent, identity: bottomKey }),
}) as any;
const identityActorMiddle = Actor.createActor(idl, {
canisterId,
agent: new HttpAgent({ source: await agent, identity: middleKey }),
}) as any;
const identityActorMiddle2 = Actor.createActor(idl, {
canisterId,
agent: new HttpAgent({ source: await agent, identity: middle2Key }),
}) as any;
const identityActorRoot = Actor.createActor(idl, {
canisterId,
agent: new HttpAgent({ source: await agent, identity: rootKey }),
}) as any;
const identityActorDelegated = Actor.createActor(idl, {
canisterId,
agent: new HttpAgent({ source: await agent, identity: idDelegated }),
}) as any;
const principalBottom = await identityActorBottom.whoami_query();
const principalMiddle = await identityActorMiddle.whoami_query();
const principalMiddle2 = await identityActorMiddle2.whoami_query();
const principalRoot = await identityActorRoot.whoami_query();
const principalDelegated = await identityActorDelegated.whoami_query();
expect(principalBottom).not.toEqual(principalMiddle);
expect(principalMiddle).not.toEqual(principalRoot);
expect(principalMiddle2).not.toEqual(principalRoot);
expect(principalBottom).not.toEqual(principalRoot);
expect(principalBottom).not.toEqual(principalMiddle2);
expect(principalRoot).toEqual(principalDelegated);
});