Skip to content

Commit a73381a

Browse files
authored
feat: CRP-2957 Add PocketIC master public keys to TS ic_vetkeys library (#251)
1 parent 942afb2 commit a73381a

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

frontend/ic_vetkeys/src/utils/utils.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
IbeCiphertext,
66
MasterPublicKey,
77
MasterPublicKeyId,
8+
PocketIcMasterPublicKeyId,
89
IbeSeed,
910
TransportSecretKey,
1011
VetKey,
@@ -122,6 +123,64 @@ test("MasterPublicKey derivation using prod key", () => {
122123
);
123124
});
124125

126+
test("MasterPublicKey derivation using PocketIC key_1", () => {
127+
const canisterId = hexToBytes("ffffffffff9000030101");
128+
129+
const masterKey = MasterPublicKey.pocketicKey(
130+
PocketIcMasterPublicKeyId.KEY_1,
131+
);
132+
const canisterKey = masterKey.deriveCanisterKey(canisterId);
133+
134+
const derivedKey = canisterKey.deriveSubKey(
135+
new TextEncoder().encode("Test Derivation For PocketIC VetKD key_1"),
136+
);
137+
138+
assertEqual(
139+
bytesToHex(derivedKey.publicKeyBytes()),
140+
"899a951f6ec2f9a96759c554a6cb01fb1cb20b2f2f96a2d2c869221c04d3349c3be8d49c3257312aed031f430f15f7ef0f4d43adf11251015d70dd91ac07df50fb70818ece721a1d6a314204acddde55542902f5d0d95e2406a5ab1fad18349d",
141+
);
142+
});
143+
144+
test("MasterPublicKey derivation using PocketIC test_key_1", () => {
145+
const canisterId = hexToBytes("ffffffffff9000030101");
146+
147+
const masterKey = MasterPublicKey.pocketicKey(
148+
PocketIcMasterPublicKeyId.TEST_KEY_1,
149+
);
150+
const canisterKey = masterKey.deriveCanisterKey(canisterId);
151+
152+
const derivedKey = canisterKey.deriveSubKey(
153+
new TextEncoder().encode(
154+
"Test Derivation For PocketIC VetKD test_key_1",
155+
),
156+
);
157+
158+
assertEqual(
159+
bytesToHex(derivedKey.publicKeyBytes()),
160+
"a60993fc46593728bd9b0a4ffb1fb9a662dd89b29c99fde36e403c311c8992e6eeb097b31174dd43f74e73fe10c190271193a4345490f64a41ce778a2f6e7c16804919e843ac72ff65bab959c53fa839c9fb3cb263e41498d17fb82704fe18bc",
161+
);
162+
});
163+
164+
test("MasterPublicKey derivation using PocketIC dfx_test_key", () => {
165+
const canisterId = hexToBytes("ffffffffff9000030101");
166+
167+
const masterKey = MasterPublicKey.pocketicKey(
168+
PocketIcMasterPublicKeyId.DFX_TEST_KEY,
169+
);
170+
const canisterKey = masterKey.deriveCanisterKey(canisterId);
171+
172+
const derivedKey = canisterKey.deriveSubKey(
173+
new TextEncoder().encode(
174+
"Test Derivation For PocketIC VetKD dfx_test_key",
175+
),
176+
);
177+
178+
assertEqual(
179+
bytesToHex(derivedKey.publicKeyBytes()),
180+
"800424bea66b95b715f86a9bed06b1f60df98206a57235c3e0f2da4d485dc1c93c56eef54155d559ef45c757fb0444920620b932652f1d683fdbc57db98b5aeb8ba664a5e040cbdf4d685e4e236a7193d1bd5b0927204fab05fff4f61f26b358",
181+
);
182+
});
183+
125184
test("DerivedPublicKey subderivation", () => {
126185
const canisterKey = DerivedPublicKey.deserialize(
127186
hexToBytes(

frontend/ic_vetkeys/src/utils/utils.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,15 @@ export enum MasterPublicKeyId {
120120
TEST_KEY_1 = "test_key_1",
121121
}
122122

123+
/**
124+
* Enumeration identifying possible PocketIC test keys
125+
*/
126+
export enum PocketIcMasterPublicKeyId {
127+
KEY_1 = "key_1",
128+
TEST_KEY_1 = "test_key_1",
129+
DFX_TEST_KEY = "dfx_test_key",
130+
}
131+
123132
/**
124133
* @internal helper to perform hex decoding
125134
*/
@@ -208,6 +217,39 @@ export class MasterPublicKey {
208217
}
209218
}
210219

220+
/**
221+
* Return the hardcoded master public key used in PocketIC
222+
*
223+
* This allows performing public key derivation offline
224+
*/
225+
static pocketicKey(
226+
keyId: PocketIcMasterPublicKeyId = PocketIcMasterPublicKeyId.KEY_1,
227+
): MasterPublicKey {
228+
if (keyId == PocketIcMasterPublicKeyId.KEY_1) {
229+
return MasterPublicKey.deserialize(
230+
hexToBytes(
231+
"8c800b5cff00463d26e8167369168827f1e48f4d8d60f71dd6a295580f65275b5f5f8e6a792c876b2c72492136530d0710a27522ee63977a76216c3cef9e70bfcb45b88736fc62142e7e0737848ce06cbb1f45a4a6a349b142ae5cf7853561e0",
232+
),
233+
);
234+
} else if (keyId == PocketIcMasterPublicKeyId.TEST_KEY_1) {
235+
return MasterPublicKey.deserialize(
236+
hexToBytes(
237+
"9069b82c7aae418cef27678291e7f2cb1a008a500eceba7199bffca12421b07c158987c6a22618af3d1958738b2835691028801f7663d311799733286c557c8979184bb62cb559a4d582fca7d2e48b860f08ed6641aef66a059ec891889a6218",
238+
),
239+
);
240+
} else if (keyId == PocketIcMasterPublicKeyId.DFX_TEST_KEY) {
241+
return MasterPublicKey.deserialize(
242+
hexToBytes(
243+
"b181c14cf9d04ba45d782c0067a44b0aaa9fc2acf94f1a875f0dae801af4f80339a7e6bf8b09fcf993824c8df3080b3f1409b688ca08cbd44d2cb28db9899f4aa3b5f06b9174240448e10be2f01f9f80079ea5431ce2d11d1c8d1c775333315f",
244+
),
245+
);
246+
} else {
247+
throw new Error(
248+
"Unknown PocketIcMasterPublicKeyId value for pocketicKey",
249+
);
250+
}
251+
}
252+
211253
/**
212254
* @internal constructor
213255
*/

0 commit comments

Comments
 (0)