Skip to content

Commit 6d086c1

Browse files
authored
Feat: ClaimNeurons does not require pub key (#1)
User can claim neurons. The publick key needed for the request is calculated.
1 parent 0c07692 commit 6d086c1

File tree

4 files changed

+347
-9
lines changed

4 files changed

+347
-9
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,16 @@ Live desktop app running on your computer. If you are facing connection issues
1616
when doing so, Ledger provides platform-specific
1717
[troubleshooting instructions](https://support.ledger.com/hc/en-us/articles/115005165269-Fix-USB-connection-issues-with-Ledger-Live?support=true)
1818
on their support site.
19+
20+
## Development
21+
22+
Clone the repository.
23+
24+
Install dependencies with `npm install`.
25+
26+
To execute a command, you can use `npm run execute -- <args>`.
27+
28+
For example
29+
30+
* The command `ic-hardware-wallet --network https://nnsdapp.dfinity.network icp balance`.
31+
* Would be `npm run execute -- --network https://nnsdapp.dfinity.network icp balance` for development.

index.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import {
1515
InsufficientAmountError,
1616
InsufficientFundsError,
1717
} from "@dfinity/nns";
18+
import { Principal } from "@dfinity/principal";
19+
import type { Secp256k1PublicKey } from "src/ledger/secp256k1";
1820
import { Agent, AnonymousIdentity, HttpAgent, Identity } from "@dfinity/agent";
1921
import chalk from "chalk";
2022

@@ -24,7 +26,7 @@ import "node-window-polyfill/register";
2426
// Add polyfill for `window.fetch` for agent-js to work.
2527
// @ts-ignore (no types are available)
2628
import fetch from "node-fetch";
27-
import { Principal } from "@dfinity/principal";
29+
2830
global.fetch = fetch;
2931
window.fetch = fetch;
3032

@@ -285,10 +287,20 @@ async function listNeurons() {
285287
}
286288
}
287289

290+
const buf2hex = (buffer: ArrayBuffer): string => {
291+
return [...new Uint8Array(buffer)]
292+
.map((x) => x.toString(16).padStart(2, "0"))
293+
.join("");
294+
};
295+
288296
/**
289297
* Fetches the balance of the main account on the wallet.
290298
*/
291-
async function claimNeurons(hexPubKey: string) {
299+
async function claimNeurons() {
300+
const identity = await LedgerIdentity.create();
301+
302+
const bufferKey = identity.getPublicKey() as Secp256k1PublicKey;
303+
const hexPubKey = buf2hex(bufferKey.toRaw());
292304
const isHex = hexPubKey.match("^[0-9a-fA-F]+$");
293305
if (!isHex) {
294306
throw new Error(`${hexPubKey} is not a hex string.`);
@@ -298,7 +310,6 @@ async function claimNeurons(hexPubKey: string) {
298310
throw new Error(`The key must be >= 130 characters and <= 150 characters.`);
299311
}
300312

301-
const identity = await LedgerIdentity.create();
302313
const governance = await GenesisTokenCanister.create({
303314
agent: await getAgent(identity),
304315
});
@@ -477,11 +488,10 @@ async function main() {
477488
)
478489
.addCommand(
479490
new Command("claim")
480-
.requiredOption(
481-
"--hex-public-key <public-key>",
491+
.description(
482492
"Claim the caller's GTC neurons."
483493
)
484-
.action((args) => run(() => claimNeurons(args.hexPublicKey)))
494+
.action((args) => run(() => claimNeurons()))
485495
);
486496

487497
const icp = new Command("icp")

0 commit comments

Comments
 (0)