-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsetupDashClient.mjs
More file actions
118 lines (102 loc) · 3.6 KB
/
setupDashClient.mjs
File metadata and controls
118 lines (102 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//
// Node-only convenience wrapper around setupDashClient-core.mjs.
//
// This file adds the bits that only make sense in Node tutorials:
// - dotenv loading (PLATFORM_MNEMONIC / NETWORK from .env)
// - clientConfig (network + mnemonic, sourced from process.env)
// - setupDashClient() — the one-call wrapper used by tutorial scripts
//
// Everything browser-safe (createClient, IdentityKeyManager, AddressKeyManager,
// dip13KeyPath, KEY_SPECS) is re-exported from setupDashClient-core.mjs so a
// Node tutorial that imports from this file sees an identical API surface.
//
// Browser apps should import from setupDashClient-core.mjs directly.
//
import { wallet } from '@dashevo/evo-sdk';
import {
createClient,
dip13KeyPath,
KEY_SPECS,
IdentityKeyManager,
AddressKeyManager,
} from './setupDashClient-core.mjs';
// Load .env if dotenv is installed (optional — not needed for tutorials).
// Top-level await requires ESM — .mjs extension ensures this.
// eslint-disable-next-line import/no-extraneous-dependencies
try {
const { config } = await import('dotenv');
config();
} catch {
/* dotenv not installed */
}
/** @typedef {import('@dashevo/evo-sdk').EvoSDK} EvoSDK */
// ⚠️ Tutorial helper — holds WIFs in memory for convenience.
// Do not use this pattern as-is for production key management.
// ###########################################################################
// # CONFIGURATION — edit these values for your environment #
// ###########################################################################
// Option 1: Edit the values below directly
// Option 2: Create a .env file with PLATFORM_MNEMONIC and NETWORK
const clientConfig = {
// The network to connect to ('testnet' or 'mainnet')
network: process.env.NETWORK || 'testnet',
// BIP39 mnemonic for wallet operations (identity & address tutorials).
// Leave as null for read-only tutorials.
mnemonic: process.env.PLATFORM_MNEMONIC || null,
// mnemonic: 'your twelve word mnemonic phrase goes here ...',
};
// ---------------------------------------------------------------------------
// setupDashClient — convenience wrapper
// ---------------------------------------------------------------------------
/**
* @param {{requireIdentity?: boolean, identityIndex?: number}} opts
* @returns {Promise<{ sdk: EvoSDK, keyManager: IdentityKeyManager | undefined, addressKeyManager: AddressKeyManager | undefined }>}
*/
export async function setupDashClient({
requireIdentity = true,
identityIndex = undefined,
} = {}) {
const { network, mnemonic } = clientConfig;
if (mnemonic && !(await wallet.validateMnemonic(mnemonic))) {
throw new Error(
'PLATFORM_MNEMONIC is not a valid BIP39 mnemonic. ' +
'Run `node create-wallet.mjs` to generate one.',
);
}
const sdk = await createClient(network);
let keyManager;
let addressKeyManager;
if (mnemonic) {
addressKeyManager = await AddressKeyManager.create({
sdk,
mnemonic,
network,
});
if (requireIdentity) {
keyManager = await IdentityKeyManager.create({
sdk,
mnemonic,
network,
identityIndex,
});
} else {
keyManager = await IdentityKeyManager.createForNewIdentity({
sdk,
mnemonic,
network,
identityIndex,
});
}
}
return { sdk, keyManager, addressKeyManager };
}
// Re-export everything from the core so existing imports
// (e.g. `import { IdentityKeyManager } from './setupDashClient.mjs'`) keep working.
export {
createClient,
dip13KeyPath,
KEY_SPECS,
IdentityKeyManager,
AddressKeyManager,
clientConfig,
};