@dfinity/hardware-wallet-cli 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/CODEOWNERS +2 -0
- package/.prettierignore +1 -0
- package/LICENSE +201 -0
- package/README.md +17 -0
- package/build/index.js +394 -0
- package/build/src/ledger/identity.js +216 -0
- package/build/src/ledger/secp256k1.js +74 -0
- package/index.ts +535 -0
- package/package.json +43 -0
- package/src/ledger/identity.ts +255 -0
- package/src/ledger/secp256k1.ts +96 -0
- package/tsconfig.json +15 -0
package/.prettierignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
build
|
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2021 DFINITY LLC.
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# hardware-wallet-cli
|
|
2
|
+
|
|
3
|
+
A CLI to interact with the Internet Computer App on Ledger Nano S/X devices.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
1. Install `npm >= 6.14`.
|
|
8
|
+
2. Install the CLI: `npm install -g @dfinity/hardware-wallet-cli`
|
|
9
|
+
|
|
10
|
+
## USB connection issues with Ledger Live
|
|
11
|
+
|
|
12
|
+
In order to install the Internet Computer App on your Ledger device or perform
|
|
13
|
+
the genuine check, you need to first connect your Ledger device with the Ledger
|
|
14
|
+
Live desktop app running on your computer. If you are facing connection issues
|
|
15
|
+
when doing so, Ledger provides platform-specific
|
|
16
|
+
[troubleshooting instructions](https://support.ledger.com/hc/en-us/articles/115005165269-Fix-USB-connection-issues-with-Ledger-Live?support=true)
|
|
17
|
+
on their support site.
|
package/build/index.js
ADDED
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
+
};
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
/**
|
|
8
|
+
* A CLI tool for testing the Ledger hardware wallet integration.
|
|
9
|
+
*/
|
|
10
|
+
const commander_1 = require("commander");
|
|
11
|
+
const identity_1 = require("./src/ledger/identity");
|
|
12
|
+
const nns_1 = require("@dfinity/nns");
|
|
13
|
+
const agent_1 = require("@dfinity/agent");
|
|
14
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
15
|
+
// Add polyfill for `window` for `TransportWebHID` checks to work.
|
|
16
|
+
require("node-window-polyfill/register");
|
|
17
|
+
// Add polyfill for `window.fetch` for agent-js to work.
|
|
18
|
+
// @ts-ignore (no types are available)
|
|
19
|
+
const node_fetch_1 = __importDefault(require("node-fetch"));
|
|
20
|
+
const principal_1 = require("@dfinity/principal");
|
|
21
|
+
global.fetch = node_fetch_1.default;
|
|
22
|
+
window.fetch = node_fetch_1.default;
|
|
23
|
+
const program = new commander_1.Command();
|
|
24
|
+
const log = console.log;
|
|
25
|
+
const SECONDS_PER_MINUTE = 60;
|
|
26
|
+
const SECONDS_PER_HOUR = 60 * SECONDS_PER_MINUTE;
|
|
27
|
+
const SECONDS_PER_DAY = 24 * SECONDS_PER_HOUR;
|
|
28
|
+
const SECONDS_PER_YEAR = 365 * SECONDS_PER_DAY + 6 * SECONDS_PER_HOUR;
|
|
29
|
+
async function getAgent(identity) {
|
|
30
|
+
const network = program.opts().network;
|
|
31
|
+
// Only fetch the rootkey if the network isn't mainnet.
|
|
32
|
+
const fetchRootKey = new URL(network).host == "ic0.app" ? false : true;
|
|
33
|
+
const agent = new agent_1.HttpAgent({
|
|
34
|
+
host: program.opts().network,
|
|
35
|
+
identity: identity,
|
|
36
|
+
});
|
|
37
|
+
if (fetchRootKey) {
|
|
38
|
+
await agent.fetchRootKey();
|
|
39
|
+
}
|
|
40
|
+
return agent;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Fetches the balance of the main account on the wallet.
|
|
44
|
+
*/
|
|
45
|
+
async function getBalance() {
|
|
46
|
+
const identity = await identity_1.LedgerIdentity.create();
|
|
47
|
+
const accountIdentifier = nns_1.AccountIdentifier.fromPrincipal({
|
|
48
|
+
principal: identity.getPrincipal(),
|
|
49
|
+
});
|
|
50
|
+
const ledger = nns_1.LedgerCanister.create({
|
|
51
|
+
agent: await getAgent(new agent_1.AnonymousIdentity()),
|
|
52
|
+
});
|
|
53
|
+
const balance = await ledger.accountBalance({
|
|
54
|
+
accountIdentifier: accountIdentifier,
|
|
55
|
+
});
|
|
56
|
+
ok(`Account ${accountIdentifier.toHex()} has balance ${balance.toE8s()} e8s`);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Send ICP to another address.
|
|
60
|
+
*
|
|
61
|
+
* @param to The account identifier in hex.
|
|
62
|
+
* @param amount Amount to send in e8s.
|
|
63
|
+
*/
|
|
64
|
+
async function sendICP(to, amount) {
|
|
65
|
+
const identity = await identity_1.LedgerIdentity.create();
|
|
66
|
+
const ledger = nns_1.LedgerCanister.create({
|
|
67
|
+
agent: await getAgent(identity),
|
|
68
|
+
});
|
|
69
|
+
const blockHeight = await ledger.transfer({
|
|
70
|
+
to: to,
|
|
71
|
+
amount: amount,
|
|
72
|
+
memo: BigInt(0),
|
|
73
|
+
});
|
|
74
|
+
ok(`Transaction completed at block height ${blockHeight}.`);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Shows the principal and account idenifier on the terminal and on the wallet's screen.
|
|
78
|
+
*/
|
|
79
|
+
async function showInfo(showOnDevice) {
|
|
80
|
+
const identity = await identity_1.LedgerIdentity.create();
|
|
81
|
+
const accountIdentifier = nns_1.AccountIdentifier.fromPrincipal({
|
|
82
|
+
principal: identity.getPrincipal(),
|
|
83
|
+
});
|
|
84
|
+
log(chalk_1.default.bold(`Principal: `) + identity.getPrincipal());
|
|
85
|
+
log(chalk_1.default.bold(`Address (${identity.derivePath}): `) + accountIdentifier.toHex());
|
|
86
|
+
if (showOnDevice) {
|
|
87
|
+
log("Displaying the principal and the address on the device...");
|
|
88
|
+
await identity.showAddressAndPubKeyOnDevice();
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Stakes a new neuron.
|
|
93
|
+
*
|
|
94
|
+
* @param amount Amount to stake in e8s.
|
|
95
|
+
*/
|
|
96
|
+
async function stakeNeuron(stake) {
|
|
97
|
+
const identity = await identity_1.LedgerIdentity.create();
|
|
98
|
+
const ledger = nns_1.LedgerCanister.create({
|
|
99
|
+
agent: await getAgent(identity),
|
|
100
|
+
});
|
|
101
|
+
const governance = nns_1.GovernanceCanister.create({
|
|
102
|
+
agent: await getAgent(new agent_1.AnonymousIdentity()),
|
|
103
|
+
hardwareWallet: true,
|
|
104
|
+
});
|
|
105
|
+
// Flag that an upcoming stake neuron transaction is coming to distinguish
|
|
106
|
+
// it from a "send ICP" transaction on the device.
|
|
107
|
+
identity.flagUpcomingStakeNeuron();
|
|
108
|
+
try {
|
|
109
|
+
const stakedNeuronId = await governance.stakeNeuron({
|
|
110
|
+
stake: stake,
|
|
111
|
+
principal: identity.getPrincipal(),
|
|
112
|
+
ledgerCanister: ledger,
|
|
113
|
+
});
|
|
114
|
+
ok(`Staked neuron with ID: ${stakedNeuronId}`);
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
if (error instanceof nns_1.InsufficientAmountError) {
|
|
118
|
+
err(`Cannot stake less than ${error.minimumAmount.toE8s()} e8s`);
|
|
119
|
+
}
|
|
120
|
+
else if (error instanceof nns_1.InsufficientFundsError) {
|
|
121
|
+
err(`Your account has insufficient funds (${error.balance.toE8s()} e8s)`);
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
console.log(error);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
async function increaseDissolveDelay(neuronId, years, days, minutes, seconds) {
|
|
129
|
+
const identity = await identity_1.LedgerIdentity.create();
|
|
130
|
+
const governance = nns_1.GovernanceCanister.create({
|
|
131
|
+
agent: await getAgent(identity),
|
|
132
|
+
hardwareWallet: true,
|
|
133
|
+
});
|
|
134
|
+
const additionalDissolveDelaySeconds = years * SECONDS_PER_YEAR +
|
|
135
|
+
days * SECONDS_PER_DAY +
|
|
136
|
+
minutes * SECONDS_PER_MINUTE +
|
|
137
|
+
seconds;
|
|
138
|
+
await governance.increaseDissolveDelay({
|
|
139
|
+
neuronId: neuronId,
|
|
140
|
+
additionalDissolveDelaySeconds: additionalDissolveDelaySeconds,
|
|
141
|
+
});
|
|
142
|
+
ok();
|
|
143
|
+
}
|
|
144
|
+
async function disburseNeuron(neuronId, to, amount) {
|
|
145
|
+
const identity = await identity_1.LedgerIdentity.create();
|
|
146
|
+
const governance = nns_1.GovernanceCanister.create({
|
|
147
|
+
agent: await getAgent(identity),
|
|
148
|
+
hardwareWallet: false,
|
|
149
|
+
});
|
|
150
|
+
await governance.disburse({
|
|
151
|
+
neuronId: BigInt(neuronId),
|
|
152
|
+
toAccountId: to,
|
|
153
|
+
amount: amount,
|
|
154
|
+
});
|
|
155
|
+
ok();
|
|
156
|
+
}
|
|
157
|
+
async function spawnNeuron(neuronId, controller) {
|
|
158
|
+
const identity = await identity_1.LedgerIdentity.create();
|
|
159
|
+
const governance = nns_1.GovernanceCanister.create({
|
|
160
|
+
agent: await getAgent(identity),
|
|
161
|
+
hardwareWallet: true,
|
|
162
|
+
});
|
|
163
|
+
const spawnedNeuronId = await governance.spawnNeuron({
|
|
164
|
+
neuronId: BigInt(neuronId),
|
|
165
|
+
newController: controller,
|
|
166
|
+
});
|
|
167
|
+
ok(`Spawned neuron with ID ${spawnedNeuronId}`);
|
|
168
|
+
}
|
|
169
|
+
async function startDissolving(neuronId) {
|
|
170
|
+
const identity = await identity_1.LedgerIdentity.create();
|
|
171
|
+
const governance = nns_1.GovernanceCanister.create({
|
|
172
|
+
agent: await getAgent(identity),
|
|
173
|
+
hardwareWallet: true,
|
|
174
|
+
});
|
|
175
|
+
await governance.startDissolving(neuronId);
|
|
176
|
+
ok();
|
|
177
|
+
}
|
|
178
|
+
async function stopDissolving(neuronId) {
|
|
179
|
+
const identity = await identity_1.LedgerIdentity.create();
|
|
180
|
+
const governance = nns_1.GovernanceCanister.create({
|
|
181
|
+
agent: await getAgent(identity),
|
|
182
|
+
hardwareWallet: true,
|
|
183
|
+
});
|
|
184
|
+
await governance.stopDissolving(neuronId);
|
|
185
|
+
ok();
|
|
186
|
+
}
|
|
187
|
+
async function addHotkey(neuronId, principal) {
|
|
188
|
+
const identity = await identity_1.LedgerIdentity.create();
|
|
189
|
+
const governance = nns_1.GovernanceCanister.create({
|
|
190
|
+
agent: await getAgent(identity),
|
|
191
|
+
hardwareWallet: true,
|
|
192
|
+
});
|
|
193
|
+
await governance.addHotkey({
|
|
194
|
+
neuronId: BigInt(neuronId),
|
|
195
|
+
principal: principal,
|
|
196
|
+
});
|
|
197
|
+
ok();
|
|
198
|
+
}
|
|
199
|
+
async function removeHotkey(neuronId, principal) {
|
|
200
|
+
const identity = await identity_1.LedgerIdentity.create();
|
|
201
|
+
const governance = nns_1.GovernanceCanister.create({
|
|
202
|
+
agent: await getAgent(identity),
|
|
203
|
+
hardwareWallet: true,
|
|
204
|
+
});
|
|
205
|
+
await governance.removeHotkey({
|
|
206
|
+
neuronId: BigInt(neuronId),
|
|
207
|
+
principal: principal,
|
|
208
|
+
});
|
|
209
|
+
ok();
|
|
210
|
+
}
|
|
211
|
+
async function listNeurons() {
|
|
212
|
+
const identity = await identity_1.LedgerIdentity.create();
|
|
213
|
+
const governance = nns_1.GovernanceCanister.create({
|
|
214
|
+
agent: await getAgent(identity),
|
|
215
|
+
hardwareWallet: true,
|
|
216
|
+
});
|
|
217
|
+
// We filter neurons with no ICP, as they'll be garbage collected by the governance canister.
|
|
218
|
+
const neurons = await governance.listNeurons({
|
|
219
|
+
certified: true,
|
|
220
|
+
});
|
|
221
|
+
if (neurons.length > 0) {
|
|
222
|
+
neurons.forEach((n) => {
|
|
223
|
+
log(`Neuron ID: ${n.neuronId}`);
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
ok("No neurons found.");
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Fetches the balance of the main account on the wallet.
|
|
232
|
+
*/
|
|
233
|
+
async function claimNeurons(hexPubKey) {
|
|
234
|
+
const isHex = hexPubKey.match("^[0-9a-fA-F]+$");
|
|
235
|
+
if (!isHex) {
|
|
236
|
+
throw new Error(`${hexPubKey} is not a hex string.`);
|
|
237
|
+
}
|
|
238
|
+
if (hexPubKey.length < 130 || hexPubKey.length > 150) {
|
|
239
|
+
throw new Error(`The key must be >= 130 characters and <= 150 characters.`);
|
|
240
|
+
}
|
|
241
|
+
const identity = await identity_1.LedgerIdentity.create();
|
|
242
|
+
const governance = await nns_1.GenesisTokenCanister.create({
|
|
243
|
+
agent: await getAgent(identity),
|
|
244
|
+
});
|
|
245
|
+
const claimedNeuronIds = await governance.claimNeurons({
|
|
246
|
+
hexPubKey: hexPubKey,
|
|
247
|
+
});
|
|
248
|
+
ok(`Successfully claimed the following neurons: ${claimedNeuronIds}`);
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Runs a function with a try/catch block.
|
|
252
|
+
*/
|
|
253
|
+
async function run(f) {
|
|
254
|
+
try {
|
|
255
|
+
await f();
|
|
256
|
+
}
|
|
257
|
+
catch (error) {
|
|
258
|
+
err(error);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
function ok(message) {
|
|
262
|
+
if (message) {
|
|
263
|
+
log(`${chalk_1.default.green(chalk_1.default.bold("OK"))}: ${message}`);
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
log(`${chalk_1.default.green(chalk_1.default.bold("OK"))}`);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
function err(error) {
|
|
270
|
+
const message = error instanceof nns_1.GovernanceError
|
|
271
|
+
? error.detail.error_message
|
|
272
|
+
: error instanceof Error
|
|
273
|
+
? error.message
|
|
274
|
+
: error;
|
|
275
|
+
log(`${chalk_1.default.bold(chalk_1.default.red("Error:"))} ${message}`);
|
|
276
|
+
}
|
|
277
|
+
function tryParseInt(value) {
|
|
278
|
+
const parsedValue = parseInt(value, 10);
|
|
279
|
+
if (isNaN(parsedValue)) {
|
|
280
|
+
throw new commander_1.InvalidArgumentError("Not a number.");
|
|
281
|
+
}
|
|
282
|
+
return parsedValue;
|
|
283
|
+
}
|
|
284
|
+
function tryParseBigInt(value) {
|
|
285
|
+
try {
|
|
286
|
+
return BigInt(value);
|
|
287
|
+
}
|
|
288
|
+
catch (err) {
|
|
289
|
+
throw new commander_1.InvalidArgumentError(err.toString());
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
function tryParsePrincipal(value) {
|
|
293
|
+
try {
|
|
294
|
+
return principal_1.Principal.fromText(value);
|
|
295
|
+
}
|
|
296
|
+
catch (err) {
|
|
297
|
+
throw new commander_1.InvalidArgumentError(err.toString());
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
function tryParseE8s(e8s) {
|
|
301
|
+
try {
|
|
302
|
+
return nns_1.ICP.fromE8s(tryParseBigInt(e8s));
|
|
303
|
+
}
|
|
304
|
+
catch (err) {
|
|
305
|
+
throw new commander_1.InvalidArgumentError(err.toString());
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
function tryParseAccountIdentifier(accountIdentifier) {
|
|
309
|
+
try {
|
|
310
|
+
return nns_1.AccountIdentifier.fromHex(accountIdentifier);
|
|
311
|
+
}
|
|
312
|
+
catch (err) {
|
|
313
|
+
throw new commander_1.InvalidArgumentError(err.toString());
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
async function main() {
|
|
317
|
+
const neuron = new commander_1.Command("neuron")
|
|
318
|
+
.description("Commands for managing neurons.")
|
|
319
|
+
.showSuggestionAfterError()
|
|
320
|
+
.addCommand(new commander_1.Command("stake")
|
|
321
|
+
.requiredOption("--amount <amount>", "Amount to stake in e8s.", tryParseE8s)
|
|
322
|
+
.action((args) => run(() => stakeNeuron(args.amount))))
|
|
323
|
+
.addCommand(new commander_1.Command("increase-dissolve-delay")
|
|
324
|
+
.requiredOption("--neuron-id <neuron-id>", "Neuron ID", tryParseBigInt)
|
|
325
|
+
.option("--years <years>", "Number of years", tryParseInt)
|
|
326
|
+
.option("--days <days>", "Number of days", tryParseInt)
|
|
327
|
+
.option("--minutes <minutes>", "Number of minutes", tryParseInt)
|
|
328
|
+
.option("--seconds <seconds>", "Number of seconds", tryParseInt)
|
|
329
|
+
.action((args) => run(() => increaseDissolveDelay(args.neuronId, args.years || 0, args.days || 0, args.minutes || 0, args.seconds || 0))))
|
|
330
|
+
.addCommand(new commander_1.Command("disburse")
|
|
331
|
+
.requiredOption("--neuron-id <neuron-id>", "Neuron ID", tryParseBigInt)
|
|
332
|
+
.option("--to <account-identifier>")
|
|
333
|
+
.option("--amount <amount>", "Amount to disburse (empty to disburse all)", tryParseBigInt)
|
|
334
|
+
.action((args) => {
|
|
335
|
+
run(() => disburseNeuron(args.neuronId, args.to, args.amount));
|
|
336
|
+
}))
|
|
337
|
+
.addCommand(new commander_1.Command("spawn")
|
|
338
|
+
.requiredOption("--neuron-id <neuron-id>", "Neuron ID", tryParseBigInt)
|
|
339
|
+
.option("--controller <new-controller>", "Controller", tryParsePrincipal)
|
|
340
|
+
.action((args) => {
|
|
341
|
+
run(() => spawnNeuron(args.neuronId, args.controller));
|
|
342
|
+
}))
|
|
343
|
+
.addCommand(new commander_1.Command("start-dissolving")
|
|
344
|
+
.requiredOption("--neuron-id <neuron-id>", "Neuron ID", tryParseBigInt)
|
|
345
|
+
.action((args) => {
|
|
346
|
+
run(() => startDissolving(args.neuronId));
|
|
347
|
+
}))
|
|
348
|
+
.addCommand(new commander_1.Command("stop-dissolving")
|
|
349
|
+
.requiredOption("--neuron-id <neuron-id>", "Neuron ID", tryParseBigInt)
|
|
350
|
+
.action((args) => {
|
|
351
|
+
run(() => stopDissolving(args.neuronId));
|
|
352
|
+
}))
|
|
353
|
+
.addCommand(new commander_1.Command("list").action(() => run(listNeurons)))
|
|
354
|
+
.addCommand(new commander_1.Command("add-hotkey")
|
|
355
|
+
.requiredOption("--neuron-id <neuron-id>", "Neuron ID", tryParseBigInt)
|
|
356
|
+
.requiredOption("--principal <principal>", "Principal", tryParsePrincipal)
|
|
357
|
+
.action((args) => run(() => addHotkey(args.neuronId, args.principal))))
|
|
358
|
+
.addCommand(new commander_1.Command("remove-hotkey")
|
|
359
|
+
.requiredOption("--neuron-id <neuron-id>", "Neuron ID", tryParseBigInt)
|
|
360
|
+
.requiredOption("--principal <principal>", "Principal", tryParsePrincipal)
|
|
361
|
+
.action((args) => run(() => removeHotkey(args.neuronId, args.principal))))
|
|
362
|
+
.addCommand(new commander_1.Command("claim")
|
|
363
|
+
.requiredOption("--hex-public-key <public-key>", "Claim the caller's GTC neurons.")
|
|
364
|
+
.action((args) => run(() => claimNeurons(args.hexPublicKey))));
|
|
365
|
+
const icp = new commander_1.Command("icp")
|
|
366
|
+
.description("Commands for managing ICP.")
|
|
367
|
+
.showSuggestionAfterError()
|
|
368
|
+
.addCommand(new commander_1.Command("balance")
|
|
369
|
+
.description("Fetch current balance.")
|
|
370
|
+
.action(() => {
|
|
371
|
+
run(getBalance);
|
|
372
|
+
}))
|
|
373
|
+
.addCommand(new commander_1.Command("transfer")
|
|
374
|
+
.requiredOption("--to <account-identifier>", "Account identifier to transfer to.", tryParseAccountIdentifier)
|
|
375
|
+
.requiredOption("--amount <amount>", "Amount to transfer in e8s.", tryParseE8s)
|
|
376
|
+
.action((args) => run(() => sendICP(args.to, args.amount))));
|
|
377
|
+
program
|
|
378
|
+
.description("A CLI for the Ledger hardware wallet.")
|
|
379
|
+
.enablePositionalOptions()
|
|
380
|
+
.showSuggestionAfterError()
|
|
381
|
+
.addOption(new commander_1.Option("--network <network>", "The IC network to talk to.")
|
|
382
|
+
.default("https://ic0.app")
|
|
383
|
+
.env("IC_NETWORK"))
|
|
384
|
+
.addCommand(new commander_1.Command("info")
|
|
385
|
+
.option("-n --no-show-on-device")
|
|
386
|
+
.description("Show the wallet's principal, address, and balance.")
|
|
387
|
+
.action((args) => {
|
|
388
|
+
run(() => showInfo(args.showOnDevice));
|
|
389
|
+
}))
|
|
390
|
+
.addCommand(icp)
|
|
391
|
+
.addCommand(neuron);
|
|
392
|
+
await program.parseAsync(process.argv);
|
|
393
|
+
}
|
|
394
|
+
main();
|