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

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.
@@ -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.7",
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/"
@@ -42,6 +42,7 @@
42
42
  "test": "vitest run",
43
43
  "lint": "eslint --max-warnings 0",
44
44
  "format": "prettier --write \"**/*.{ts,js,json,yaml,yml}\"",
45
+ "format:check": "prettier --check \"**/*.{ts,js,json,yaml,yml}\"",
45
46
  "dist": "tsc -b ./tsconfig.build.json",
46
47
  "dist:publish": "tsc -b ./tsconfig.publish.json",
47
48
  "clean": "rimraf --glob dist 'tsconfig.*.tsbuildinfo' && date +%s > .clean-timestamp",