@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
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import type { IVaultConnector } from "@twin.org/vault-models";
|
|
2
|
+
import type { IAccountConfig } from "../models/IAccountConfig.js";
|
|
3
|
+
/**
|
|
4
|
+
* Helper class for account management operations.
|
|
5
|
+
*/
|
|
6
|
+
export declare class AccountHelper {
|
|
7
|
+
/**
|
|
8
|
+
* Runtime name for the class.
|
|
9
|
+
*/
|
|
10
|
+
static readonly CLASS_NAME: string;
|
|
11
|
+
/**
|
|
12
|
+
* Default name for the mnemonic secret.
|
|
13
|
+
*/
|
|
14
|
+
static readonly DEFAULT_MNEMONIC_SECRET_NAME: string;
|
|
15
|
+
/**
|
|
16
|
+
* Default name for the seed secret.
|
|
17
|
+
*/
|
|
18
|
+
static readonly DEFAULT_SEED_SECRET_NAME: string;
|
|
19
|
+
/**
|
|
20
|
+
* Default coin type.
|
|
21
|
+
*/
|
|
22
|
+
static readonly DEFAULT_COIN_TYPE: number;
|
|
23
|
+
/**
|
|
24
|
+
* Default pre-calculation chunk size.
|
|
25
|
+
*/
|
|
26
|
+
static readonly DEFAULT_CALC_CHUNK_SIZE: number;
|
|
27
|
+
/**
|
|
28
|
+
* Default scan range.
|
|
29
|
+
*/
|
|
30
|
+
static readonly DEFAULT_SCAN_RANGE_SIZE: number;
|
|
31
|
+
/**
|
|
32
|
+
* Create a new account by generating a mnemonic and seed, storing them in the vault, and pre-caching the first chunk of derived keys.
|
|
33
|
+
* @param accountConfig The account configuration.
|
|
34
|
+
* @param vaultConnector The vault connector.
|
|
35
|
+
* @param identity The identity of the user to access the vault keys.
|
|
36
|
+
* @param mnemonic The mnemonic to store, if undefined a new one will be generated and returned.
|
|
37
|
+
* @param accountIndex The account index to pre-cache.
|
|
38
|
+
* @returns The mnemonic that was stored.
|
|
39
|
+
*/
|
|
40
|
+
static createAccountKeys(accountConfig: IAccountConfig | undefined, vaultConnector: IVaultConnector, identity: string, mnemonic?: string, accountIndex?: number): Promise<string>;
|
|
41
|
+
/**
|
|
42
|
+
* Rename all vault entries for an account from one identity to another.
|
|
43
|
+
* @param accountConfig The account configuration.
|
|
44
|
+
* @param vaultConnector The vault connector.
|
|
45
|
+
* @param fromIdentity The source identity whose vault entries should be copied.
|
|
46
|
+
* @param toIdentity The destination identity that will receive the copied entries.
|
|
47
|
+
*/
|
|
48
|
+
static renameAccountKeys(accountConfig: IAccountConfig | undefined, vaultConnector: IVaultConnector, fromIdentity: string, toIdentity: string): Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* Remove all vault entries for an account.
|
|
51
|
+
* @param accountConfig The account configuration.
|
|
52
|
+
* @param vaultConnector The vault connector.
|
|
53
|
+
* @param identity The identity of the user whose vault keys should be removed.
|
|
54
|
+
*/
|
|
55
|
+
static removeAccountKeys(accountConfig: IAccountConfig | undefined, vaultConnector: IVaultConnector, identity: string): Promise<void>;
|
|
56
|
+
/**
|
|
57
|
+
* Get the key for storing the seed.
|
|
58
|
+
* @param identity The identity to use.
|
|
59
|
+
* @param vaultSeedId The seed ID to use.
|
|
60
|
+
* @returns The seed key.
|
|
61
|
+
*/
|
|
62
|
+
static buildSeedKey(identity: string, vaultSeedId?: string): string;
|
|
63
|
+
/**
|
|
64
|
+
* Get the key for storing the mnemonic.
|
|
65
|
+
* @param identity The identity to use.
|
|
66
|
+
* @param vaultMnemonicId The mnemonic ID to use.
|
|
67
|
+
* @returns The mnemonic key.
|
|
68
|
+
*/
|
|
69
|
+
static buildMnemonicKey(identity: string, vaultMnemonicId?: string): string;
|
|
70
|
+
/**
|
|
71
|
+
* Ensure a range of BIP44-derived keys are registered as individual vault keys and return their public keys.
|
|
72
|
+
* If the first key of the range already exists the range is considered registered and only public keys are derived.
|
|
73
|
+
* If not registered, all keys are derived and added to the vault before returning the public keys.
|
|
74
|
+
* @param accountConfig The account configuration.
|
|
75
|
+
* @param vaultConnector The vault connector to use.
|
|
76
|
+
* @param identity The identity of the user to access the vault keys.
|
|
77
|
+
* @param accountIndex The account index.
|
|
78
|
+
* @param internal Whether the addresses are internal or external.
|
|
79
|
+
* @param addressIndex Any address index within the desired chunk; aligned internally.
|
|
80
|
+
* @param seedProvider Callback invoked at most once per call to supply the seed when a chunk is not yet registered.
|
|
81
|
+
* @returns The base64-encoded public keys for each address in the range.
|
|
82
|
+
*/
|
|
83
|
+
static getPublicKeys(accountConfig: IAccountConfig | undefined, vaultConnector: IVaultConnector, identity: string, accountIndex: number, internal: boolean, addressIndex: number, seedProvider: () => Promise<Uint8Array>): Promise<string[]>;
|
|
84
|
+
/**
|
|
85
|
+
* Build the vault key name for a specific derived address.
|
|
86
|
+
* @param identity The identity to use.
|
|
87
|
+
* @param accountIndex The account index.
|
|
88
|
+
* @param internal Whether the address is internal or external.
|
|
89
|
+
* @param addressIndex The address index.
|
|
90
|
+
* @returns The vault key name.
|
|
91
|
+
*/
|
|
92
|
+
static buildAddressKeyName(identity: string, accountIndex: number, internal: boolean, addressIndex: number): string;
|
|
93
|
+
/**
|
|
94
|
+
* Get address for the identity.
|
|
95
|
+
* @param accountConfig The account configuration.
|
|
96
|
+
* @param vaultConnector The vault connector.
|
|
97
|
+
* @param identity The identity of the user to access the vault keys.
|
|
98
|
+
* @param accountIndex The account index to get the addresses for.
|
|
99
|
+
* @param startAddressIndex The start index for the addresses.
|
|
100
|
+
* @param isInternal Whether the addresses are internal.
|
|
101
|
+
* @returns The address.
|
|
102
|
+
*/
|
|
103
|
+
static getAddress(accountConfig: IAccountConfig | undefined, vaultConnector: IVaultConnector, identity: string, accountIndex: number, startAddressIndex: number, isInternal?: boolean): Promise<string>;
|
|
104
|
+
/**
|
|
105
|
+
* Get addresses for the identity.
|
|
106
|
+
* @param accountConfig The account configuration.
|
|
107
|
+
* @param vaultConnector The vault connector.
|
|
108
|
+
* @param identity The identity of the user to access the vault keys.
|
|
109
|
+
* @param accountIndex The account index to get the addresses for.
|
|
110
|
+
* @param startAddressIndex The start index for the addresses.
|
|
111
|
+
* @param count The number of addresses to generate.
|
|
112
|
+
* @param isInternal Whether the addresses are internal.
|
|
113
|
+
* @returns The list of addresses.
|
|
114
|
+
*/
|
|
115
|
+
static getAddresses(accountConfig: IAccountConfig | undefined, vaultConnector: IVaultConnector, identity: string, accountIndex: number, startAddressIndex: number, count: number, isInternal?: boolean): Promise<string[]>;
|
|
116
|
+
/**
|
|
117
|
+
* Get the seed from the vault, deriving it from the mnemonic if necessary.
|
|
118
|
+
* @param accountConfig The account configuration.
|
|
119
|
+
* @param vaultConnector The vault connector to use.
|
|
120
|
+
* @param identity The identity of the user to access the vault keys.
|
|
121
|
+
* @returns The seed bytes.
|
|
122
|
+
*/
|
|
123
|
+
static getSeed(accountConfig: IAccountConfig | undefined, vaultConnector: IVaultConnector, identity: string): Promise<Uint8Array>;
|
|
124
|
+
/**
|
|
125
|
+
* Find the vault key name and public key for a specific address by scanning derived keys.
|
|
126
|
+
* @param accountConfig The account configuration.
|
|
127
|
+
* @param vaultConnector The vault connector to use.
|
|
128
|
+
* @param identity The identity of the user to access the vault keys.
|
|
129
|
+
* @param address The owner address whose key should be located.
|
|
130
|
+
* @param accountIndex The account index to search.
|
|
131
|
+
* @returns The vault key name and the public key bytes.
|
|
132
|
+
*/
|
|
133
|
+
static findAddressKey(accountConfig: IAccountConfig | undefined, vaultConnector: IVaultConnector, identity: string, address: string, accountIndex?: number): Promise<{
|
|
134
|
+
keyName: string;
|
|
135
|
+
publicKey: Uint8Array;
|
|
136
|
+
}>;
|
|
137
|
+
/**
|
|
138
|
+
* Derive an address from a public key.
|
|
139
|
+
* @param publicKey The public key to derive the address from.
|
|
140
|
+
* @returns The derived address.
|
|
141
|
+
*/
|
|
142
|
+
static publicKeyToAddress(publicKey: Uint8Array): string;
|
|
143
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The configuration for an account.
|
|
3
|
+
*/
|
|
4
|
+
export interface IAccountConfig {
|
|
5
|
+
/**
|
|
6
|
+
* The ID of the vault seed.
|
|
7
|
+
*/
|
|
8
|
+
vaultSeedId?: string;
|
|
9
|
+
/**
|
|
10
|
+
* The ID of the vault mnemonic.
|
|
11
|
+
*/
|
|
12
|
+
vaultMnemonicId?: string;
|
|
13
|
+
/**
|
|
14
|
+
* The coin type.
|
|
15
|
+
*/
|
|
16
|
+
coinType?: number;
|
|
17
|
+
/**
|
|
18
|
+
* The maximum number of addresses to scan for the account.
|
|
19
|
+
*/
|
|
20
|
+
maxAddressScanRange?: number;
|
|
21
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [0.9.2-next.2](https://github.com/iotaledger/twin-dlt/compare/dlt-account-v0.9.2-next.1...dlt-account-v0.9.2-next.2) (2026-08-07)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* linting and dependency update ([6eb1848](https://github.com/iotaledger/twin-dlt/commit/6eb18480bb1b4608e3590b7e03f5f525ea2e0952))
|
|
9
|
+
|
|
10
|
+
## Changelog
|
package/docs/examples.md
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
# DLT Account Examples
|
|
2
|
+
|
|
3
|
+
These examples show how to create, query, and manage distributed ledger accounts through a vault connector, covering the full lifecycle from creation to removal.
|
|
4
|
+
|
|
5
|
+
## AccountHelper
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
import type { IVaultConnector } from '@twin.org/vault-models';
|
|
9
|
+
import { AccountHelper } from '@twin.org/dlt-account';
|
|
10
|
+
|
|
11
|
+
declare const vaultConnector: IVaultConnector;
|
|
12
|
+
const identity = 'did:example:alice';
|
|
13
|
+
|
|
14
|
+
// Generate a fresh mnemonic automatically, store it with its derived seed,
|
|
15
|
+
// and pre-cache the first chunk of BIP44 address keys for account index 0.
|
|
16
|
+
const mnemonic = await AccountHelper.createAccountKeys(undefined, vaultConnector, identity);
|
|
17
|
+
|
|
18
|
+
// Or import a known mnemonic for an existing account.
|
|
19
|
+
const existingMnemonic =
|
|
20
|
+
'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about';
|
|
21
|
+
const stored = await AccountHelper.createAccountKeys(
|
|
22
|
+
undefined,
|
|
23
|
+
vaultConnector,
|
|
24
|
+
'did:example:bob',
|
|
25
|
+
existingMnemonic
|
|
26
|
+
);
|
|
27
|
+
console.log(stored === existingMnemonic); // true
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import type { IVaultConnector } from '@twin.org/vault-models';
|
|
32
|
+
import { AccountHelper } from '@twin.org/dlt-account';
|
|
33
|
+
|
|
34
|
+
declare const vaultConnector: IVaultConnector;
|
|
35
|
+
const identity = 'did:example:alice';
|
|
36
|
+
|
|
37
|
+
// Retrieve three consecutive external addresses starting at index 0.
|
|
38
|
+
const addresses = await AccountHelper.getAddresses(undefined, vaultConnector, identity, 0, 0, 3);
|
|
39
|
+
console.log(addresses.length); // 3
|
|
40
|
+
|
|
41
|
+
// Get a single external address at a known index.
|
|
42
|
+
const address = await AccountHelper.getAddress(undefined, vaultConnector, identity, 0, 0);
|
|
43
|
+
console.log(address === addresses[0]); // true
|
|
44
|
+
|
|
45
|
+
// Retrieve internal (change) addresses for account index 1.
|
|
46
|
+
const internalAddresses = await AccountHelper.getAddresses(
|
|
47
|
+
undefined,
|
|
48
|
+
vaultConnector,
|
|
49
|
+
identity,
|
|
50
|
+
1,
|
|
51
|
+
0,
|
|
52
|
+
5,
|
|
53
|
+
true
|
|
54
|
+
);
|
|
55
|
+
console.log(internalAddresses.length); // 5
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
import type { IVaultConnector } from '@twin.org/vault-models';
|
|
60
|
+
import { AccountHelper } from '@twin.org/dlt-account';
|
|
61
|
+
|
|
62
|
+
declare const vaultConnector: IVaultConnector;
|
|
63
|
+
const identity = 'did:example:alice';
|
|
64
|
+
|
|
65
|
+
// Generate addresses then find the vault key for a specific one.
|
|
66
|
+
const addresses = await AccountHelper.getAddresses(undefined, vaultConnector, identity, 0, 0, 5);
|
|
67
|
+
const { keyName, publicKey } = await AccountHelper.findAddressKey(
|
|
68
|
+
undefined,
|
|
69
|
+
vaultConnector,
|
|
70
|
+
identity,
|
|
71
|
+
addresses[2]
|
|
72
|
+
);
|
|
73
|
+
console.log(keyName); // "did:example:alice/account/0/0/2"
|
|
74
|
+
console.log(AccountHelper.publicKeyToAddress(publicKey) === addresses[2]); // true
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
import type { IVaultConnector } from '@twin.org/vault-models';
|
|
79
|
+
import { AccountHelper } from '@twin.org/dlt-account';
|
|
80
|
+
|
|
81
|
+
declare const vaultConnector: IVaultConnector;
|
|
82
|
+
|
|
83
|
+
// Move all vault entries for an identity from one DID to another.
|
|
84
|
+
await AccountHelper.renameAccountKeys(
|
|
85
|
+
undefined,
|
|
86
|
+
vaultConnector,
|
|
87
|
+
'did:example:old-identity',
|
|
88
|
+
'did:example:new-identity'
|
|
89
|
+
);
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
import type { IVaultConnector } from '@twin.org/vault-models';
|
|
94
|
+
import { AccountHelper } from '@twin.org/dlt-account';
|
|
95
|
+
|
|
96
|
+
declare const vaultConnector: IVaultConnector;
|
|
97
|
+
const identity = 'did:example:alice';
|
|
98
|
+
|
|
99
|
+
// Remove all vault entries for the identity: mnemonic, seed, and all derived keys.
|
|
100
|
+
await AccountHelper.removeAccountKeys(undefined, vaultConnector, identity);
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
import { Converter } from '@twin.org/core';
|
|
105
|
+
import type { IVaultConnector } from '@twin.org/vault-models';
|
|
106
|
+
import { AccountHelper } from '@twin.org/dlt-account';
|
|
107
|
+
|
|
108
|
+
declare const vaultConnector: IVaultConnector;
|
|
109
|
+
const identity = 'did:example:alice';
|
|
110
|
+
|
|
111
|
+
// Retrieve the seed bytes, deriving from the mnemonic if not yet stored.
|
|
112
|
+
const seed = await AccountHelper.getSeed(undefined, vaultConnector, identity);
|
|
113
|
+
console.log(seed instanceof Uint8Array); // true
|
|
114
|
+
|
|
115
|
+
// Derive and cache a chunk of 25 public keys starting at address index 0.
|
|
116
|
+
const publicKeys = await AccountHelper.getPublicKeys(
|
|
117
|
+
undefined,
|
|
118
|
+
vaultConnector,
|
|
119
|
+
identity,
|
|
120
|
+
0,
|
|
121
|
+
false,
|
|
122
|
+
0,
|
|
123
|
+
async () => seed
|
|
124
|
+
);
|
|
125
|
+
console.log(publicKeys.length); // 25
|
|
126
|
+
|
|
127
|
+
// Convert a base64-encoded public key to its on-chain hex address.
|
|
128
|
+
const derivedAddress = AccountHelper.publicKeyToAddress(Converter.base64ToBytes(publicKeys[0]));
|
|
129
|
+
console.log(derivedAddress.startsWith('0x')); // true
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
import { AccountHelper } from '@twin.org/dlt-account';
|
|
134
|
+
|
|
135
|
+
const identity = 'did:example:alice';
|
|
136
|
+
|
|
137
|
+
// Build vault key names for secrets and derived address keys.
|
|
138
|
+
console.log(AccountHelper.buildSeedKey(identity)); // "did:example:alice/seed"
|
|
139
|
+
console.log(AccountHelper.buildMnemonicKey(identity)); // "did:example:alice/mnemonic"
|
|
140
|
+
console.log(AccountHelper.buildAddressKeyName(identity, 0, false, 7)); // "did:example:alice/account/0/0/7"
|
|
141
|
+
console.log(AccountHelper.buildAddressKeyName(identity, 1, true, 0)); // "did:example:alice/account/1/1/0"
|
|
142
|
+
|
|
143
|
+
// Override default key names via IAccountConfig.
|
|
144
|
+
console.log(AccountHelper.buildSeedKey(identity, 'deployment-seed')); // "did:example:alice/deployment-seed"
|
|
145
|
+
console.log(AccountHelper.buildMnemonicKey(identity, 'deployment-mnemonic')); // "did:example:alice/deployment-mnemonic"
|
|
146
|
+
```
|