@zerc20/storage-sdk 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/viewKey.d.ts +3 -3
- package/dist/viewKey.js +10 -8
- package/dist/viewKey.js.map +1 -1
- package/package.json +1 -1
- package/src/viewKey.ts +10 -8
package/dist/viewKey.d.ts
CHANGED
|
@@ -9,9 +9,9 @@ export interface ViewKey {
|
|
|
9
9
|
address: Uint8Array;
|
|
10
10
|
seed: Uint8Array;
|
|
11
11
|
}
|
|
12
|
-
/** `address`'s view key
|
|
13
|
-
* canonical `personal_sign` signature). */
|
|
14
|
-
export declare function viewKey(rootSeed: Uint8Array, address: Uint8Array): ViewKey;
|
|
12
|
+
/** `address`'s view key in `canister`, from the root seed of the address's zERC20 identity (in zERC20-v2, `keccak256`
|
|
13
|
+
* of a canonical `personal_sign` signature). */
|
|
14
|
+
export declare function viewKey(rootSeed: Uint8Array, address: Uint8Array, canister: Principal): ViewKey;
|
|
15
15
|
/** The X25519 secret that opens announcements to `view`'s address inbox. */
|
|
16
16
|
export declare const notifySecret: (view: ViewKey) => Uint8Array;
|
|
17
17
|
/** The key payers encrypt announcements to, which names `view`'s registration. */
|
package/dist/viewKey.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
// View keys and registration.
|
|
2
2
|
//
|
|
3
3
|
// An address's view key derives from the root seed of the address's zERC20 identity, so restoring a wallet needs
|
|
4
|
-
// nothing from the canister.
|
|
5
|
-
//
|
|
6
|
-
//
|
|
4
|
+
// nothing from the canister. It derives from the canister's id too: a testnet and mainnet share root seeds, and a key
|
|
5
|
+
// escrowed in one canister, which that canister's auditor, controllers and vetKD key can open, must open nothing in
|
|
6
|
+
// another. Registering (one wallet signature) publishes what payers need: the address's zERC20 public key and the key
|
|
7
|
+
// payers encrypt channel announcements to. It also escrows the view key to the auditor, whom vetKD alone can give the
|
|
8
|
+
// key to it. A later registration, from another root seed, replaces it.
|
|
7
9
|
import { EncryptedVetKey, IbeCiphertext, IbeIdentity, IbeSeed, } from '@dfinity/vetkeys';
|
|
8
10
|
import { x25519 } from '@noble/curves/ed25519.js';
|
|
9
11
|
import { secp256k1 } from '@noble/curves/secp256k1.js';
|
|
@@ -11,12 +13,12 @@ import { keccak_256 } from '@noble/hashes/sha3.js';
|
|
|
11
13
|
import { compare, concat, equal, kdf, nowNs, utf8 } from './crypto.js';
|
|
12
14
|
import { FR_BE, addressInbox } from './formats.js';
|
|
13
15
|
import { MAX_TTL_NS, registerMessage, vetkdInput } from './wire.js';
|
|
14
|
-
/** `address`'s view key
|
|
15
|
-
* canonical `personal_sign` signature). */
|
|
16
|
-
export function viewKey(rootSeed, address) {
|
|
16
|
+
/** `address`'s view key in `canister`, from the root seed of the address's zERC20 identity (in zERC20-v2, `keccak256`
|
|
17
|
+
* of a canonical `personal_sign` signature). */
|
|
18
|
+
export function viewKey(rootSeed, address, canister) {
|
|
17
19
|
if (rootSeed.length !== 32)
|
|
18
20
|
throw new Error('the root seed must be 32 bytes');
|
|
19
|
-
return { address, seed: kdf(rootSeed, 'view', [address]) };
|
|
21
|
+
return { address, seed: kdf(rootSeed, 'view', [address, canister.toUint8Array()]) };
|
|
20
22
|
}
|
|
21
23
|
/** The X25519 secret that opens announcements to `view`'s address inbox. */
|
|
22
24
|
export const notifySecret = (view) => kdf(view.seed, 'notify');
|
|
@@ -27,7 +29,7 @@ export const notifyKey = (view) => x25519.getPublicKey(notifySecret(view));
|
|
|
27
29
|
* before registering (one wallet signature) or throwing. Throws if the address is registered with another root seed;
|
|
28
30
|
* `register` to take it over. */
|
|
29
31
|
export async function registeredViewKey(client, rootSeed, signer, address, publicKey) {
|
|
30
|
-
const view = viewKey(rootSeed, address);
|
|
32
|
+
const view = viewKey(rootSeed, address, client.canisterId);
|
|
31
33
|
const ours = (r) => r && equal(r.notify_key, notifyKey(view)) && equal(r.public_key, publicKey);
|
|
32
34
|
if (ours(await client.registration(address)))
|
|
33
35
|
return view;
|
package/dist/viewKey.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"viewKey.js","sourceRoot":"","sources":["../src/viewKey.ts"],"names":[],"mappings":"AAAA,8BAA8B;AAC9B,EAAE;AACF,iHAAiH;AACjH,
|
|
1
|
+
{"version":3,"file":"viewKey.js","sourceRoot":"","sources":["../src/viewKey.ts"],"names":[],"mappings":"AAAA,8BAA8B;AAC9B,EAAE;AACF,iHAAiH;AACjH,sHAAsH;AACtH,oHAAoH;AACpH,sHAAsH;AACtH,sHAAsH;AACtH,wEAAwE;AAExE,OAAO,EAEL,eAAe,EACf,aAAa,EACb,WAAW,EACX,OAAO,GAER,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAC;AACvD,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAInD,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AACvE,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAWpE;gDACgD;AAChD,MAAM,UAAU,OAAO,CAAC,QAAoB,EAAE,OAAmB,EAAE,QAAmB;IACpF,IAAI,QAAQ,CAAC,MAAM,KAAK,EAAE;QAAE,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;IAC9E,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,CAAC,OAAO,EAAE,QAAQ,CAAC,YAAY,EAAE,CAAC,CAAC,EAAE,CAAC;AACtF,CAAC;AAED,4EAA4E;AAC5E,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,IA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AO,EAAE,CAAC,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,UAAU,EAAE,KAAK,CAAC,IAAI,CAAC;QAAE,OAAO,IAAI,CAAC;IAC/G,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,CAAC,CAAC,UAAU,EAAE,SAAS,EAAE,CAAC,CAAC,UAAU,EAAE,KAAK,EAAE,YAAY,CAAC,OAAO,EAAE,CAAC,CAAC,UAAU,CAAC,EAAE,CAAC;AACnH,CAAC;AAED;qEACqE;AACrE,MAAM,UAAU,UAAU,CACxB,SAAqB,EACrB,YAAgC,EAChC,eAAiC,EACjC,YAA0B;IAE1B,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,GAAG,YAAY,CAAC;IAC7C,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,eAAe,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC,gBAAgB,CACpE,YAAY,EACZ,eAAe,EACf,UAAU,CAAC,OAAO,EAAE,UAAU,CAAC,CAChC,CAAC;QACF,MAAM,IAAI,GAAG,aAAa,CAAC,WAAW,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QAC5E,OAAO,IAAI,CAAC,MAAM,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;IACvD,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
package/src/viewKey.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
// View keys and registration.
|
|
2
2
|
//
|
|
3
3
|
// An address's view key derives from the root seed of the address's zERC20 identity, so restoring a wallet needs
|
|
4
|
-
// nothing from the canister.
|
|
5
|
-
//
|
|
6
|
-
//
|
|
4
|
+
// nothing from the canister. It derives from the canister's id too: a testnet and mainnet share root seeds, and a key
|
|
5
|
+
// escrowed in one canister, which that canister's auditor, controllers and vetKD key can open, must open nothing in
|
|
6
|
+
// another. Registering (one wallet signature) publishes what payers need: the address's zERC20 public key and the key
|
|
7
|
+
// payers encrypt channel announcements to. It also escrows the view key to the auditor, whom vetKD alone can give the
|
|
8
|
+
// key to it. A later registration, from another root seed, replaces it.
|
|
7
9
|
|
|
8
10
|
import {
|
|
9
11
|
DerivedPublicKey,
|
|
@@ -32,11 +34,11 @@ export interface ViewKey {
|
|
|
32
34
|
seed: Uint8Array;
|
|
33
35
|
}
|
|
34
36
|
|
|
35
|
-
/** `address`'s view key
|
|
36
|
-
* canonical `personal_sign` signature). */
|
|
37
|
-
export function viewKey(rootSeed: Uint8Array, address: Uint8Array): ViewKey {
|
|
37
|
+
/** `address`'s view key in `canister`, from the root seed of the address's zERC20 identity (in zERC20-v2, `keccak256`
|
|
38
|
+
* of a canonical `personal_sign` signature). */
|
|
39
|
+
export function viewKey(rootSeed: Uint8Array, address: Uint8Array, canister: Principal): ViewKey {
|
|
38
40
|
if (rootSeed.length !== 32) throw new Error('the root seed must be 32 bytes');
|
|
39
|
-
return { address, seed: kdf(rootSeed, 'view', [address]) };
|
|
41
|
+
return { address, seed: kdf(rootSeed, 'view', [address, canister.toUint8Array()]) };
|
|
40
42
|
}
|
|
41
43
|
|
|
42
44
|
/** The X25519 secret that opens announcements to `view`'s address inbox. */
|
|
@@ -56,7 +58,7 @@ export async function registeredViewKey(
|
|
|
56
58
|
address: Uint8Array,
|
|
57
59
|
publicKey: Uint8Array,
|
|
58
60
|
): Promise<ViewKey> {
|
|
59
|
-
const view = viewKey(rootSeed, address);
|
|
61
|
+
const view = viewKey(rootSeed, address, client.canisterId);
|
|
60
62
|
const ours = (r: Registration | null) => r && equal(r.notify_key, notifyKey(view)) && equal(r.public_key, publicKey);
|
|
61
63
|
if (ours(await client.registration(address))) return view;
|
|
62
64
|
const [r] = await client.replicated.registration(address, []);
|