@midnight-ntwrk/wallet-sdk-hd 3.0.0-beta.6 → 3.0.0-beta.8

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/README.md CHANGED
@@ -2,30 +2,34 @@
2
2
 
3
3
  This package provides support for Hierarchical Deterministic (HD) Wallet.
4
4
 
5
- To allow deterministic derivation of keys for different features, Midnight follows algorithms and structure being a mix of [BIP-32](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki), [BIP-44](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki) and [CIP-1852](https://github.com/cardano-foundation/CIPs/blob/master/CIP-1852/README.md). Specifically, derivation follows BIP-32, and following path for a key pair is used:
5
+ To allow deterministic derivation of keys for different features, Midnight follows algorithms and structure being a mix
6
+ of [BIP-32](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki),
7
+ [BIP-44](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki) and
8
+ [CIP-1852](https://github.com/cardano-foundation/CIPs/blob/master/CIP-1852/README.md). Specifically, derivation follows
9
+ BIP-32, and following path for a key pair is used:
6
10
 
7
11
  ```
8
12
  m / purpose' / coin_type' / account' / role / index
9
13
  ```
10
14
 
11
15
  Where:
16
+
12
17
  - `purpose` is integer `44` (`0x8000002c`) - despite extensions, Night part of the hierarchy follows BIP-44
13
18
  - `coin_type` is integer `2400` (`0x80000960`)
14
19
  - `account` follows BIP-44 recommendations
15
20
  - `role` follows table below
16
21
  - `index` follows BIP-44 recommendations
17
22
 
18
-
19
23
  | Role name | Value | Description |
20
- |----------------------|-------|---------------------------------------------------------|
24
+ | -------------------- | ----- | ------------------------------------------------------- |
21
25
  | Night External chain | 0 | Night is Midnight's main token of value, Follows BIP-44 |
22
26
  | Night Internal chain | 1 | as above |
23
27
  | Dust | 2 | Dust is needed to pay fees on Midnight |
24
28
  | Zswap | 3 | Zswap is a sub-protocol for shielded native tokens |
25
29
  | Metadata | 4 | Keys for signing metadata |
26
30
 
27
-
28
31
  ## How to derive the keys?
32
+
29
33
  Below you can find an example on how to derive keys from a random seed:
30
34
 
31
35
  ```typescript
@@ -9,35 +9,60 @@ export declare const Roles: {
9
9
  };
10
10
  export type Role = ValueOf<typeof Roles>;
11
11
  type DerivationResult = {
12
- type: 'keyDerived';
13
- key: Uint8Array;
12
+ readonly type: 'keyDerived';
13
+ readonly key: Uint8Array;
14
14
  } | {
15
- type: 'keyOutOfBounds';
15
+ readonly type: 'keyOutOfBounds';
16
+ };
17
+ type CompositeDerivationResult<T extends readonly Role[]> = {
18
+ readonly type: 'keysDerived';
19
+ readonly keys: Record<T[number], Uint8Array>;
20
+ } | {
21
+ readonly type: 'keyOutOfBounds';
22
+ readonly roles: readonly Role[];
16
23
  };
17
24
  type HDWalletResult = {
18
- type: 'seedOk';
19
- hdWallet: HDWallet;
25
+ readonly type: 'seedOk';
26
+ readonly hdWallet: HDWallet;
20
27
  } | {
21
- type: 'seedError';
22
- error: unknown;
28
+ readonly type: 'seedError';
29
+ readonly error: unknown;
30
+ };
31
+ declare const CompositeDerivationResult: {
32
+ fromResults: <T extends readonly Role[]>(results: {
33
+ role: T[number];
34
+ result: DerivationResult;
35
+ }[]) => CompositeDerivationResult<T>;
23
36
  };
24
37
  export declare class HDWallet {
25
38
  private readonly rootKey;
26
39
  private constructor();
27
40
  static fromSeed(seed: Uint8Array): HDWalletResult;
28
41
  selectAccount(account: number): AccountKey;
42
+ /**
43
+ * Once all keys are derived - clear internals from private data, so that they do not reside in memory longer than needed.
44
+ */
45
+ clear(): void;
29
46
  }
30
47
  export declare class AccountKey {
31
- private rootKey;
32
- private account;
48
+ private readonly rootKey;
49
+ private readonly account;
33
50
  constructor(rootKey: HDKey, account: number);
34
51
  selectRole(role: Role): RoleKey;
52
+ selectRoles<T extends readonly Role[]>(roles: T): CompositeRoleKey<T>;
35
53
  }
36
54
  export declare class RoleKey {
37
- private rootKey;
38
- private account;
39
- private role;
55
+ private readonly rootKey;
56
+ private readonly account;
57
+ private readonly role;
40
58
  constructor(rootKey: HDKey, account: number, role: Role);
41
59
  deriveKeyAt(index: number): DerivationResult;
42
60
  }
61
+ export declare class CompositeRoleKey<T extends readonly Role[]> {
62
+ private readonly rootKey;
63
+ private readonly account;
64
+ private readonly roles;
65
+ constructor(rootKey: HDKey, account: number, roles: T);
66
+ deriveKeysAt(index: number): CompositeDerivationResult<T>;
67
+ }
43
68
  export {};
package/dist/HDWallet.js CHANGED
@@ -1,3 +1,15 @@
1
+ // This file is part of MIDNIGHT-WALLET-SDK.
2
+ // Copyright (C) 2025 Midnight Foundation
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // You may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ // Unless required by applicable law or agreed to in writing, software
9
+ // distributed under the License is distributed on an "AS IS" BASIS,
10
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ // See the License for the specific language governing permissions and
12
+ // limitations under the License.
1
13
  import { HDKey } from '@scure/bip32';
2
14
  export const Roles = {
3
15
  NightExternal: 0,
@@ -8,6 +20,25 @@ export const Roles = {
8
20
  };
9
21
  const PURPOSE = 44;
10
22
  const COIN_TYPE = 2400;
23
+ const CompositeDerivationResult = {
24
+ fromResults: (results) => {
25
+ const { succeededKeys, failedRoles } = results.reduce((acc, result) => {
26
+ if (result.result.type === 'keyDerived') {
27
+ acc.succeededKeys[result.role] = result.result.key;
28
+ }
29
+ else {
30
+ acc.failedRoles.push(result.role);
31
+ }
32
+ return acc;
33
+ }, { succeededKeys: {}, failedRoles: [] });
34
+ if (failedRoles.length > 0) {
35
+ return { type: 'keyOutOfBounds', roles: failedRoles };
36
+ }
37
+ else {
38
+ return { type: 'keysDerived', keys: succeededKeys };
39
+ }
40
+ },
41
+ };
11
42
  export class HDWallet {
12
43
  rootKey;
13
44
  constructor(key) {
@@ -26,6 +57,12 @@ export class HDWallet {
26
57
  selectAccount(account) {
27
58
  return new AccountKey(this.rootKey, account);
28
59
  }
60
+ /**
61
+ * Once all keys are derived - clear internals from private data, so that they do not reside in memory longer than needed.
62
+ */
63
+ clear() {
64
+ this.rootKey.wipePrivateData();
65
+ }
29
66
  }
30
67
  export class AccountKey {
31
68
  rootKey;
@@ -38,6 +75,9 @@ export class AccountKey {
38
75
  selectRole(role) {
39
76
  return new RoleKey(this.rootKey, this.account, role);
40
77
  }
78
+ selectRoles(roles) {
79
+ return new CompositeRoleKey(this.rootKey, this.account, roles);
80
+ }
41
81
  }
42
82
  export class RoleKey {
43
83
  rootKey;
@@ -55,3 +95,20 @@ export class RoleKey {
55
95
  return derivedKey.privateKey ? { type: 'keyDerived', key: derivedKey.privateKey } : { type: 'keyOutOfBounds' };
56
96
  }
57
97
  }
98
+ export class CompositeRoleKey {
99
+ rootKey;
100
+ account;
101
+ roles;
102
+ constructor(rootKey, account, roles) {
103
+ this.roles = roles;
104
+ this.rootKey = rootKey;
105
+ this.account = account;
106
+ }
107
+ deriveKeysAt(index) {
108
+ const results = this.roles.map((role) => ({
109
+ role,
110
+ result: new RoleKey(this.rootKey, this.account, role).deriveKeyAt(index),
111
+ }));
112
+ return CompositeDerivationResult.fromResults(results);
113
+ }
114
+ }
@@ -1,3 +1,15 @@
1
+ // This file is part of MIDNIGHT-WALLET-SDK.
2
+ // Copyright (C) 2025 Midnight Foundation
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // You may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ // Unless required by applicable law or agreed to in writing, software
9
+ // distributed under the License is distributed on an "AS IS" BASIS,
10
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ // See the License for the specific language governing permissions and
12
+ // limitations under the License.
1
13
  import * as bip39 from '@scure/bip39';
2
14
  import { wordlist as english } from '@scure/bip39/wordlists/english';
3
15
  export const mnemonicToWords = (mnemonic) => mnemonic.split(' ');
package/dist/index.js CHANGED
@@ -1,2 +1,14 @@
1
+ // This file is part of MIDNIGHT-WALLET-SDK.
2
+ // Copyright (C) 2025 Midnight Foundation
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // You may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ // Unless required by applicable law or agreed to in writing, software
9
+ // distributed under the License is distributed on an "AS IS" BASIS,
10
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ // See the License for the specific language governing permissions and
12
+ // limitations under the License.
1
13
  export * from './HDWallet.js';
2
14
  export * from './MnemonicUtils.js';
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@midnight-ntwrk/wallet-sdk-hd",
3
- "version": "3.0.0-beta.6",
3
+ "version": "3.0.0-beta.8",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
8
- "author": "IOHK",
8
+ "author": "Midnight Foundation",
9
9
  "license": "Apache-2.0",
10
10
  "publishConfig": {
11
11
  "registry": "https://npm.pkg.github.com/"
@@ -32,16 +32,18 @@
32
32
  "@ethereumjs/wallet": "^3.0.0-alpha.1",
33
33
  "eslint": "^9.37.0",
34
34
  "fast-check": "^4.2.0",
35
+ "prettier": "^3.7.0",
35
36
  "publint": "~0.3.14",
36
37
  "rimraf": "^6.0.1",
37
38
  "typescript": "^5.9.3",
38
- "vitest": "^3.2.4"
39
+ "vitest": "^4.0.16"
39
40
  },
40
41
  "scripts": {
41
42
  "typecheck": "tsc -b ./tsconfig.json --noEmit",
42
43
  "test": "vitest run",
43
44
  "lint": "eslint --max-warnings 0",
44
- "format": "prettier --write \"**/*.{ts,js,json,yaml,yml}\"",
45
+ "format": "prettier --write \"**/*.{ts,js,json,yaml,yml,md}\"",
46
+ "format:check": "prettier --check \"**/*.{ts,js,json,yaml,yml,md}\"",
45
47
  "dist": "tsc -b ./tsconfig.build.json",
46
48
  "dist:publish": "tsc -b ./tsconfig.publish.json",
47
49
  "clean": "rimraf --glob dist 'tsconfig.*.tsbuildinfo' && date +%s > .clean-timestamp",