-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmultiSigSigner.js
430 lines (368 loc) · 14.9 KB
/
multiSigSigner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
require('dotenv').config();
const {
AccountId,
PrivateKey,
Client,
Mnemonic,
Transaction,
Hbar,
HbarUnit,
AccountCreateTransaction,
KeyList,
PublicKey,
AccountUpdateTransaction,
TransactionId,
TransferTransaction,
ContractExecuteTransaction,
} = require('@hashgraph/sdk');
const fs = require('fs');
const readlineSync = require('readline-sync');
const { requestMultiSig } = require('./reqMultiSig.js');
require('dotenv').config();
let publicKeyList = process.env.MULTI_SIG_PUBLIC_KEYS.split(',') || null;
let privateKeyList = process.env.MULTI_SIG_PRIVATE_KEYS.split(',') || null;
let multiSigThreshold = Number(process.env.MULTI_SIG_THRESHOLD) || null;
// add signature documented to require only single node to be used.
const nodeId = [new AccountId(3)];
function getArg(arg) {
const customIndex = process.argv.indexOf(`-${arg}`);
let customValue;
if (customIndex > -1) {
// Retrieve the value after --custom
customValue = process.argv[customIndex + 1];
}
return customValue;
}
function getArgFlag(arg) {
const customIndex = process.argv.indexOf(`-${arg}`);
if (customIndex > -1) {
return true;
}
return false;
}
// read in from transaction bytes, add signature and export transaction bytes
// execute signed tx if signature list complete.
// update keys on an account based to convert to multi sig
async function main() {
if (getArgFlag('h')) {
console.log('Usage: node multiSigSigner.js [-generate]');
console.log(' [-query [-bytes <TRANSACTIONBYTES as base64>]');
console.log(' [-sign [-privatekeys \'302ABC,302QQA\'] [-bytes <TRANSACTIONBYTES as base64>]');
console.log(' [-newaccount [-threshold Y] [-initialbal Z] [-publickeys \'MFC,BRV,GAS\']]');
console.log(' [-convert -account 0.0.XXXX [[-threshold Y] [-publickeys \'MFC,BRV,GAS\']] [-singlekey]');
console.log(' [-update -account 0.0.XXXX [[-threshold Y] [-publickeys \'MFC,BRV,GAS\']]');
console.log(' -generate create a new public / private keypair');
console.log(' -query displays tx details of MULTI_SIG_BYTES or overide on commandline with -bytes');
console.log(' -sign query then sign tx details of MULTI_SIG_BYTES or overide on commandline with -bytes');
console.log(' -privatekeys overide private key(s) as csv else env MULTI_SIG_PRIVATE_KEYS');
console.log(' -newaccount create a new multi signature secured account');
console.log(' -threshold overide threshold (e.g. 2 of X public keys) else env MULTI_SIG_THRESHOLD');
console.log(' -publickeys overide public keys (as csv) else env MULTI_SIG_PUBLIC_KEYS');
console.log(' -convert update keys on account to multisig / back to single key of operator');
console.log(' -account looks to .env MULTI_SIG_ADJUST_ACCOUNT or overide here -> 0.0.XXX the account to operate on');
console.log(' -threshold overide threshold (e.g. 2 of X public keys) else env MULTI_SIG_THRESHOLD');
console.log(' -publickeys overide public keys (as csv) else env MULTI_SIG_PUBLIC_KEYS');
console.log(' -oldkey old account key overide -> looks to OLD_KEY in .env and if not specified tries operator key');
console.log(' -singlekey convert back to an account on single key (the operator -> MY_ACCOUNT_ID fron .env)');
console.log(' -update update a multiSig account to a new multiSig key set');
console.log(' -account looks to .env MULTI_SIG_ADJUST_ACCOUNT or overide here -> 0.0.XXX the account to operate on');
console.log(' -threshold overide threshold (e.g. 2 of X public keys) else env MULTI_SIG_THRESHOLD');
console.log(' -publickeys overide public keys (as csv) else env MULTI_SIG_PUBLIC_KEYS');
process.exit(0);
}
const isQuery = getArgFlag('query');
const isNewAccount = getArgFlag('newaccount');
const isConvertAccount = getArgFlag('convert');
const isUpdateAccont = getArgFlag('update');
const isSingleKeyUpdate = getArgFlag('singlekey');
if (getArgFlag('sign') || isQuery) {
let txBytesAsBase64 = process.env.MULTI_SIG_BYTES;
if (getArgFlag('bytes')) txBytesAsBase64 = getArg('bytes');
if (!txBytesAsBase64) {
console.log('No tx supplied ot query - exiting');
return;
}
console.log('\n-Decoding...');
const txAsBytes = Uint8Array.from(Buffer.from(txBytesAsBase64, 'base64'));
console.log('\n-Reconstructing transaction...');
const tx = Transaction.fromBytes(txAsBytes);
console.log(JSON.stringify(tx));
// TODO: extend for FT / NFT / SC calls
console.log('\n* memo: ' + tx._transactionMemo +
'\n* maxTxFee: ' + new Hbar(tx._maxTransactionFee._valueInTinybar, HbarUnit.Tinybar).toString() +
'\n* proposed tx type: ' + tx.constructor.name + ' : ' + getTransactionType(tx) +
'\n* proposed hbar tx: ' + getHbarTransfers(tx));
if (isQuery) return;
const sign = readlineSync.keyInYNStrict('Do you want to sign the proposed tx?');
if (sign) {
// not recomended but adding for flexibility
if (getArgFlag('privatekeys')) privateKeyList = getArg('privatekeys').split(',');
if (!privateKeyList || privateKeyList.length == 0 || privateKeyList[0] == '') {
console.log('No private keys supplied - exiting');
return;
}
else {
console.log('\n-Private Keys loaded');
}
for (let k = 0; k < privateKeyList.length; k++) {
const pk = PrivateKey.fromString(privateKeyList[k]);
const signedTxAsBytes = await pk.signTransaction(tx);
const signedTxBytesAsBase64 = Buffer.from(signedTxAsBytes).toString('base64');
console.log('\n*Signed - tx@' + k + ' *\n' +
'-------Copy between lines-------\n' +
pk.publicKey + ':' + signedTxBytesAsBase64 +
'\n-------Copy between lines-------');
}
}
else {
console.log('User aborted');
return;
}
}
else if (getArgFlag('generate')) {
console.log('Generating new keys...');
// Generate New key
const mnemonic = await Mnemonic.generate();
const newPrivateKey = await mnemonic.toPrivateKey();
const outputString = 'Mnemonic:\n'
+ mnemonic.toString()
+ '\nNew Private Key:\n'
+ newPrivateKey.toString()
+ '\nNew Public Key:\n'
+ newPrivateKey.publicKey
+ '\n\nNew mnemonic:\n'
+ mnemonic;
const save = readlineSync.keyInYNStrict('Do you want to save your new generated keys to file?\n**HIGHLY RECOMMENDED as if lost the wallet could become inaccessible**');
if (save) {
const startTime = new Date();
const timestamp = startTime.toISOString().split('.')[0].replaceAll(':', '-');
const filename = `./PK-${timestamp}.txt`;
fs.writeFileSync(filename, outputString, { flag: 'w' }, function(err) {
if (err) {
console.log('ERROR occured - printing to console:\n', outputString);
return console.error(err);
}
// read it back in to be sure it worked.
fs.readFile(filename, 'utf-8', function(err) {
if (err) {
console.log('ERROR reading back the file - printing to console:\n', outputString);
return console.error(err);
}
console.log('Keys saved', filename);
});
});
}
else {
console.log(outputString);
}
}
else if (isNewAccount || isConvertAccount || isUpdateAccont) {
const operatorId = AccountId.fromString(process.env.MY_ACCOUNT_ID);
const operatorKey = PrivateKey.fromString(process.env.MY_PRIVATE_KEY);
const env = process.env.ENVIRONMENT || null;
let client;
if (!env || !operatorId || !operatorKey) {
console.log('Please check environment variables ar set -> MY_PRIVATE_KEY / MY_PRIVATE_KEY / ENVIRONMENT');
process.exit(1);
}
if (getArgFlag('threshold')) {
multiSigThreshold = Number(getArg('threshold'));
}
let initialBalance = 10;
if (getArgFlag('initialbal')) {
initialBalance = Number(getArg('initialbal'));
}
if (getArgFlag('publickeys')) {
publicKeyList = getArg('publickeys').split(',');
}
console.log(`- Using account: ${operatorId} as payer`);
console.log('- Using ENVIRONMENT:', env);
console.log('- Using threshold:', multiSigThreshold);
if (isNewAccount) console.log('- Using Initial Balance:', initialBalance);
if (env == 'TEST') {
client = Client.forTestnet();
console.log('operating in *TESTNET*');
}
else if (env == 'MAIN') {
client = Client.forMainnet();
console.log('operating in *MAINNET*');
}
else {
console.log('ERROR: Must specify either MAIN or TEST as environment in .env file');
return;
}
const keyList = [];
for (const p in publicKeyList) {
const publicKey = PublicKey.fromString(publicKeyList[p]);
keyList.push(publicKey);
if (!isSingleKeyUpdate) console.log('-Adding public key:', publicKey.toString());
}
if (isNewAccount) {
const proceed = readlineSync.keyInYNStrict('Do you want to create the account as a multi signature wallet?');
if (proceed) {
const thresholdKey = new KeyList(keyList, multiSigThreshold);
console.log('The ' + multiSigThreshold + '/' + keyList.length + ' threshold key structure' + thresholdKey);
const newAcctId = await multiSigAccountCreator(initialBalance, thresholdKey, operatorId, operatorKey, client);
console.log('\n\nPlease note down the new account ID:', newAcctId.toString());
}
else {
console.log('User aborted');
return;
}
}
else {
let accountIdString = process.env.MULTI_SIG_ADJUST_ACCOUNT;
if (getArgFlag('account')) accountIdString = getArg('account');
if (!accountIdString) {
console.log('Please specify the account to update either with -account or in .env MULTI_SIG_ADJUST_ACCOUNT');
return;
}
const accountId = AccountId.fromString(accountIdString);
if (isConvertAccount) {
if (isSingleKeyUpdate) {
const proceed = readlineSync.keyInYNStrict('Do you want to revert the account to a single signature wallet?');
if (proceed) {
await updateKeysOnAccount(accountId, new KeyList(), operatorKey, operatorId, operatorKey, client);
}
else {
console.log('User aborted');
return;
}
}
else {
let oldkey = process.env.OLD_KEY;
if (getArgFlag('oldkey')) oldkey = getArg('oldkey');
const oldKeyAsKey = oldkey ? PrivateKey.fromString(oldkey) : operatorKey;
if (oldKeyAsKey == operatorKey) {
console.log('\n-Using operator key as old key');
}
else {
console.log('\n-Using OLD_KEY from env file');
}
console.log('\n-**Account update requires the new keys to sign too**\n');
const proceed = readlineSync.keyInYNStrict('Do you want change the account to a multiSig wallet?');
if (proceed) {
const thresholdKey = new KeyList(keyList, multiSigThreshold);
console.log('The ' + multiSigThreshold + '/' + keyList.length + ' threshold key structure' + thresholdKey);
await updateKeysOnAccount(accountId, oldKeyAsKey, thresholdKey, operatorId, operatorKey, client);
}
else {
console.log('User aborted');
return;
}
}
}
else if (isUpdateAccont) {
// designed as multi sig to multi sig
// pass in an empty keylist as old key and the method will ask for multi sig based on type.
const proceed = readlineSync.keyInYNStrict('Do you want to update the multisig on the wallet?');
if (proceed) {
const thresholdKey = new KeyList(keyList, multiSigThreshold);
console.log('The ' + multiSigThreshold + '/' + keyList.length + ' threshold key structure' + thresholdKey);
await updateKeysOnAccount(accountId, new KeyList(), keyList, operatorId, operatorKey, client);
}
else {
console.log('User aborted');
return;
}
}
}
}
else {
console.log('No eligible arguments supplied - please check usage ruinning the command with a -h switch');
}
}
/**
* Helper function to create new accounts
* @param {PrivateKey} privateKey new accounts private key
* @param {string | number} initialBalance initial balance in hbar
* @param {KeyList} thresholdKey the threshold key
* @param {AccountId} operatorId the account paying for the execution to generate the tx id
* @param {PrivateKey} operatorKey the key to sign for payment
* @param {Client} client
* @returns {AccountId} the nrewly created Account ID object
*/
async function multiSigAccountCreator(initialBalance, thresholdKey, operatorId, operatorKey, client) {
console.log('Creating multisig account');
const acctCreateTx = await new AccountCreateTransaction()
.setInitialBalance(new Hbar(initialBalance))
.setKey(thresholdKey)
.setTransactionId(TransactionId.generate(operatorId))
.setNodeAccountIds(nodeId)
.freezeWith(client);
const signedTx = await acctCreateTx.sign(operatorKey);
const response = await signedTx.execute(client);
const receipt = await response.getReceipt(client);
console.log('Result:', receipt.status.toString());
return receipt.accountId;
}
/**
* Helper function to change keys that can handle multi sig
* @param {AccountId} accountToChange
* @param {Key} oldKey
* @param {Key} newKey
* @param {AccountId} operatorId the account paying for the execution to generate the tx id
* @param {PrivateKey} operatorKey the key to sign for payment
* @param {Client} client configured with the operator who is paying
*/
async function updateKeysOnAccount(accountToChange, oldKey, newKey, operatorId, operatorKey, client) {
let signedTx;
const transaction = new AccountUpdateTransaction()
.setAccountId(accountToChange)
.setKey(newKey)
.setTransactionId(TransactionId.generate(operatorId))
.setNodeAccountIds(nodeId)
.freezeWith(client);
signedTx = await transaction.sign(operatorKey);
// all keys need to sign
if (oldKey instanceof KeyList) {
// multi sig required
signedTx = await requestMultiSig(signedTx);
}
else {
signedTx = await signedTx.sign(oldKey);
}
if (newKey instanceof KeyList) {
// multi sig required
signedTx = await requestMultiSig(signedTx);
}
else {
signedTx = await signedTx.sign(newKey);
}
const txResponse = await signedTx.execute(client);
const receipt = await txResponse.getReceipt(client);
const transactionStatus = receipt.status;
console.log('Account update: ' + transactionStatus.toString());
}
/**
* Encapsulation of transaction processing to get hbar movements
* @param {Transaction} tx
* @returns {string} findings
*/
function getHbarTransfers(tx) {
let outputStr = '';
const hbarTransfers = tx._hbarTransfers;
for (const t in hbarTransfers) {
const hbarTransfer = hbarTransfers[t];
outputStr += '\n\t' + hbarTransfer.accountId.toString() + '\t->\t' + new Hbar(hbarTransfer.amount._valueInTinybar, HbarUnit.Tinybar).toString();
}
return outputStr ? outputStr : 'No Hbar transfers found';
}
/**
* decode the transaction type
* @param {Transaction} transaction
* @returns {string} identified type
*/
function getTransactionType(transaction) {
if (transaction instanceof AccountUpdateTransaction) {
return `Account Update : ${transaction._accountId}`;
}
else if (transaction instanceof TransferTransaction) {
return 'Transfer Transaction';
}
else if (transaction instanceof ContractExecuteTransaction) {
return `${transaction.contractId} : gas -> ${transaction.gas}`;
}
return 'Type unidentifed - please share bytes with the devs';
}
main();