forked from BitGo/BitGoJS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransactionOPReturn.js
115 lines (101 loc) · 3.58 KB
/
transactionOPReturn.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
//
// Advanced transaction with OP_RETURN output
// This transaction will send out an OP_RETURN message onto the blockchain
//
// Copyright 2015, BitGo, Inc. All Rights Reserved.
//
const BitGoJS = require('../src/index.js');
const BitcoinJSLib = require('bitgo-bitcoinjs-lib');
if (process.argv.length < 8) {
console.log('usage:\n\t' + process.argv[0] + ' ' + process.argv[1] +
' <user> <pass> <otp> <walletId> <walletPassphrase> <message>');
console.log('user: user email (on test.bitgo.com)');
console.log('pass: password');
console.log('otp: one-time password, 0000000 on test');
console.log('walletId: wallet ID (first address on the wallet)');
console.log('walletPassphrase: passphrase to decrypt the user key');
console.log('message: message to be sent into the blockchain');
process.exit(-1);
}
const user = process.argv[2];
const password = process.argv[3];
const otp = process.argv[4];
const walletId = process.argv[5];
const walletPassphrase = process.argv[6];
const message = process.argv[7];
const bitgo = new BitGoJS.BitGo();
const sendBitcoin = function() {
console.log('Getting wallet..');
// Now get the wallet
bitgo.wallets().get({ id: walletId }, function(err, wallet) {
if (err) {
console.log('Error getting wallet!');
console.dir(err);
return process.exit(-1);
}
console.log('Balance is: ' + (wallet.balance() / 1e8).toFixed(4));
wallet.getEncryptedUserKeychain({}, function(err, keychain) {
if (err) {
console.log('Error getting encrypted keychain!');
console.dir(err);
return process.exit(-1);
}
console.log('Got encrypted user keychain');
// Decrypt the user key with the passphrase
keychain.xprv = bitgo.decrypt({ password: walletPassphrase, input: keychain.encryptedXprv });
const data = new Buffer(message);
const outputScript = BitcoinJSLib.script.nullDataOutput(data);
// Set recipients
const recipients = [];
recipients.push({ script: outputScript, amount: 0.0001 * 1e8 });
console.log('Creating transaction');
wallet.createTransaction({
recipients: recipients
},
function(err, transaction) {
if (err) {
console.log('Failed to create transaction!');
console.dir(err);
return process.exit(-1);
}
console.dir(transaction);
console.log('Signing transaction');
wallet.signTransaction({
transactionHex: transaction.transactionHex,
unspents: transaction.unspents,
keychain: keychain
},
function(err, transaction) {
if (err) {
console.log('Failed to sign transaction!');
console.dir(err);
return process.exit(-1);
}
console.dir(transaction);
console.log('Sending transaction');
wallet.sendTransaction({ tx: transaction.tx }, function(err, callback) {
if (err) {
console.log('Failed to send transaction to BitGo!');
console.dir(err);
return process.exit(-1);
}
console.log('Transaction sent!');
console.dir(callback);
});
});
});
});
});
};
// Authenticate first
bitgo.authenticate({ username: user, password: password, otp: otp }, function(err, result) {
if (err) {
console.dir(err);
throw new Error('Could not authenticate!');
}
console.log('Unlocking account..' );
bitgo.unlock({ otp: otp }, function(err) {
if (err) { console.dir(err); throw new Error('Could not unlock!'); }
sendBitcoin();
});
});