Skip to content

Commit 226787d

Browse files
author
archx
committed
Generate multiple wallets imported on creation
1 parent 559b04a commit 226787d

File tree

6 files changed

+61
-39
lines changed

6 files changed

+61
-39
lines changed

lib/cli.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,15 @@ program.command('wallet-create')
209209
.description('Creates and displays new 12-word secret mnemonic phrase along with the primary and funding addresses')
210210
.action(async (options) => {
211211
const result = await Atomicals.walletCreate();
212-
console.log('Generated mnemonic phrase:');
213-
console.log(`phrase: ${result.data.phrase}`);
214-
console.log(`Primary address (P2TR): ${result.data.primary.address}`);
215-
console.log(`Primary address WIF: ${result.data.primary.WIF}`);
216-
console.log(`Primary address path: ${result.data.primary.path}`);
217-
console.log(`Funding address (P2TR): ${result.data.funding.address}`);
218-
console.log(`Funding address WIF: ${result.data.funding.WIF}`);
219-
console.log(`Funding address path: ${result.data.funding.path}`);
220-
console.log(result)
212+
console.log('Generated mnemonic phrase:', result);
213+
console.log(`phrase: ${result.data.wallet.phrase}`);
214+
console.log(`Primary address (P2TR): ${result.data.wallet.primary.address}`);
215+
console.log(`Primary address WIF: ${result.data.wallet.primary.WIF}`);
216+
console.log(`Primary address path: ${result.data.wallet.primary.path}`);
217+
console.log(`Funding address (P2TR): ${result.data.wallet.funding.address}`);
218+
console.log(`Funding address WIF: ${result.data.wallet.funding.WIF}`);
219+
console.log(`Funding address path: ${result.data.wallet.funding.path}`);
220+
console.log(JSON.stringify(result, null, 2));
221221
console.log(`------------------------------------------------------`);
222222
});
223223

