Skip to content

Commit

Permalink
🎨 [feat]: create_wallet function, docs update (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanishq-singh-2407 authored Feb 9, 2023
2 parents 0812a5f + bed1d48 commit ce545a3
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 6 deletions.
66 changes: 60 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ import nanojs from "https://deno.land/x/nanojs/mod.ts";
}
```

<br />

5. ### _Send XNO_
```ts
const sender_private_key = "40C146373BF03EF2D62E067D38A5E6BDE2B511B5C90A99C62B6F7C3D321DDEAC";
Expand All @@ -153,13 +155,64 @@ import nanojs from "https://deno.land/x/nanojs/mod.ts";

</details>


<details>
<summary><b>Wallet</b></summary>

1. ### _Create Wallet_
```ts
const wallet1 = await create_wallet({ });
const wallet2 = await create_wallet({ seed: "5b4b36a524c213a1ef891af31dc5b3071c59c12e36b7fe14f692052b2ea9c74a", till_index: 2 });
```

```bash
{ # wallet1
seed: "a4eb1443df8cb67d7b3a869412e8f9c89734962df0711af344a9e7c44afb145e",
accounts: [
{
index: 0,
private_key: "E4A3C94BBD0592070661002E253DDAD5B1345801AFC3175DBDCE923CB6FEEAEC",
public_key: "C7537142ED0ADE795D6B73008B24A9BD31A09E477A83BCB9B7A6B0B3A7EB5418",
address: "nano_3jtmg73gt4pyh7gppwr1jekcmhbjn4h6gyn5qkwuhboipgmypo1rdzbbadxa"
}
]
}
{ # wallet2
seed: "5b4b36a524c213a1ef891af31dc5b3071c59c12e36b7fe14f692052b2ea9c74a",
accounts: [
{
index: 0,
private_key: "5C7F0183303876298751DF2482F1DB6DF27C789E1EEE42598B9BAD35B12130D2",
public_key: "2BF49EAF6AEF19BCFEBF40A05D7782838957C212CE3AC1B441023A5029DDA4E0",
address: "nano_1cznmtqpoursqmzdyi71dour71wbcz337mjtr8t641jtc1nxub915abj3pcm"
},
{
index: 1,
private_key: "325A1C6B0AC52350BD4DE2A15965753DB397E278DDE4A377346916A8DB7C8496",
public_key: "4ADDA484090D7B14B24EE56B86D7DAB1F8694FE7E18133317C126DBD5A5AD121",
address: "nano_1kpxnk41k5du4ks6xsddiudxoehrf79yhre38erqr6mfqof7onb3om5hdqax"
},
{
index: 2,
private_key: "5B573170C262F3243305880F385B61A20AA5C66757F55A76E51DCD5B4EF2C66C",
public_key: "D8818A1D542976846759F2EE6A5D90D174BA089049B42961EC4FC2663BA92732",
address: "nano_3p63jagoacdpijmomwqgfbgs3ndnqa6b1kfn77iyrmy4erxtkbskfws7pujn"
}
]
}
```

<br />

</details>

<details>
<summary><b>Block</b></summary>

1. ### _Block Information_
```ts
const hash =
"87434F8041869A01C8F6F263B87972D7BA443A72E0A97D7A3FD0CCC2358FD6F9";
const hash = "87434F8041869A01C8F6F263B87972D7BA443A72E0A97D7A3FD0CCC2358FD6F9";
const info = await nanojs.get_block_info(hash);
console.log(info);
Expand Down Expand Up @@ -219,8 +272,7 @@ import nanojs from "https://deno.land/x/nanojs/mod.ts";

1. ### _Work Generate_
```ts
const hash =
"87434F8041869A01C8F6F263B87972D7BA443A72E0A97D7A3FD0CCC2358FD6F9";
const hash = "87434F8041869A01C8F6F263B87972D7BA443A72E0A97D7A3FD0CCC2358FD6F9";
const work = await nanojs.work_generate(hash);
console.log(work);
Expand Down Expand Up @@ -255,7 +307,9 @@ import nanojs from "https://deno.land/x/nanojs/mod.ts";
"1210000000000000000000000000000"
```

2. ### _RAW to XNO_
<br />

2. ### _RAW to XNO_
```ts
const raw = "1.21";
const xno = nanojs.raw_to_xno(raw);
Expand All @@ -269,7 +323,7 @@ import nanojs from "https://deno.land/x/nanojs/mod.ts";

</details>

<br />
<br/>

# _**license**_

Expand Down
5 changes: 5 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@ export * from "./src/work/generate.ts";
export * from "./config/raw_xno.ts";
export * from "./config/representatives.ts";
export * from "./config/rpc_node_url.ts";

/**
* Wallet
*/
export * from "./src/wallet/create.ts";
49 changes: 49 additions & 0 deletions src/wallet/create.ts
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;
};
48 changes: 48 additions & 0 deletions tests/wallet/create.ts
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);
});
});
});

0 comments on commit ce545a3

Please sign in to comment.