-
Notifications
You must be signed in to change notification settings - Fork 4
/
sign_offline_transfer.js
82 lines (70 loc) · 1.96 KB
/
sign_offline_transfer.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
const steem = require('@steemit/steem-js');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let refBlockNum = 0;
let prefix = 0;
let fromAccount = '';
let toAccount = '';
let amountToSend = '';
let privateKey = '';
let transaction = {
ref_block_num: 0,
ref_block_prefix: 0,
expiration: '',
operations: [],
extensions: []
}
let time = new Date();
time.setHours(time.getHours() + 1);
let expirationTime = time.toISOString().split('.')[0];
async function handleArgs() {
if (process.argv.length != 7)
handleError('Usage: node sign_offline_transfer.js <ref block num> <prefix> <from account> <to account> <amount>');
refBlockNum = process.argv[2];
prefix = process.argv[3];
fromAccount = process.argv[4];
toAccount = process.argv[5];
amountToSend = process.argv[6];
}
async function buildTransaction() {
transaction.ref_block_num = parseInt(refBlockNum);
transaction.ref_block_prefix = parseInt(prefix);
transaction.expiration = expirationTime;
transaction.operations = [[
'transfer',
{
from: fromAccount,
to: toAccount,
amount: amountToSend + ' STEEM',
memo: ''
}
]];
}
async function signTransaction() {
console.log('Signed transaction: \n');
console.log(JSON.stringify(steem.auth.signTransaction(transaction, [privateKey])));
console.log('\n');
process.exit(0);
}
function waitInputFromTerminal(questionText) {
return new Promise((resolve, reject) => {
rl.question(questionText, (input) => resolve(input) );
});
}
async function getWifFromTerminal() {
privateKey = await waitInputFromTerminal('Please enter your private WIF: ');
if(!steem.auth.isWif(privateKey)) {
handleError('This is not a valid WIF');
}
}
function handleError(err) {
console.log(`Error: ${err}`);
process.exit(1);
}
handleArgs()
.then(getWifFromTerminal)
.then(buildTransaction)
.then(signTransaction);