@@ -242,9 +242,10 @@ program.command('wallet-init')
242242
.description('Initializes a new wallet at wallet.json')
243243
.option('--phrase <string>', 'Provide a wallet phrase')
244244
.option('--path <string>', 'Provide a path base', `m/86'/0'/0'`)
245+
.option('--n <number>', 'Provider number of alias')
245246
.action(async (options) => {
246247
try {
247-
const result = await Atomicals.walletInit(options.phrase, options.path);
248+
const result = await Atomicals.walletInit(options.phrase, options.path, options.n ? parseInt(options.n, 10) : undefined);
248249
console.log('Wallet created at wallet.json');
249250
console.log(`phrase: ${result.data.phrase}`);
250251
console.log(`Primary address (P2TR): ${result.data.primary.address}`);
@@ -253,6 +254,7 @@ program.command('wallet-init')
253254
console.log(`Funding address (P2TR): ${result.data.funding.address}`);
254255
console.log(`Funding address WIF: ${result.data.funding.WIF}`);
255256
console.log(`Funding address path: ${result.data.funding.path}`);
257+
console.log(`Full Data: ${JSON.stringify(result.data, null, 2)}`);
256258
console.log(`------------------------------------------------------`);
257259
} catch (err: any) {
258260
console.log('Error', err);

lib/commands/wallet-create-command.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { CommandResultInterface } from "./command-result.interface";
22
import { CommandInterface } from "./command.interface";
3-
import { createPrimaryAndFundingKeyPairs } from "../utils/create-key-pair";
3+
import { createPrimaryAndFundingImportedKeyPairs } from "../utils/create-key-pair";
44

55
export class WalletCreateCommand implements CommandInterface {
66
async run(): Promise<CommandResultInterface> {
7-
const keypairs = await createPrimaryAndFundingKeyPairs();
7+
const keypairs = await createPrimaryAndFundingImportedKeyPairs();
88

99
return {
1010
success: true,

lib/commands/wallet-init-command.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
import { CommandResultInterface } from "./command-result.interface";
22
import { CommandInterface } from "./command.interface";
3-
import { createPrimaryAndFundingKeyPairs } from "../utils/create-key-pair";
3+
import { createPrimaryAndFundingImportedKeyPairs } from "../utils/create-key-pair";
44
import { jsonFileExists, jsonFileWriter } from "../utils/file-utils";
55
import { walletPathResolver } from "../utils/wallet-path-resolver";
6+
import * as fs from 'fs';
67

78
const walletPath = walletPathResolver();
89

910
export class WalletInitCommand implements CommandInterface {
10-
constructor(private phrase: string | undefined, private path: string) {
11+
constructor(private phrase: string | undefined, private path: string, private n?: number) {
1112

1213
}
1314
async run(): Promise<CommandResultInterface> {
1415
if (await this.walletExists()) {
1516
throw "wallet.json exists, please remove it first to initialize another wallet. You may also use 'wallet-create' command to generate a new wallet."
1617
}
1718

18-
const wallet = await createPrimaryAndFundingKeyPairs(this.phrase, this.path);
19-
20-
await jsonFileWriter(walletPath, {
19+
const { wallet, imported } = await createPrimaryAndFundingImportedKeyPairs(this.phrase, this.path, this.n);
20+
const walletDir = `wallets/`;
21+
if (!fs.existsSync(walletDir)) {
22+
fs.mkdirSync(walletDir);
23+
}
24+
const created = {
2125
phrase: wallet.phrase,
2226
primary: {
2327
address: wallet.primary.address,
@@ -28,11 +32,13 @@ export class WalletInitCommand implements CommandInterface {
2832
address: wallet.funding.address,
2933
path: wallet.funding.path,
3034
WIF: wallet.funding.WIF
31-
}
32-
});
35+
},
36+
imported
37+
};
38+
await jsonFileWriter(walletPath, created);
3339
return {
3440
success: true,
35-
data: wallet
41+
data: created
3642
}
3743
}
3844
async walletExists() {

lib/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,9 @@ export class Atomicals implements APIInterface {
243243
}
244244
}
245245

246-
static async walletInit(phrase: string | undefined, path: string): Promise<any> {
246+
static async walletInit(phrase: string | undefined, path: string, n?: number): Promise<any> {
247247
try {
248-
const command: CommandInterface = new WalletInitCommand(phrase, path);
248+
const command: CommandInterface = new WalletInitCommand(phrase, path, n);
249249
return command.run();
250250
} catch (error: any) {
251251
return {

lib/utils/create-key-pair.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,12 @@ export const createKeyPair = async (phrase: string = '', path = `m/44'/0'/0'/0/0
5555
// tweakedChildNode: tweakedChildNodePrimary
5656
}
5757
}
58+
export interface WalletRequestDefinition {
59+
phrase?: string | undefined
60+
path?: string | undefined
61+
}
5862

59-
export const createPrimaryAndFundingKeyPairs = async (phrase?: string | undefined, path?: string | undefined) => {
63+
export const createPrimaryAndFundingImportedKeyPairs = async (phrase?: string | undefined, path?: string | undefined, n?: number) => {
6064
let phraseResult: any = phrase;
6165
if (!phraseResult) {
6266
phraseResult = await createMnemonicPhrase();
@@ -66,10 +70,20 @@ export const createPrimaryAndFundingKeyPairs = async (phrase?: string | undefine
6670
if (path) {
6771
pathUsed = path;
6872
}
73+
const imported = {}
74+
75+
if (n) {
76+
for (let i = 2; i < n + 2; i++) {
77+
imported[i+''] = await createKeyPair(phraseResult, `${pathUsed}/0/` + i)
78+
}
79+
}
6980
return {
70-
phrase: phraseResult,
71-
primary: await createKeyPair(phraseResult, `${pathUsed}/0/0`),
72-
funding: await createKeyPair(phraseResult, `${pathUsed}/1/0`)
81+
wallet: {
82+
phrase: phraseResult,
83+
primary: await createKeyPair(phraseResult, `${pathUsed}/0/0`),
84+
funding: await createKeyPair(phraseResult, `${pathUsed}/1/0`)
85+
},
86+
imported
7387
}
7488
}
7589

test/commands/wallet-create.test.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ describe('wallet-create', () => {
99
it('success', async () => {
1010
const result = await index.Atomicals.walletCreate();
1111
expect(result.success).to.be.true;
12-
expect(result.data.phrase).to.not.equal(undefined)
13-
expect(result.data.primary.WIF).to.not.equal(undefined)
14-
expect(result.data.primary.address).to.not.equal(undefined)
15-
expect(result.data.primary.privateKey).to.not.equal(undefined)
16-
expect(result.data.primary.publicKey).to.not.equal(undefined)
17-
expect(result.data.primary.publicKeyXOnly).to.not.equal(undefined)
18-
expect(result.data.primary.path).to.not.equal(undefined)
19-
expect(result.data.funding.WIF).to.not.equal(undefined)
20-
expect(result.data.funding.address).to.not.equal(undefined)
21-
expect(result.data.funding.privateKey).to.not.equal(undefined)
22-
expect(result.data.funding.publicKey).to.not.equal(undefined)
23-
expect(result.data.funding.publicKeyXOnly).to.not.equal(undefined)
24-
expect(result.data.funding.path).to.not.equal(undefined)
12+
expect(result.data.wallet.phrase).to.not.equal(undefined)
13+
expect(result.data.wallet.primary.WIF).to.not.equal(undefined)
14+
expect(result.data.wallet.primary.address).to.not.equal(undefined)
15+
expect(result.data.wallet.primary.privateKey).to.not.equal(undefined)
16+
expect(result.data.wallet.primary.publicKey).to.not.equal(undefined)
17+
expect(result.data.wallet.primary.publicKeyXOnly).to.not.equal(undefined)
18+
expect(result.data.wallet.primary.path).to.not.equal(undefined)
19+
expect(result.data.wallet.funding.WIF).to.not.equal(undefined)
20+
expect(result.data.wallet.funding.address).to.not.equal(undefined)
21+
expect(result.data.wallet.funding.privateKey).to.not.equal(undefined)
22+
expect(result.data.wallet.funding.publicKey).to.not.equal(undefined)
23+
expect(result.data.wallet.funding.publicKeyXOnly).to.not.equal(undefined)
24+
expect(result.data.wallet.funding.path).to.not.equal(undefined)
2525

2626
});
2727
});

0 commit comments

Comments
 (0)