-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎨 [feat]:
create_wallet
function, docs update (#8)
- Loading branch information
Showing
4 changed files
with
162 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { nanocurrency } from "../../deps.ts"; | ||
import { get_account_keys } from "../../mod.ts"; | ||
|
||
export type Account = { | ||
index: number; | ||
private_key: string; | ||
public_key: string; | ||
address: string; | ||
}; | ||
|
||
export type Wallet = { | ||
seed: string; | ||
accounts: Account[]; | ||
}; | ||
|
||
export type Create_Wallet_Props = { seed?: string; till_index?: number }; | ||
|
||
/** | ||
* @name create wallet | ||
* @param {Props} Props | ||
* @returns {Wallet} | ||
* | ||
* @example | ||
* ```ts | ||
* import { create_wallet } from './deps'; | ||
* | ||
* const wallet1 = await create_wallet({ }); | ||
* const wallet2 = await create_wallet({ till_index: 2 }); | ||
* const wallet3 = await create_wallet({ seed: "5b4b36a524c213a1ef891af31dc5b3071c59c12e36b7fe14f692052b2ea9c74a" }); | ||
* const wallet4 = await create_wallet({ seed: "5b4b36a524c213a1ef891af31dc5b3071c59c12e36b7fe14f692052b2ea9c74a", till_index: 3 }); | ||
* ``` | ||
*/ | ||
export const create_wallet = async ( | ||
{ seed, till_index = 0 }: Create_Wallet_Props, | ||
): Promise<Wallet> => { | ||
const wallet: Wallet = { | ||
seed: seed ? seed : await nanocurrency.generateSeed(), | ||
accounts: [], | ||
}; | ||
|
||
for (let i = 0; i <= till_index; i++) { | ||
const private_key = nanocurrency.deriveSecretKey(wallet.seed, i); | ||
const account = get_account_keys(private_key); | ||
|
||
wallet.accounts.push({ index: i, ...account }); | ||
} | ||
|
||
return wallet; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { create_wallet } from "../../mod.ts"; | ||
import { testing } from "../../deps.ts"; | ||
import fake_wallet from "../../config/fake_wallet.json" assert { type: "json" }; | ||
|
||
const { assertEquals } = testing; | ||
|
||
Deno.test("Create Wallet", async (t) => { | ||
await t.step("Without Seed", async (t) => { | ||
await t.step("Index = null", async () => { | ||
const wallet = await create_wallet({}); | ||
|
||
assertEquals(wallet.seed.length, 64); | ||
assertEquals(wallet.accounts.length, 1); | ||
}); | ||
|
||
await t.step("Index = 4", async () => { | ||
const wallet = await create_wallet({ till_index: 4 }); | ||
|
||
assertEquals(wallet.seed.length, 64); | ||
assertEquals(wallet.accounts.length, 5); | ||
}); | ||
}); | ||
|
||
await t.step("With Seed", async (t) => { | ||
await t.step("Index = null", async () => { | ||
const wallet = await create_wallet({ seed: fake_wallet.seed }); | ||
|
||
assertEquals(wallet.seed.length, 64); | ||
assertEquals(wallet.accounts.length, 1); | ||
|
||
assertEquals(wallet.seed, fake_wallet.seed); | ||
assertEquals(wallet.accounts[0], fake_wallet.accounts[0]); | ||
}); | ||
|
||
await t.step("Index = max of fake wallet", async () => { | ||
const wallet = await create_wallet({ | ||
seed: fake_wallet.seed, | ||
till_index: fake_wallet.accounts.length - 1, | ||
}); | ||
|
||
assertEquals(wallet.seed.length, 64); | ||
assertEquals(wallet.accounts.length, fake_wallet.accounts.length); | ||
|
||
assertEquals(wallet.seed, fake_wallet.seed); | ||
assertEquals(wallet.accounts, fake_wallet.accounts); | ||
}); | ||
}); | ||
}); |