@tetherto/wdk-wallet 1.0.0-beta.20 → 1.0.0-beta.21

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,7 +2,7 @@
2
2
 
3
3
  ## What This Does
4
4
 
5
- Base classes for the wallet and protocol modules in the [Wallet Development Kit (WDK)](https://docs.wallet.tether.io).
5
+ Base classes for the wallet and protocol modules in the [WDK (Wallet Development Kit)](https://docs.wdk.tether.io/) by Tether.
6
6
 
7
7
  ## Who Should Use This
8
8
 
@@ -32,7 +32,7 @@ This module is for internal use only. If you want to build a wallet, use one of
32
32
 
33
33
  ## Learn More
34
34
 
35
- For full docs, visit [docs.wallet.tether.io](https://docs.wallet.tether.io)
35
+ For general WDK documentation, visit [docs.wdk.tether.io](https://docs.wdk.tether.io/)
36
36
 
37
37
  ## License
38
38
 
package/index.js CHANGED
@@ -39,4 +39,6 @@ export { IWalletAccount } from './src/wallet-account.js'
39
39
 
40
40
  export { ISigner } from './src/signer.js'
41
41
 
42
+ export { IDisposable } from './src/disposable.js'
43
+
42
44
  export { AssertionError, InvalidTokenError, InvalidSignerError, InvalidSignerError as SignerError, MaximumFeeExceededError, NoSuchElementError, NotImplementedError, ProviderError, ProviderErrorReason, ProviderRequiredError, TimeoutError, TransactionError, TransactionErrorReason, TransferError, TransferErrorReason, UnsupportedOperationError, ValueError, WdkError } from './src/errors.js'
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "@tetherto/wdk-wallet",
3
- "version": "1.0.0-beta.20",
4
- "description": "A simple package to manage BIP-32 wallets.",
3
+ "version": "1.0.0-beta.21",
4
+ "description": "Base wallet and protocol classes for WDK (Wallet Development Kit) by Tether. For internal use only.",
5
+ "homepage": "https://docs.wdk.tether.io/",
5
6
  "keywords": [
6
7
  "wdk",
7
8
  "wallet",
@@ -0,0 +1,26 @@
1
+ // Copyright 2024 Tether Operations Limited
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ 'use strict'
15
+
16
+ import { NotImplementedError } from './errors.js'
17
+
18
+ /** @interface */
19
+ export class IDisposable {
20
+ /**
21
+ * Disposes the object along with all its data (cleaning up any sensitive field from memory).
22
+ */
23
+ dispose () {
24
+ throw new NotImplementedError('dispose()')
25
+ }
26
+ }
package/src/signer.js CHANGED
@@ -15,6 +15,9 @@
15
15
 
16
16
  import { NotImplementedError } from './errors.js'
17
17
 
18
+ import { IDisposable } from './disposable.js'
19
+
20
+ /** @typedef {import('./wallet-account.js').KeyPair} KeyPair */
18
21
  /** @typedef {import('./errors.js').UnsupportedOperationError} UnsupportedOperationError */
19
22
  /** @typedef {import('./errors.js').ValueError} ValueError */
20
23
 
@@ -23,7 +26,36 @@ import { NotImplementedError } from './errors.js'
23
26
  *
24
27
  * @interface
25
28
  */
26
- export class ISigner {
29
+ export class ISigner extends IDisposable {
30
+ /**
31
+ * Whether the signer supports account derivation via {@link derive}.
32
+ *
33
+ * @type {boolean}
34
+ */
35
+ get isDerivable () {
36
+ throw new NotImplementedError('isDerivable')
37
+ }
38
+
39
+ /**
40
+ * The signer's key pair, or null if the signer does not allow retrieving
41
+ * key material (e.g. hardware signers).
42
+ *
43
+ * @type {KeyPair | null}
44
+ */
45
+ get keyPair () {
46
+ throw new NotImplementedError('keyPair')
47
+ }
48
+
49
+ /**
50
+ * The BIP 0032 derivation path, or null if the signer is not bound to a derivation position
51
+ * (e.g. private-key signers).
52
+ *
53
+ * @type {string | null}
54
+ */
55
+ get path () {
56
+ throw new NotImplementedError('path')
57
+ }
58
+
27
59
  /**
28
60
  * Derive a child signer using a relative path (e.g., "0'/0/0").
29
61
  *
@@ -46,9 +78,12 @@ export class ISigner {
46
78
  }
47
79
 
48
80
  /**
49
- * Disposes the signer and clears any secret material from memory.
81
+ * Signs a message.
82
+ *
83
+ * @param {string} message - The message to sign.
84
+ * @returns {Promise<string>} The message's signature.
50
85
  */
51
- dispose () {
52
- throw new NotImplementedError('dispose()')
86
+ async sign (message) {
87
+ throw new NotImplementedError('sign(message)')
53
88
  }
54
89
  }
@@ -37,33 +37,30 @@ import { NotImplementedError } from './errors.js'
37
37
  * @property {Uint8Array | null} privateKey - The private key (null if the account has been disposed).
38
38
  */
39
39
 
40
+ /** @typedef {import('./disposable.js').IDisposable} IDisposable */
41
+
40
42
  /**
41
43
  * @interface
44
+ * @extends {IWalletAccountReadOnly}
45
+ * @extends {IDisposable}
42
46
  * @template [TSignedTransaction=unknown]
43
47
  */
44
48
  export class IWalletAccount extends IWalletAccountReadOnly {
45
49
  /**
46
- * The derivation path's index of this account.
47
- *
48
- * @type {number}
49
- */
50
- get index () {
51
- throw new NotImplementedError('index')
52
- }
53
-
54
- /**
55
- * The derivation path of this account (see [BIP-44](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki)).
50
+ * The derivation path of this account (see [BIP-44](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki)),
51
+ * or null if the account's signer is not bound to a BIP-44 position (e.g. private-key signers).
56
52
  *
57
- * @type {string}
53
+ * @type {string | null}
58
54
  */
59
55
  get path () {
60
56
  throw new NotImplementedError('path')
61
57
  }
62
58
 
63
59
  /**
64
- * The account's key pair.
60
+ * The account's key pair, or null if the account's signer does not allow
61
+ * retrieving key material (e.g. hardware signers).
65
62
  *
66
- * @type {KeyPair}
63
+ * @type {KeyPair | null}
67
64
  */
68
65
  get keyPair () {
69
66
  throw new NotImplementedError('keyPair')
@@ -154,11 +151,4 @@ export class IWalletAccount extends IWalletAccountReadOnly {
154
151
  async toReadOnlyAccount () {
155
152
  throw new NotImplementedError('toReadOnlyAccount()')
156
153
  }
157
-
158
- /**
159
- * Disposes the wallet account, erasing the private key from the memory.
160
- */
161
- dispose () {
162
- throw new NotImplementedError('dispose()')
163
- }
164
154
  }
@@ -15,13 +15,13 @@
15
15
 
16
16
  import * as bip39 from 'bip39'
17
17
 
18
- import { NoSuchElementError, NotImplementedError, ValueError } from './errors.js'
18
+ import { InvalidSignerError, NoSuchElementError, NotImplementedError, ValueError } from './errors.js'
19
19
 
20
20
  /** @typedef {import('./wallet-account.js').IWalletAccount} IWalletAccount */
21
21
 
22
22
  /** @typedef {import('./signer.js').ISigner} ISigner */
23
+ /** @typedef {import('./disposable.js').IDisposable} IDisposable */
23
24
 
24
- /** @typedef {import('./errors.js').InvalidSignerError} InvalidSignerError */
25
25
  /** @typedef {import('./errors.js').ProviderError} ProviderError */
26
26
  /** @typedef {import('./errors.js').ProviderRequiredError} ProviderRequiredError */
27
27
 
@@ -37,7 +37,11 @@ import { NoSuchElementError, NotImplementedError, ValueError } from './errors.js
37
37
  * @property {bigint} fast - The fee rate for transaction sent with fast priority.
38
38
  */
39
39
 
40
- /** @abstract */
40
+ /**
41
+ * @abstract
42
+ * @template {ISigner} [TSigner=ISigner]
43
+ * @implements {IDisposable}
44
+ */
41
45
  export default class WalletManager {
42
46
  /**
43
47
  * Creates a new wallet manager from a seed.
@@ -52,13 +56,11 @@ export default class WalletManager {
52
56
  * Creates a new wallet manager from a default signer.
53
57
  *
54
58
  * @overload
55
- * @param {ISigner} signer - The default signer.
59
+ * @param {TSigner} signer - The default signer.
56
60
  * @param {WalletConfig} [config] - The wallet configuration.
57
61
  * @throws {InvalidSignerError} If the given signer doesn't support account derivation.
58
62
  */
59
63
  constructor (seedOrSigner, config = {}) {
60
- // TODO: Add check to assert that the default signer is derivable.
61
-
62
64
  if (typeof seedOrSigner === 'string') {
63
65
  if (!WalletManager.isValidSeedPhrase(seedOrSigner)) {
64
66
  throw new ValueError('Invalid seed phrase.')
@@ -69,6 +71,10 @@ export default class WalletManager {
69
71
 
70
72
  const isSeed = seedOrSigner instanceof Uint8Array
71
73
 
74
+ if (!isSeed && !seedOrSigner.isDerivable) {
75
+ throw new InvalidSignerError('The default signer must be derivable. Non-derivable signers (e.g. private-key signers) can only be registered by name via addSigner.')
76
+ }
77
+
72
78
  /** @private */
73
79
  this._seed = isSeed ? seedOrSigner : undefined
74
80
 
@@ -76,7 +82,7 @@ export default class WalletManager {
76
82
  * The default signer.
77
83
  *
78
84
  * @protected
79
- * @type {ISigner | undefined}
85
+ * @type {TSigner | undefined}
80
86
  */
81
87
  this._defaultSigner = isSeed ? undefined : seedOrSigner
82
88
 
@@ -84,7 +90,7 @@ export default class WalletManager {
84
90
  * A map between signer names and signers added via {@link addSigner}.
85
91
  *
86
92
  * @protected
87
- * @type {Record<string, ISigner>}
93
+ * @type {Record<string, TSigner>}
88
94
  */
89
95
  this._signers = {}
90
96
 
@@ -142,8 +148,8 @@ export default class WalletManager {
142
148
  * Registers a signer with the given name.
143
149
  *
144
150
  * @param {string} signerName - The signer name.
145
- * @param {ISigner} signer - The signer.
146
- * @returns {WalletManager} The wallet manager.
151
+ * @param {TSigner} signer - The signer.
152
+ * @returns {this} The wallet manager.
147
153
  * @throws {ValueError} If the signer name is an empty or blank string.
148
154
  */
149
155
  addSigner (signerName, signer) {
@@ -160,7 +166,7 @@ export default class WalletManager {
160
166
  * Returns the default signer, or the signer with the given name.
161
167
  *
162
168
  * @param {string} [signerName] - If set, returns the signer with the given name.
163
- * @returns {ISigner} The signer.
169
+ * @returns {TSigner} The signer.
164
170
  * @throws {NoSuchElementError} If the default signer is not set, or no signers are found for the given name.
165
171
  */
166
172
  getSigner (signerName) {
@@ -186,7 +192,7 @@ export default class WalletManager {
186
192
  * The default signer is not included; use {@link getSigner} with no arguments
187
193
  * to retrieve it.
188
194
  *
189
- * @returns {Record<string, ISigner>} A map of signer names to signers. Empty if no signers have been registered.
195
+ * @returns {Record<string, TSigner>} A map of signer names to signers. Empty if no signers have been registered.
190
196
  */
191
197
  getSigners () {
192
198
  return { ...this._signers }
@@ -251,7 +257,7 @@ export default class WalletManager {
251
257
  }
252
258
 
253
259
  /**
254
- * Disposes all wallet accounts and signers, clearing secret material from memory.
260
+ * Disposes all wallet accounts, clearing secret material from memory.
255
261
  */
256
262
  dispose () {
257
263
  for (const account of Object.values(this._accounts)) {
@@ -260,14 +266,6 @@ export default class WalletManager {
260
266
  }
261
267
  }
262
268
 
263
- this._defaultSigner?.dispose()
264
-
265
- for (const signer of Object.values(this._signers)) {
266
- signer.dispose()
267
- }
268
-
269
269
  this._accounts = {}
270
- this._defaultSigner = undefined
271
- this._signers = {}
272
270
  }
273
271
  }
package/types/index.d.ts CHANGED
@@ -12,6 +12,7 @@ export type TransactionReceipt = import("./src/wallet-account-read-only-simple.j
12
12
  export type WaitForTransactionTarget = import("./src/wallet-account-read-only-simple.js").WaitForTransactionTarget;
13
13
  export type WaitForTransactionOptions = import("./src/wallet-account-read-only-simple.js").WaitForTransactionOptions;
14
14
  export type KeyPair = import("./src/wallet-account.js").KeyPair;
15
+ export { IDisposable } from "./src/disposable.js";
15
16
  export type ProviderErrorOptions = import("./src/errors.js").ProviderErrorOptions;
16
17
  export type TransactionErrorOptions = import("./src/errors.js").TransactionErrorOptions;
17
18
  export type TransferErrorOptions = import("./src/errors.js").TransferErrorOptions;
@@ -0,0 +1,7 @@
1
+ /** @interface */
2
+ export class IDisposable {
3
+ /**
4
+ * Disposes the object along with all its data (cleaning up any sensitive field from memory).
5
+ */
6
+ dispose(): void;
7
+ }
@@ -3,7 +3,27 @@
3
3
  *
4
4
  * @interface
5
5
  */
6
- export class ISigner {
6
+ export class ISigner extends IDisposable {
7
+ /**
8
+ * Whether the signer supports account derivation via {@link derive}.
9
+ *
10
+ * @type {boolean}
11
+ */
12
+ get isDerivable(): boolean;
13
+ /**
14
+ * The signer's key pair, or null if the signer does not allow retrieving
15
+ * key material (e.g. hardware signers).
16
+ *
17
+ * @type {KeyPair | null}
18
+ */
19
+ get keyPair(): KeyPair | null;
20
+ /**
21
+ * The BIP 0032 derivation path, or null if the signer is not bound to a derivation position
22
+ * (e.g. private-key signers).
23
+ *
24
+ * @type {string | null}
25
+ */
26
+ get path(): string | null;
7
27
  /**
8
28
  * Derive a child signer using a relative path (e.g., "0'/0/0").
9
29
  *
@@ -20,9 +40,14 @@ export class ISigner {
20
40
  */
21
41
  getAddress(): Promise<string>;
22
42
  /**
23
- * Disposes the signer and clears any secret material from memory.
43
+ * Signs a message.
44
+ *
45
+ * @param {string} message - The message to sign.
46
+ * @returns {Promise<string>} The message's signature.
24
47
  */
25
- dispose(): void;
48
+ sign(message: string): Promise<string>;
26
49
  }
50
+ export type KeyPair = import("./wallet-account.js").KeyPair;
51
+ import { IDisposable } from "./disposable.js";
27
52
  export type UnsupportedOperationError = import("./errors.js").UnsupportedOperationError;
28
53
  export type ValueError = import("./errors.js").ValueError;
@@ -1,26 +1,24 @@
1
1
  /**
2
2
  * @interface
3
+ * @extends {IWalletAccountReadOnly}
4
+ * @extends {IDisposable}
3
5
  * @template [TSignedTransaction=unknown]
4
6
  */
5
- export interface IWalletAccount<TSignedTransaction = unknown> extends IWalletAccountReadOnly {
7
+ export interface IWalletAccount<TSignedTransaction = unknown> extends IWalletAccountReadOnly, IDisposable {
6
8
  /**
7
- * The derivation path's index of this account.
9
+ * The derivation path of this account (see [BIP-44](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki)),
10
+ * or null if the account's signer is not bound to a BIP-44 position (e.g. private-key signers).
8
11
  *
9
- * @type {number}
12
+ * @type {string | null}
10
13
  */
11
- get index(): number;
14
+ get path(): string | null;
12
15
  /**
13
- * The derivation path of this account (see [BIP-44](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki)).
16
+ * The account's key pair, or null if the account's signer does not allow
17
+ * retrieving key material (e.g. hardware signers).
14
18
  *
15
- * @type {string}
19
+ * @type {KeyPair | null}
16
20
  */
17
- get path(): string;
18
- /**
19
- * The account's key pair.
20
- *
21
- * @type {KeyPair}
22
- */
23
- get keyPair(): KeyPair;
21
+ get keyPair(): KeyPair | null;
24
22
  /**
25
23
  * Signs a message.
26
24
  *
@@ -86,10 +84,6 @@ export interface IWalletAccount<TSignedTransaction = unknown> extends IWalletAcc
86
84
  * @returns {Promise<IWalletAccountReadOnly>} The read-only account.
87
85
  */
88
86
  toReadOnlyAccount(): Promise<IWalletAccountReadOnly>;
89
- /**
90
- * Disposes the wallet account, erasing the private key from the memory.
91
- */
92
- dispose(): void;
93
87
  }
94
88
  export type Transaction = import("./wallet-account-read-only.js").Transaction;
95
89
  export type TransactionResult = import("./wallet-account-read-only.js").TransactionResult;
@@ -113,3 +107,4 @@ export type KeyPair = {
113
107
  privateKey: Uint8Array | null;
114
108
  };
115
109
  import { IWalletAccountReadOnly } from './wallet-account-read-only.js';
110
+ export type IDisposable = import("./disposable.js").IDisposable;
@@ -1,5 +1,9 @@
1
- /** @abstract */
2
- export default abstract class WalletManager {
1
+ /**
2
+ * @abstract
3
+ * @template {ISigner} [TSigner=ISigner]
4
+ * @implements {IDisposable}
5
+ */
6
+ export default abstract class WalletManager<TSigner extends ISigner = ISigner> implements IDisposable {
3
7
  /**
4
8
  * Returns a random [BIP-39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki) seed phrase.
5
9
  *
@@ -27,27 +31,27 @@ export default abstract class WalletManager {
27
31
  * Creates a new wallet manager from a default signer.
28
32
  *
29
33
  * @overload
30
- * @param {ISigner} signer - The default signer.
34
+ * @param {TSigner} signer - The default signer.
31
35
  * @param {WalletConfig} [config] - The wallet configuration.
32
36
  * @throws {InvalidSignerError} If the given signer doesn't support account derivation.
33
37
  */
34
- constructor(signer: ISigner, config?: WalletConfig);
38
+ constructor(signer: TSigner, config?: WalletConfig);
35
39
  /** @private */
36
40
  private _seed;
37
41
  /**
38
42
  * The default signer.
39
43
  *
40
44
  * @protected
41
- * @type {ISigner | undefined}
45
+ * @type {TSigner | undefined}
42
46
  */
43
- protected _defaultSigner: ISigner | undefined;
47
+ protected _defaultSigner: TSigner | undefined;
44
48
  /**
45
49
  * A map between signer names and signers added via {@link addSigner}.
46
50
  *
47
51
  * @protected
48
- * @type {Record<string, ISigner>}
52
+ * @type {Record<string, TSigner>}
49
53
  */
50
- protected _signers: Record<string, ISigner>;
54
+ protected _signers: Record<string, TSigner>;
51
55
  /**
52
56
  * A map between derivation paths and wallet accounts. The {@link dispose} method will automatically dispose
53
57
  * all the accounts in this map, so developers are encouraged to map all accounts accessed through the
@@ -74,27 +78,27 @@ export default abstract class WalletManager {
74
78
  * Registers a signer with the given name.
75
79
  *
76
80
  * @param {string} signerName - The signer name.
77
- * @param {ISigner} signer - The signer.
78
- * @returns {WalletManager} The wallet manager.
81
+ * @param {TSigner} signer - The signer.
82
+ * @returns {this} The wallet manager.
79
83
  * @throws {ValueError} If the signer name is an empty or blank string.
80
84
  */
81
- addSigner(signerName: string, signer: ISigner): WalletManager;
85
+ addSigner(signerName: string, signer: TSigner): this;
82
86
  /**
83
87
  * Returns the default signer, or the signer with the given name.
84
88
  *
85
89
  * @param {string} [signerName] - If set, returns the signer with the given name.
86
- * @returns {ISigner} The signer.
90
+ * @returns {TSigner} The signer.
87
91
  * @throws {NoSuchElementError} If the default signer is not set, or no signers are found for the given name.
88
92
  */
89
- getSigner(signerName?: string): ISigner;
93
+ getSigner(signerName?: string): TSigner;
90
94
  /**
91
95
  * Returns a shallow copy of the map of signers registered via {@link addSigner}.
92
96
  * The default signer is not included; use {@link getSigner} with no arguments
93
97
  * to retrieve it.
94
98
  *
95
- * @returns {Record<string, ISigner>} A map of signer names to signers. Empty if no signers have been registered.
99
+ * @returns {Record<string, TSigner>} A map of signer names to signers. Empty if no signers have been registered.
96
100
  */
97
- getSigners(): Record<string, ISigner>;
101
+ getSigners(): Record<string, TSigner>;
98
102
  /**
99
103
  * Returns the wallet account at a specific index (see [BIP-44](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki)).
100
104
  *
@@ -147,7 +151,7 @@ export default abstract class WalletManager {
147
151
  */
148
152
  abstract getFeeRates(): Promise<FeeRates>;
149
153
  /**
150
- * Disposes all wallet accounts and signers, clearing secret material from memory.
154
+ * Disposes all wallet accounts, clearing secret material from memory.
151
155
  */
152
156
  dispose(): void;
153
157
  }
@@ -176,3 +180,4 @@ export type FeeRates = {
176
180
  */
177
181
  fast: bigint;
178
182
  };
183
+ import { IDisposable } from "./disposable.js";