@twin.org/dlt-account 0.9.2-next.2
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/LICENSE +201 -0
- package/README.md +21 -0
- package/dist/es/helpers/accountHelper.js +310 -0
- package/dist/es/helpers/accountHelper.js.map +1 -0
- package/dist/es/index.js +5 -0
- package/dist/es/index.js.map +1 -0
- package/dist/es/models/IAccountConfig.js +4 -0
- package/dist/es/models/IAccountConfig.js.map +1 -0
- package/dist/types/helpers/accountHelper.d.ts +143 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/models/IAccountConfig.d.ts +21 -0
- package/docs/changelog.md +10 -0
- package/docs/examples.md +146 -0
- package/docs/reference/classes/AccountHelper.md +545 -0
- package/docs/reference/index.md +9 -0
- package/docs/reference/interfaces/IAccountConfig.md +35 -0
- package/locales/en.json +8 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2024 IOTA Stiftung.
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# TWIN DLT Account
|
|
2
|
+
|
|
3
|
+
This package provides helper utilities for managing distributed ledger accounts. It covers the full account lifecycle against a vault connector, including BIP44 key derivation, secure storage of mnemonics and seeds, address generation, and account migration operations. Services that need consistent, repeatable account management can use it to avoid reimplementing common cryptographic patterns.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```shell
|
|
8
|
+
npm install @twin.org/dlt-account
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Examples
|
|
12
|
+
|
|
13
|
+
Usage of the APIs is shown in the examples [docs/examples.md](docs/examples.md)
|
|
14
|
+
|
|
15
|
+
## Reference
|
|
16
|
+
|
|
17
|
+
Detailed reference documentation for the API can be found in [docs/reference/index.md](docs/reference/index.md)
|
|
18
|
+
|
|
19
|
+
## Changelog
|
|
20
|
+
|
|
21
|
+
The changes between each version can be found in [docs/changelog.md](docs/changelog.md)
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
// Copyright 2026 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
import { Converter, GeneralError, Guards } from "@twin.org/core";
|
|
4
|
+
import { Bip39, Bip44, Blake2b, KeyType } from "@twin.org/crypto";
|
|
5
|
+
import { VaultConnectorHelper, VaultKeyType } from "@twin.org/vault-models";
|
|
6
|
+
/**
|
|
7
|
+
* Helper class for account management operations.
|
|
8
|
+
*/
|
|
9
|
+
export class AccountHelper {
|
|
10
|
+
/**
|
|
11
|
+
* Runtime name for the class.
|
|
12
|
+
*/
|
|
13
|
+
static CLASS_NAME = "AccountHelper";
|
|
14
|
+
/**
|
|
15
|
+
* Default name for the mnemonic secret.
|
|
16
|
+
*/
|
|
17
|
+
static DEFAULT_MNEMONIC_SECRET_NAME = "mnemonic";
|
|
18
|
+
/**
|
|
19
|
+
* Default name for the seed secret.
|
|
20
|
+
*/
|
|
21
|
+
static DEFAULT_SEED_SECRET_NAME = "seed";
|
|
22
|
+
/**
|
|
23
|
+
* Default coin type.
|
|
24
|
+
*/
|
|
25
|
+
static DEFAULT_COIN_TYPE = 4218;
|
|
26
|
+
/**
|
|
27
|
+
* Default pre-calculation chunk size.
|
|
28
|
+
*/
|
|
29
|
+
static DEFAULT_CALC_CHUNK_SIZE = 25;
|
|
30
|
+
/**
|
|
31
|
+
* Default scan range.
|
|
32
|
+
*/
|
|
33
|
+
static DEFAULT_SCAN_RANGE_SIZE = 1000;
|
|
34
|
+
/**
|
|
35
|
+
* Create a new account by generating a mnemonic and seed, storing them in the vault, and pre-caching the first chunk of derived keys.
|
|
36
|
+
* @param accountConfig The account configuration.
|
|
37
|
+
* @param vaultConnector The vault connector.
|
|
38
|
+
* @param identity The identity of the user to access the vault keys.
|
|
39
|
+
* @param mnemonic The mnemonic to store, if undefined a new one will be generated and returned.
|
|
40
|
+
* @param accountIndex The account index to pre-cache.
|
|
41
|
+
* @returns The mnemonic that was stored.
|
|
42
|
+
*/
|
|
43
|
+
static async createAccountKeys(accountConfig, vaultConnector, identity, mnemonic, accountIndex) {
|
|
44
|
+
Guards.object(AccountHelper.CLASS_NAME, "vaultConnector", vaultConnector);
|
|
45
|
+
Guards.stringValue(AccountHelper.CLASS_NAME, "identity", identity);
|
|
46
|
+
const mnemonicToStore = mnemonic ?? Bip39.randomMnemonic();
|
|
47
|
+
await vaultConnector.setSecret(AccountHelper.buildMnemonicKey(identity, accountConfig?.vaultMnemonicId), mnemonicToStore);
|
|
48
|
+
const seed = Bip39.mnemonicToSeed(mnemonicToStore);
|
|
49
|
+
await vaultConnector.setSecret(AccountHelper.buildSeedKey(identity, accountConfig?.vaultSeedId), Converter.bytesToBase64(seed));
|
|
50
|
+
await AccountHelper.getPublicKeys(accountConfig, vaultConnector, identity, accountIndex ?? 0, false, 0, async () => seed);
|
|
51
|
+
return mnemonicToStore;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Rename all vault entries for an account from one identity to another.
|
|
55
|
+
* @param accountConfig The account configuration.
|
|
56
|
+
* @param vaultConnector The vault connector.
|
|
57
|
+
* @param fromIdentity The source identity whose vault entries should be copied.
|
|
58
|
+
* @param toIdentity The destination identity that will receive the copied entries.
|
|
59
|
+
*/
|
|
60
|
+
static async renameAccountKeys(accountConfig, vaultConnector, fromIdentity, toIdentity) {
|
|
61
|
+
Guards.object(AccountHelper.CLASS_NAME, "vaultConnector", vaultConnector);
|
|
62
|
+
Guards.stringValue(AccountHelper.CLASS_NAME, "fromIdentity", fromIdentity);
|
|
63
|
+
Guards.stringValue(AccountHelper.CLASS_NAME, "toIdentity", toIdentity);
|
|
64
|
+
if (fromIdentity === toIdentity) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const mnemonicKey = AccountHelper.buildMnemonicKey(fromIdentity, accountConfig?.vaultMnemonicId);
|
|
68
|
+
const mnemonic = await vaultConnector.getSecret(mnemonicKey);
|
|
69
|
+
await vaultConnector.setSecret(AccountHelper.buildMnemonicKey(toIdentity, accountConfig?.vaultMnemonicId), mnemonic);
|
|
70
|
+
await vaultConnector.removeSecret(mnemonicKey);
|
|
71
|
+
try {
|
|
72
|
+
const seedKey = AccountHelper.buildSeedKey(fromIdentity, accountConfig?.vaultSeedId);
|
|
73
|
+
const seed = await vaultConnector.getSecret(seedKey);
|
|
74
|
+
await vaultConnector.setSecret(AccountHelper.buildSeedKey(toIdentity, accountConfig?.vaultSeedId), seed);
|
|
75
|
+
await vaultConnector.removeSecret(seedKey);
|
|
76
|
+
}
|
|
77
|
+
catch { }
|
|
78
|
+
for (let accountIndex = 0;; accountIndex++) {
|
|
79
|
+
let accountHasKeys = false;
|
|
80
|
+
for (const internal of [false, true]) {
|
|
81
|
+
for (let chunkStart = 0;; chunkStart += AccountHelper.DEFAULT_CALC_CHUNK_SIZE) {
|
|
82
|
+
const firstKeyName = AccountHelper.buildAddressKeyName(fromIdentity, accountIndex, internal, chunkStart);
|
|
83
|
+
if (!(await vaultConnector.keyExists(firstKeyName))) {
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
accountHasKeys = true;
|
|
87
|
+
for (let i = chunkStart; i < chunkStart + AccountHelper.DEFAULT_CALC_CHUNK_SIZE; i++) {
|
|
88
|
+
await vaultConnector.renameKey(AccountHelper.buildAddressKeyName(fromIdentity, accountIndex, internal, i), AccountHelper.buildAddressKeyName(toIdentity, accountIndex, internal, i));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (!accountHasKeys) {
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Remove all vault entries for an account.
|
|
99
|
+
* @param accountConfig The account configuration.
|
|
100
|
+
* @param vaultConnector The vault connector.
|
|
101
|
+
* @param identity The identity of the user whose vault keys should be removed.
|
|
102
|
+
*/
|
|
103
|
+
static async removeAccountKeys(accountConfig, vaultConnector, identity) {
|
|
104
|
+
Guards.object(AccountHelper.CLASS_NAME, "vaultConnector", vaultConnector);
|
|
105
|
+
Guards.stringValue(AccountHelper.CLASS_NAME, "identity", identity);
|
|
106
|
+
for (let accountIndex = 0;; accountIndex++) {
|
|
107
|
+
let accountHasKeys = false;
|
|
108
|
+
for (const internal of [false, true]) {
|
|
109
|
+
for (let chunkStart = 0;; chunkStart += AccountHelper.DEFAULT_CALC_CHUNK_SIZE) {
|
|
110
|
+
const firstKeyName = AccountHelper.buildAddressKeyName(identity, accountIndex, internal, chunkStart);
|
|
111
|
+
if (!(await vaultConnector.keyExists(firstKeyName))) {
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
accountHasKeys = true;
|
|
115
|
+
for (let i = chunkStart; i < chunkStart + AccountHelper.DEFAULT_CALC_CHUNK_SIZE; i++) {
|
|
116
|
+
await vaultConnector.removeKey(AccountHelper.buildAddressKeyName(identity, accountIndex, internal, i));
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (!accountHasKeys) {
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
try {
|
|
125
|
+
await vaultConnector.removeSecret(AccountHelper.buildSeedKey(identity, accountConfig?.vaultSeedId));
|
|
126
|
+
}
|
|
127
|
+
catch { }
|
|
128
|
+
try {
|
|
129
|
+
await vaultConnector.removeSecret(AccountHelper.buildMnemonicKey(identity, accountConfig?.vaultMnemonicId));
|
|
130
|
+
}
|
|
131
|
+
catch { }
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Get the key for storing the seed.
|
|
135
|
+
* @param identity The identity to use.
|
|
136
|
+
* @param vaultSeedId The seed ID to use.
|
|
137
|
+
* @returns The seed key.
|
|
138
|
+
*/
|
|
139
|
+
static buildSeedKey(identity, vaultSeedId) {
|
|
140
|
+
return VaultConnectorHelper.buildKeyName(identity, vaultSeedId ?? AccountHelper.DEFAULT_SEED_SECRET_NAME);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Get the key for storing the mnemonic.
|
|
144
|
+
* @param identity The identity to use.
|
|
145
|
+
* @param vaultMnemonicId The mnemonic ID to use.
|
|
146
|
+
* @returns The mnemonic key.
|
|
147
|
+
*/
|
|
148
|
+
static buildMnemonicKey(identity, vaultMnemonicId) {
|
|
149
|
+
return VaultConnectorHelper.buildKeyName(identity, vaultMnemonicId ?? AccountHelper.DEFAULT_MNEMONIC_SECRET_NAME);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Ensure a range of BIP44-derived keys are registered as individual vault keys and return their public keys.
|
|
153
|
+
* If the first key of the range already exists the range is considered registered and only public keys are derived.
|
|
154
|
+
* If not registered, all keys are derived and added to the vault before returning the public keys.
|
|
155
|
+
* @param accountConfig The account configuration.
|
|
156
|
+
* @param vaultConnector The vault connector to use.
|
|
157
|
+
* @param identity The identity of the user to access the vault keys.
|
|
158
|
+
* @param accountIndex The account index.
|
|
159
|
+
* @param internal Whether the addresses are internal or external.
|
|
160
|
+
* @param addressIndex Any address index within the desired chunk; aligned internally.
|
|
161
|
+
* @param seedProvider Callback invoked at most once per call to supply the seed when a chunk is not yet registered.
|
|
162
|
+
* @returns The base64-encoded public keys for each address in the range.
|
|
163
|
+
*/
|
|
164
|
+
static async getPublicKeys(accountConfig, vaultConnector, identity, accountIndex, internal, addressIndex, seedProvider) {
|
|
165
|
+
const chunkStart = addressIndex - (addressIndex % AccountHelper.DEFAULT_CALC_CHUNK_SIZE);
|
|
166
|
+
const firstKeyName = AccountHelper.buildAddressKeyName(identity, accountIndex, internal, chunkStart);
|
|
167
|
+
const publicKeys = [];
|
|
168
|
+
if (await vaultConnector.keyExists(firstKeyName)) {
|
|
169
|
+
for (let i = chunkStart; i < chunkStart + AccountHelper.DEFAULT_CALC_CHUNK_SIZE; i++) {
|
|
170
|
+
const keyName = AccountHelper.buildAddressKeyName(identity, accountIndex, internal, i);
|
|
171
|
+
const keyData = await vaultConnector.getKey(keyName, "public");
|
|
172
|
+
if (!keyData.publicKey) {
|
|
173
|
+
throw new GeneralError(AccountHelper.CLASS_NAME, "missingPublicKey", { keyName });
|
|
174
|
+
}
|
|
175
|
+
publicKeys.push(Converter.bytesToBase64(keyData.publicKey));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
const seed = await seedProvider();
|
|
180
|
+
const coinType = accountConfig?.coinType ?? AccountHelper.DEFAULT_COIN_TYPE;
|
|
181
|
+
for (let i = chunkStart; i < chunkStart + AccountHelper.DEFAULT_CALC_CHUNK_SIZE; i++) {
|
|
182
|
+
const keyName = AccountHelper.buildAddressKeyName(identity, accountIndex, internal, i);
|
|
183
|
+
const keyPair = Bip44.keyPair(seed, KeyType.Ed25519, coinType, accountIndex, internal, i);
|
|
184
|
+
await vaultConnector.addKey(keyName, VaultKeyType.Ed25519, keyPair.privateKey, keyPair.publicKey);
|
|
185
|
+
publicKeys.push(Converter.bytesToBase64(keyPair.publicKey));
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return publicKeys;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Build the vault key name for a specific derived address.
|
|
192
|
+
* @param identity The identity to use.
|
|
193
|
+
* @param accountIndex The account index.
|
|
194
|
+
* @param internal Whether the address is internal or external.
|
|
195
|
+
* @param addressIndex The address index.
|
|
196
|
+
* @returns The vault key name.
|
|
197
|
+
*/
|
|
198
|
+
static buildAddressKeyName(identity, accountIndex, internal, addressIndex) {
|
|
199
|
+
return VaultConnectorHelper.buildKeyName(identity, "account", accountIndex.toString(), internal ? "1" : "0", `${addressIndex}`);
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Get address for the identity.
|
|
203
|
+
* @param accountConfig The account configuration.
|
|
204
|
+
* @param vaultConnector The vault connector.
|
|
205
|
+
* @param identity The identity of the user to access the vault keys.
|
|
206
|
+
* @param accountIndex The account index to get the addresses for.
|
|
207
|
+
* @param startAddressIndex The start index for the addresses.
|
|
208
|
+
* @param isInternal Whether the addresses are internal.
|
|
209
|
+
* @returns The address.
|
|
210
|
+
*/
|
|
211
|
+
static async getAddress(accountConfig, vaultConnector, identity, accountIndex, startAddressIndex, isInternal) {
|
|
212
|
+
const addresses = await AccountHelper.getAddresses(accountConfig, vaultConnector, identity, accountIndex, startAddressIndex, 1, isInternal);
|
|
213
|
+
return addresses[0];
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Get addresses for the identity.
|
|
217
|
+
* @param accountConfig The account configuration.
|
|
218
|
+
* @param vaultConnector The vault connector.
|
|
219
|
+
* @param identity The identity of the user to access the vault keys.
|
|
220
|
+
* @param accountIndex The account index to get the addresses for.
|
|
221
|
+
* @param startAddressIndex The start index for the addresses.
|
|
222
|
+
* @param count The number of addresses to generate.
|
|
223
|
+
* @param isInternal Whether the addresses are internal.
|
|
224
|
+
* @returns The list of addresses.
|
|
225
|
+
*/
|
|
226
|
+
static async getAddresses(accountConfig, vaultConnector, identity, accountIndex, startAddressIndex, count, isInternal) {
|
|
227
|
+
Guards.object(AccountHelper.CLASS_NAME, "vaultConnector", vaultConnector);
|
|
228
|
+
Guards.stringValue(AccountHelper.CLASS_NAME, "identity", identity);
|
|
229
|
+
Guards.integer(AccountHelper.CLASS_NAME, "accountIndex", accountIndex);
|
|
230
|
+
Guards.integer(AccountHelper.CLASS_NAME, "startAddressIndex", startAddressIndex);
|
|
231
|
+
Guards.integer(AccountHelper.CLASS_NAME, "count", count);
|
|
232
|
+
const addresses = [];
|
|
233
|
+
const internal = isInternal ?? false;
|
|
234
|
+
let currentIndex = startAddressIndex;
|
|
235
|
+
let cachedSeed;
|
|
236
|
+
const seedProvider = async () => {
|
|
237
|
+
cachedSeed ??= await AccountHelper.getSeed(accountConfig, vaultConnector, identity);
|
|
238
|
+
return cachedSeed;
|
|
239
|
+
};
|
|
240
|
+
while (addresses.length < count) {
|
|
241
|
+
const chunkStart = Math.floor(currentIndex / AccountHelper.DEFAULT_CALC_CHUNK_SIZE) *
|
|
242
|
+
AccountHelper.DEFAULT_CALC_CHUNK_SIZE;
|
|
243
|
+
const publicKeys = await AccountHelper.getPublicKeys(accountConfig, vaultConnector, identity, accountIndex, internal, chunkStart, seedProvider);
|
|
244
|
+
const chunk = publicKeys.map((pk) => AccountHelper.publicKeyToAddress(Converter.base64ToBytes(pk)));
|
|
245
|
+
const offsetInChunk = currentIndex - chunkStart;
|
|
246
|
+
const remaining = count - addresses.length;
|
|
247
|
+
addresses.push(...chunk.slice(offsetInChunk, offsetInChunk + remaining));
|
|
248
|
+
currentIndex = chunkStart + AccountHelper.DEFAULT_CALC_CHUNK_SIZE;
|
|
249
|
+
}
|
|
250
|
+
return addresses;
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Get the seed from the vault, deriving it from the mnemonic if necessary.
|
|
254
|
+
* @param accountConfig The account configuration.
|
|
255
|
+
* @param vaultConnector The vault connector to use.
|
|
256
|
+
* @param identity The identity of the user to access the vault keys.
|
|
257
|
+
* @returns The seed bytes.
|
|
258
|
+
*/
|
|
259
|
+
static async getSeed(accountConfig, vaultConnector, identity) {
|
|
260
|
+
const seedKey = AccountHelper.buildSeedKey(identity, accountConfig?.vaultSeedId);
|
|
261
|
+
try {
|
|
262
|
+
const seedBase64 = await vaultConnector.getSecret(seedKey);
|
|
263
|
+
return Converter.base64ToBytes(seedBase64);
|
|
264
|
+
}
|
|
265
|
+
catch { }
|
|
266
|
+
const mnemonic = await vaultConnector.getSecret(AccountHelper.buildMnemonicKey(identity, accountConfig?.vaultMnemonicId));
|
|
267
|
+
// If the seed is not found but the mnemonic exists, derive the seed and store it for future use
|
|
268
|
+
const seed = Bip39.mnemonicToSeed(mnemonic);
|
|
269
|
+
await vaultConnector.setSecret(seedKey, Converter.bytesToBase64(seed));
|
|
270
|
+
return seed;
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Find the vault key name and public key for a specific address by scanning derived keys.
|
|
274
|
+
* @param accountConfig The account configuration.
|
|
275
|
+
* @param vaultConnector The vault connector to use.
|
|
276
|
+
* @param identity The identity of the user to access the vault keys.
|
|
277
|
+
* @param address The owner address whose key should be located.
|
|
278
|
+
* @param accountIndex The account index to search.
|
|
279
|
+
* @returns The vault key name and the public key bytes.
|
|
280
|
+
*/
|
|
281
|
+
static async findAddressKey(accountConfig, vaultConnector, identity, address, accountIndex = 0) {
|
|
282
|
+
const scanRange = accountConfig?.maxAddressScanRange ?? AccountHelper.DEFAULT_SCAN_RANGE_SIZE;
|
|
283
|
+
let cachedSeed;
|
|
284
|
+
const seedProvider = async () => {
|
|
285
|
+
cachedSeed ??= await AccountHelper.getSeed(accountConfig, vaultConnector, identity);
|
|
286
|
+
return cachedSeed;
|
|
287
|
+
};
|
|
288
|
+
for (let chunkStart = 0; chunkStart < scanRange; chunkStart += AccountHelper.DEFAULT_CALC_CHUNK_SIZE) {
|
|
289
|
+
const publicKeys = await AccountHelper.getPublicKeys(accountConfig, vaultConnector, identity, accountIndex, false, chunkStart, seedProvider);
|
|
290
|
+
const matchIndex = publicKeys.findIndex((pk) => AccountHelper.publicKeyToAddress(Converter.base64ToBytes(pk)) === address);
|
|
291
|
+
if (matchIndex >= 0) {
|
|
292
|
+
const addressIndex = chunkStart + matchIndex;
|
|
293
|
+
return {
|
|
294
|
+
keyName: AccountHelper.buildAddressKeyName(identity, accountIndex, false, addressIndex),
|
|
295
|
+
publicKey: Converter.base64ToBytes(publicKeys[matchIndex])
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
throw new GeneralError(AccountHelper.CLASS_NAME, "addressNotFound", { address });
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Derive an address from a public key.
|
|
303
|
+
* @param publicKey The public key to derive the address from.
|
|
304
|
+
* @returns The derived address.
|
|
305
|
+
*/
|
|
306
|
+
static publicKeyToAddress(publicKey) {
|
|
307
|
+
return Converter.bytesToHex(Blake2b.sum256(publicKey), true);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
//# sourceMappingURL=accountHelper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"accountHelper.js","sourceRoot":"","sources":["../../../src/helpers/accountHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACjE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAGlE,OAAO,EAAE,oBAAoB,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAG5E;;GAEG;AACH,MAAM,OAAO,aAAa;IACzB;;OAEG;IACI,MAAM,CAAU,UAAU,mBAAmC;IAEpE;;OAEG;IACI,MAAM,CAAU,4BAA4B,GAAW,UAAU,CAAC;IAEzE;;OAEG;IACI,MAAM,CAAU,wBAAwB,GAAW,MAAM,CAAC;IAEjE;;OAEG;IACI,MAAM,CAAU,iBAAiB,GAAW,IAAI,CAAC;IAExD;;OAEG;IACI,MAAM,CAAU,uBAAuB,GAAW,EAAE,CAAC;IAE5D;;OAEG;IACI,MAAM,CAAU,uBAAuB,GAAW,IAAI,CAAC;IAE9D;;;;;;;;OAQG;IACI,MAAM,CAAC,KAAK,CAAC,iBAAiB,CACpC,aAAyC,EACzC,cAA+B,EAC/B,QAAgB,EAChB,QAAiB,EACjB,YAAqB;QAErB,MAAM,CAAC,MAAM,CAAC,aAAa,CAAC,UAAU,oBAA0B,cAAc,CAAC,CAAC;QA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tB,UAAU,IAAI,aAAa,CAAC,uBAAuB,EAClD,CAAC;YACF,MAAM,UAAU,GAAG,MAAM,aAAa,CAAC,aAAa,CACnD,aAAa,EACb,cAAc,EACd,QAAQ,EACR,YAAY,EACZ,KAAK,EACL,UAAU,EACV,YAAY,CACZ,CAAC;YACF,MAAM,UAAU,GAAG,UAAU,CAAC,SAAS,CACtC,CAAC,EAAU,EAAE,EAAE,CAAC,aAAa,CAAC,kBAAkB,CAAC,SAAS,CAAC,aAAa,CAAC,EAAE,CAAC,CAAC,KAAK,OAAO,CACzF,CAAC;YACF,IAAI,UAAU,IAAI,CAAC,EAAE,CAAC;gBACrB,MAAM,YAAY,GAAG,UAAU,GAAG,UAAU,CAAC;gBAC7C,OAAO;oBACN,OAAO,EAAE,aAAa,CAAC,mBAAmB,CAAC,QAAQ,EAAE,YAAY,EAAE,KAAK,EAAE,YAAY,CAAC;oBACvF,SAAS,EAAE,SAAS,CAAC,aAAa,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC;iBAC1D,CAAC;YACH,CAAC;QACF,CAAC;QAED,MAAM,IAAI,YAAY,CAAC,aAAa,CAAC,UAAU,EAAE,iBAAiB,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC;IAClF,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,kBAAkB,CAAC,SAAqB;QACrD,OAAO,SAAS,CAAC,UAAU,CAAC,OAAO,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,IAAI,CAAC,CAAC;IAC9D,CAAC","sourcesContent":["// Copyright 2026 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Converter, GeneralError, Guards } from \"@twin.org/core\";\nimport { Bip39, Bip44, Blake2b, KeyType } from \"@twin.org/crypto\";\nimport { nameof } from \"@twin.org/nameof\";\nimport type { IVaultConnector } from \"@twin.org/vault-models\";\nimport { VaultConnectorHelper, VaultKeyType } from \"@twin.org/vault-models\";\nimport type { IAccountConfig } from \"../models/IAccountConfig.js\";\n\n/**\n * Helper class for account management operations.\n */\nexport class AccountHelper {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<AccountHelper>();\n\n\t/**\n\t * Default name for the mnemonic secret.\n\t */\n\tpublic static readonly DEFAULT_MNEMONIC_SECRET_NAME: string = \"mnemonic\";\n\n\t/**\n\t * Default name for the seed secret.\n\t */\n\tpublic static readonly DEFAULT_SEED_SECRET_NAME: string = \"seed\";\n\n\t/**\n\t * Default coin type.\n\t */\n\tpublic static readonly DEFAULT_COIN_TYPE: number = 4218;\n\n\t/**\n\t * Default pre-calculation chunk size.\n\t */\n\tpublic static readonly DEFAULT_CALC_CHUNK_SIZE: number = 25;\n\n\t/**\n\t * Default scan range.\n\t */\n\tpublic static readonly DEFAULT_SCAN_RANGE_SIZE: number = 1000;\n\n\t/**\n\t * Create a new account by generating a mnemonic and seed, storing them in the vault, and pre-caching the first chunk of derived keys.\n\t * @param accountConfig The account configuration.\n\t * @param vaultConnector The vault connector.\n\t * @param identity The identity of the user to access the vault keys.\n\t * @param mnemonic The mnemonic to store, if undefined a new one will be generated and returned.\n\t * @param accountIndex The account index to pre-cache.\n\t * @returns The mnemonic that was stored.\n\t */\n\tpublic static async createAccountKeys(\n\t\taccountConfig: IAccountConfig | undefined,\n\t\tvaultConnector: IVaultConnector,\n\t\tidentity: string,\n\t\tmnemonic?: string,\n\t\taccountIndex?: number\n\t): Promise<string> {\n\t\tGuards.object(AccountHelper.CLASS_NAME, nameof(vaultConnector), vaultConnector);\n\t\tGuards.stringValue(AccountHelper.CLASS_NAME, nameof(identity), identity);\n\n\t\tconst mnemonicToStore = mnemonic ?? Bip39.randomMnemonic();\n\n\t\tawait vaultConnector.setSecret(\n\t\t\tAccountHelper.buildMnemonicKey(identity, accountConfig?.vaultMnemonicId),\n\t\t\tmnemonicToStore\n\t\t);\n\n\t\tconst seed = Bip39.mnemonicToSeed(mnemonicToStore);\n\t\tawait vaultConnector.setSecret(\n\t\t\tAccountHelper.buildSeedKey(identity, accountConfig?.vaultSeedId),\n\t\t\tConverter.bytesToBase64(seed)\n\t\t);\n\n\t\tawait AccountHelper.getPublicKeys(\n\t\t\taccountConfig,\n\t\t\tvaultConnector,\n\t\t\tidentity,\n\t\t\taccountIndex ?? 0,\n\t\t\tfalse,\n\t\t\t0,\n\t\t\tasync () => seed\n\t\t);\n\n\t\treturn mnemonicToStore;\n\t}\n\n\t/**\n\t * Rename all vault entries for an account from one identity to another.\n\t * @param accountConfig The account configuration.\n\t * @param vaultConnector The vault connector.\n\t * @param fromIdentity The source identity whose vault entries should be copied.\n\t * @param toIdentity The destination identity that will receive the copied entries.\n\t */\n\tpublic static async renameAccountKeys(\n\t\taccountConfig: IAccountConfig | undefined,\n\t\tvaultConnector: IVaultConnector,\n\t\tfromIdentity: string,\n\t\ttoIdentity: string\n\t): Promise<void> {\n\t\tGuards.object(AccountHelper.CLASS_NAME, nameof(vaultConnector), vaultConnector);\n\t\tGuards.stringValue(AccountHelper.CLASS_NAME, nameof(fromIdentity), fromIdentity);\n\t\tGuards.stringValue(AccountHelper.CLASS_NAME, nameof(toIdentity), toIdentity);\n\n\t\tif (fromIdentity === toIdentity) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst mnemonicKey = AccountHelper.buildMnemonicKey(\n\t\t\tfromIdentity,\n\t\t\taccountConfig?.vaultMnemonicId\n\t\t);\n\t\tconst mnemonic = await vaultConnector.getSecret<string>(mnemonicKey);\n\t\tawait vaultConnector.setSecret(\n\t\t\tAccountHelper.buildMnemonicKey(toIdentity, accountConfig?.vaultMnemonicId),\n\t\t\tmnemonic\n\t\t);\n\t\tawait vaultConnector.removeSecret(mnemonicKey);\n\n\t\ttry {\n\t\t\tconst seedKey = AccountHelper.buildSeedKey(fromIdentity, accountConfig?.vaultSeedId);\n\t\t\tconst seed = await vaultConnector.getSecret<string>(seedKey);\n\t\t\tawait vaultConnector.setSecret(\n\t\t\t\tAccountHelper.buildSeedKey(toIdentity, accountConfig?.vaultSeedId),\n\t\t\t\tseed\n\t\t\t);\n\t\t\tawait vaultConnector.removeSecret(seedKey);\n\t\t} catch {}\n\n\t\tfor (let accountIndex = 0; ; accountIndex++) {\n\t\t\tlet accountHasKeys = false;\n\n\t\t\tfor (const internal of [false, true]) {\n\t\t\t\tfor (let chunkStart = 0; ; chunkStart += AccountHelper.DEFAULT_CALC_CHUNK_SIZE) {\n\t\t\t\t\tconst firstKeyName = AccountHelper.buildAddressKeyName(\n\t\t\t\t\t\tfromIdentity,\n\t\t\t\t\t\taccountIndex,\n\t\t\t\t\t\tinternal,\n\t\t\t\t\t\tchunkStart\n\t\t\t\t\t);\n\t\t\t\t\tif (!(await vaultConnector.keyExists(firstKeyName))) {\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\taccountHasKeys = true;\n\t\t\t\t\tfor (let i = chunkStart; i < chunkStart + AccountHelper.DEFAULT_CALC_CHUNK_SIZE; i++) {\n\t\t\t\t\t\tawait vaultConnector.renameKey(\n\t\t\t\t\t\t\tAccountHelper.buildAddressKeyName(fromIdentity, accountIndex, internal, i),\n\t\t\t\t\t\t\tAccountHelper.buildAddressKeyName(toIdentity, accountIndex, internal, i)\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (!accountHasKeys) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Remove all vault entries for an account.\n\t * @param accountConfig The account configuration.\n\t * @param vaultConnector The vault connector.\n\t * @param identity The identity of the user whose vault keys should be removed.\n\t */\n\tpublic static async removeAccountKeys(\n\t\taccountConfig: IAccountConfig | undefined,\n\t\tvaultConnector: IVaultConnector,\n\t\tidentity: string\n\t): Promise<void> {\n\t\tGuards.object(AccountHelper.CLASS_NAME, nameof(vaultConnector), vaultConnector);\n\t\tGuards.stringValue(AccountHelper.CLASS_NAME, nameof(identity), identity);\n\n\t\tfor (let accountIndex = 0; ; accountIndex++) {\n\t\t\tlet accountHasKeys = false;\n\n\t\t\tfor (const internal of [false, true]) {\n\t\t\t\tfor (let chunkStart = 0; ; chunkStart += AccountHelper.DEFAULT_CALC_CHUNK_SIZE) {\n\t\t\t\t\tconst firstKeyName = AccountHelper.buildAddressKeyName(\n\t\t\t\t\t\tidentity,\n\t\t\t\t\t\taccountIndex,\n\t\t\t\t\t\tinternal,\n\t\t\t\t\t\tchunkStart\n\t\t\t\t\t);\n\t\t\t\t\tif (!(await vaultConnector.keyExists(firstKeyName))) {\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\taccountHasKeys = true;\n\t\t\t\t\tfor (let i = chunkStart; i < chunkStart + AccountHelper.DEFAULT_CALC_CHUNK_SIZE; i++) {\n\t\t\t\t\t\tawait vaultConnector.removeKey(\n\t\t\t\t\t\t\tAccountHelper.buildAddressKeyName(identity, accountIndex, internal, i)\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (!accountHasKeys) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\ttry {\n\t\t\tawait vaultConnector.removeSecret(\n\t\t\t\tAccountHelper.buildSeedKey(identity, accountConfig?.vaultSeedId)\n\t\t\t);\n\t\t} catch {}\n\t\ttry {\n\t\t\tawait vaultConnector.removeSecret(\n\t\t\t\tAccountHelper.buildMnemonicKey(identity, accountConfig?.vaultMnemonicId)\n\t\t\t);\n\t\t} catch {}\n\t}\n\n\t/**\n\t * Get the key for storing the seed.\n\t * @param identity The identity to use.\n\t * @param vaultSeedId The seed ID to use.\n\t * @returns The seed key.\n\t */\n\tpublic static buildSeedKey(identity: string, vaultSeedId?: string): string {\n\t\treturn VaultConnectorHelper.buildKeyName(\n\t\t\tidentity,\n\t\t\tvaultSeedId ?? AccountHelper.DEFAULT_SEED_SECRET_NAME\n\t\t);\n\t}\n\n\t/**\n\t * Get the key for storing the mnemonic.\n\t * @param identity The identity to use.\n\t * @param vaultMnemonicId The mnemonic ID to use.\n\t * @returns The mnemonic key.\n\t */\n\tpublic static buildMnemonicKey(identity: string, vaultMnemonicId?: string): string {\n\t\treturn VaultConnectorHelper.buildKeyName(\n\t\t\tidentity,\n\t\t\tvaultMnemonicId ?? AccountHelper.DEFAULT_MNEMONIC_SECRET_NAME\n\t\t);\n\t}\n\n\t/**\n\t * Ensure a range of BIP44-derived keys are registered as individual vault keys and return their public keys.\n\t * If the first key of the range already exists the range is considered registered and only public keys are derived.\n\t * If not registered, all keys are derived and added to the vault before returning the public keys.\n\t * @param accountConfig The account configuration.\n\t * @param vaultConnector The vault connector to use.\n\t * @param identity The identity of the user to access the vault keys.\n\t * @param accountIndex The account index.\n\t * @param internal Whether the addresses are internal or external.\n\t * @param addressIndex Any address index within the desired chunk; aligned internally.\n\t * @param seedProvider Callback invoked at most once per call to supply the seed when a chunk is not yet registered.\n\t * @returns The base64-encoded public keys for each address in the range.\n\t */\n\tpublic static async getPublicKeys(\n\t\taccountConfig: IAccountConfig | undefined,\n\t\tvaultConnector: IVaultConnector,\n\t\tidentity: string,\n\t\taccountIndex: number,\n\t\tinternal: boolean,\n\t\taddressIndex: number,\n\t\tseedProvider: () => Promise<Uint8Array>\n\t): Promise<string[]> {\n\t\tconst chunkStart = addressIndex - (addressIndex % AccountHelper.DEFAULT_CALC_CHUNK_SIZE);\n\t\tconst firstKeyName = AccountHelper.buildAddressKeyName(\n\t\t\tidentity,\n\t\t\taccountIndex,\n\t\t\tinternal,\n\t\t\tchunkStart\n\t\t);\n\t\tconst publicKeys: string[] = [];\n\n\t\tif (await vaultConnector.keyExists(firstKeyName)) {\n\t\t\tfor (let i = chunkStart; i < chunkStart + AccountHelper.DEFAULT_CALC_CHUNK_SIZE; i++) {\n\t\t\t\tconst keyName = AccountHelper.buildAddressKeyName(identity, accountIndex, internal, i);\n\t\t\t\tconst keyData = await vaultConnector.getKey(keyName, \"public\");\n\t\t\t\tif (!keyData.publicKey) {\n\t\t\t\t\tthrow new GeneralError(AccountHelper.CLASS_NAME, \"missingPublicKey\", { keyName });\n\t\t\t\t}\n\t\t\t\tpublicKeys.push(Converter.bytesToBase64(keyData.publicKey));\n\t\t\t}\n\t\t} else {\n\t\t\tconst seed = await seedProvider();\n\t\t\tconst coinType = accountConfig?.coinType ?? AccountHelper.DEFAULT_COIN_TYPE;\n\n\t\t\tfor (let i = chunkStart; i < chunkStart + AccountHelper.DEFAULT_CALC_CHUNK_SIZE; i++) {\n\t\t\t\tconst keyName = AccountHelper.buildAddressKeyName(identity, accountIndex, internal, i);\n\t\t\t\tconst keyPair = Bip44.keyPair(seed, KeyType.Ed25519, coinType, accountIndex, internal, i);\n\t\t\t\tawait vaultConnector.addKey(\n\t\t\t\t\tkeyName,\n\t\t\t\t\tVaultKeyType.Ed25519,\n\t\t\t\t\tkeyPair.privateKey,\n\t\t\t\t\tkeyPair.publicKey\n\t\t\t\t);\n\t\t\t\tpublicKeys.push(Converter.bytesToBase64(keyPair.publicKey));\n\t\t\t}\n\t\t}\n\n\t\treturn publicKeys;\n\t}\n\n\t/**\n\t * Build the vault key name for a specific derived address.\n\t * @param identity The identity to use.\n\t * @param accountIndex The account index.\n\t * @param internal Whether the address is internal or external.\n\t * @param addressIndex The address index.\n\t * @returns The vault key name.\n\t */\n\tpublic static buildAddressKeyName(\n\t\tidentity: string,\n\t\taccountIndex: number,\n\t\tinternal: boolean,\n\t\taddressIndex: number\n\t): string {\n\t\treturn VaultConnectorHelper.buildKeyName(\n\t\t\tidentity,\n\t\t\t\"account\",\n\t\t\taccountIndex.toString(),\n\t\t\tinternal ? \"1\" : \"0\",\n\t\t\t`${addressIndex}`\n\t\t);\n\t}\n\n\t/**\n\t * Get address for the identity.\n\t * @param accountConfig The account configuration.\n\t * @param vaultConnector The vault connector.\n\t * @param identity The identity of the user to access the vault keys.\n\t * @param accountIndex The account index to get the addresses for.\n\t * @param startAddressIndex The start index for the addresses.\n\t * @param isInternal Whether the addresses are internal.\n\t * @returns The address.\n\t */\n\tpublic static async getAddress(\n\t\taccountConfig: IAccountConfig | undefined,\n\t\tvaultConnector: IVaultConnector,\n\t\tidentity: string,\n\t\taccountIndex: number,\n\t\tstartAddressIndex: number,\n\t\tisInternal?: boolean\n\t): Promise<string> {\n\t\tconst addresses = await AccountHelper.getAddresses(\n\t\t\taccountConfig,\n\t\t\tvaultConnector,\n\t\t\tidentity,\n\t\t\taccountIndex,\n\t\t\tstartAddressIndex,\n\t\t\t1,\n\t\t\tisInternal\n\t\t);\n\t\treturn addresses[0];\n\t}\n\n\t/**\n\t * Get addresses for the identity.\n\t * @param accountConfig The account configuration.\n\t * @param vaultConnector The vault connector.\n\t * @param identity The identity of the user to access the vault keys.\n\t * @param accountIndex The account index to get the addresses for.\n\t * @param startAddressIndex The start index for the addresses.\n\t * @param count The number of addresses to generate.\n\t * @param isInternal Whether the addresses are internal.\n\t * @returns The list of addresses.\n\t */\n\tpublic static async getAddresses(\n\t\taccountConfig: IAccountConfig | undefined,\n\t\tvaultConnector: IVaultConnector,\n\t\tidentity: string,\n\t\taccountIndex: number,\n\t\tstartAddressIndex: number,\n\t\tcount: number,\n\t\tisInternal?: boolean\n\t): Promise<string[]> {\n\t\tGuards.object(AccountHelper.CLASS_NAME, nameof(vaultConnector), vaultConnector);\n\t\tGuards.stringValue(AccountHelper.CLASS_NAME, nameof(identity), identity);\n\t\tGuards.integer(AccountHelper.CLASS_NAME, nameof(accountIndex), accountIndex);\n\t\tGuards.integer(AccountHelper.CLASS_NAME, nameof(startAddressIndex), startAddressIndex);\n\t\tGuards.integer(AccountHelper.CLASS_NAME, nameof(count), count);\n\n\t\tconst addresses: string[] = [];\n\t\tconst internal = isInternal ?? false;\n\t\tlet currentIndex = startAddressIndex;\n\t\tlet cachedSeed: Uint8Array | undefined;\n\t\tconst seedProvider = async (): Promise<Uint8Array> => {\n\t\t\tcachedSeed ??= await AccountHelper.getSeed(accountConfig, vaultConnector, identity);\n\t\t\treturn cachedSeed;\n\t\t};\n\n\t\twhile (addresses.length < count) {\n\t\t\tconst chunkStart =\n\t\t\t\tMath.floor(currentIndex / AccountHelper.DEFAULT_CALC_CHUNK_SIZE) *\n\t\t\t\tAccountHelper.DEFAULT_CALC_CHUNK_SIZE;\n\t\t\tconst publicKeys = await AccountHelper.getPublicKeys(\n\t\t\t\taccountConfig,\n\t\t\t\tvaultConnector,\n\t\t\t\tidentity,\n\t\t\t\taccountIndex,\n\t\t\t\tinternal,\n\t\t\t\tchunkStart,\n\t\t\t\tseedProvider\n\t\t\t);\n\t\t\tconst chunk = publicKeys.map((pk: string) =>\n\t\t\t\tAccountHelper.publicKeyToAddress(Converter.base64ToBytes(pk))\n\t\t\t);\n\t\t\tconst offsetInChunk = currentIndex - chunkStart;\n\t\t\tconst remaining = count - addresses.length;\n\t\t\taddresses.push(...chunk.slice(offsetInChunk, offsetInChunk + remaining));\n\t\t\tcurrentIndex = chunkStart + AccountHelper.DEFAULT_CALC_CHUNK_SIZE;\n\t\t}\n\n\t\treturn addresses;\n\t}\n\n\t/**\n\t * Get the seed from the vault, deriving it from the mnemonic if necessary.\n\t * @param accountConfig The account configuration.\n\t * @param vaultConnector The vault connector to use.\n\t * @param identity The identity of the user to access the vault keys.\n\t * @returns The seed bytes.\n\t */\n\tpublic static async getSeed(\n\t\taccountConfig: IAccountConfig | undefined,\n\t\tvaultConnector: IVaultConnector,\n\t\tidentity: string\n\t): Promise<Uint8Array> {\n\t\tconst seedKey = AccountHelper.buildSeedKey(identity, accountConfig?.vaultSeedId);\n\n\t\ttry {\n\t\t\tconst seedBase64 = await vaultConnector.getSecret<string>(seedKey);\n\t\t\treturn Converter.base64ToBytes(seedBase64);\n\t\t} catch {}\n\n\t\tconst mnemonic = await vaultConnector.getSecret<string>(\n\t\t\tAccountHelper.buildMnemonicKey(identity, accountConfig?.vaultMnemonicId)\n\t\t);\n\n\t\t// If the seed is not found but the mnemonic exists, derive the seed and store it for future use\n\t\tconst seed = Bip39.mnemonicToSeed(mnemonic);\n\t\tawait vaultConnector.setSecret(seedKey, Converter.bytesToBase64(seed));\n\n\t\treturn seed;\n\t}\n\n\t/**\n\t * Find the vault key name and public key for a specific address by scanning derived keys.\n\t * @param accountConfig The account configuration.\n\t * @param vaultConnector The vault connector to use.\n\t * @param identity The identity of the user to access the vault keys.\n\t * @param address The owner address whose key should be located.\n\t * @param accountIndex The account index to search.\n\t * @returns The vault key name and the public key bytes.\n\t */\n\tpublic static async findAddressKey(\n\t\taccountConfig: IAccountConfig | undefined,\n\t\tvaultConnector: IVaultConnector,\n\t\tidentity: string,\n\t\taddress: string,\n\t\taccountIndex: number = 0\n\t): Promise<{ keyName: string; publicKey: Uint8Array }> {\n\t\tconst scanRange = accountConfig?.maxAddressScanRange ?? AccountHelper.DEFAULT_SCAN_RANGE_SIZE;\n\t\tlet cachedSeed: Uint8Array | undefined;\n\t\tconst seedProvider = async (): Promise<Uint8Array> => {\n\t\t\tcachedSeed ??= await AccountHelper.getSeed(accountConfig, vaultConnector, identity);\n\t\t\treturn cachedSeed;\n\t\t};\n\n\t\tfor (\n\t\t\tlet chunkStart = 0;\n\t\t\tchunkStart < scanRange;\n\t\t\tchunkStart += AccountHelper.DEFAULT_CALC_CHUNK_SIZE\n\t\t) {\n\t\t\tconst publicKeys = await AccountHelper.getPublicKeys(\n\t\t\t\taccountConfig,\n\t\t\t\tvaultConnector,\n\t\t\t\tidentity,\n\t\t\t\taccountIndex,\n\t\t\t\tfalse,\n\t\t\t\tchunkStart,\n\t\t\t\tseedProvider\n\t\t\t);\n\t\t\tconst matchIndex = publicKeys.findIndex(\n\t\t\t\t(pk: string) => AccountHelper.publicKeyToAddress(Converter.base64ToBytes(pk)) === address\n\t\t\t);\n\t\t\tif (matchIndex >= 0) {\n\t\t\t\tconst addressIndex = chunkStart + matchIndex;\n\t\t\t\treturn {\n\t\t\t\t\tkeyName: AccountHelper.buildAddressKeyName(identity, accountIndex, false, addressIndex),\n\t\t\t\t\tpublicKey: Converter.base64ToBytes(publicKeys[matchIndex])\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\n\t\tthrow new GeneralError(AccountHelper.CLASS_NAME, \"addressNotFound\", { address });\n\t}\n\n\t/**\n\t * Derive an address from a public key.\n\t * @param publicKey The public key to derive the address from.\n\t * @returns The derived address.\n\t */\n\tpublic static publicKeyToAddress(publicKey: Uint8Array): string {\n\t\treturn Converter.bytesToHex(Blake2b.sum256(publicKey), true);\n\t}\n}\n"]}
|
package/dist/es/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,4BAA4B,CAAC","sourcesContent":["// Copyright 2026 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nexport * from \"./helpers/accountHelper.js\";\nexport * from \"./models/IAccountConfig.js\";\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IAccountConfig.js","sourceRoot":"","sources":["../../../src/models/IAccountConfig.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC","sourcesContent":["// Copyright 2026 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n\n/**\n * The configuration for an account.\n */\nexport interface IAccountConfig {\n\t/**\n\t * The ID of the vault seed.\n\t */\n\tvaultSeedId?: string;\n\n\t/**\n\t * The ID of the vault mnemonic.\n\t */\n\tvaultMnemonicId?: string;\n\n\t/**\n\t * The coin type.\n\t */\n\tcoinType?: number;\n\n\t/**\n\t * The maximum number of addresses to scan for the account.\n\t */\n\tmaxAddressScanRange?: number;\n}\n"]}
|