-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransaction.js
49 lines (38 loc) · 1.48 KB
/
transaction.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
var bitcore = require("bitcore-lib");
var Insight = require("bitcore-explorers").Insight;
//pubkey mibK5jk9eP7EkLH175RSPGTLR27zphvvxa
var privateKeyWIF = 'cQN511BWtc2dSUMWySmZpr6ShY1un4WK42JegGwkSFX5a8n9GWr3';
//pub key2 : mfdqfjQXaTzYM9hYTBa7G4We6mL443UEwq
//var privateKeyWIF = '925V8LkbBYjqiYXotdS5f1Hxido46hg1btoYb2m4bamAQx4o3iv';
//var privKey = 'tpubDDU119ipMf4tx5eDdpkMULuwBeBBe2h3K5VgMGTR75H2UzKjhboSRf3ECruKmDNatqqMgopFF3NqKjJFzcKXsdDLXWnX1ko8hqLvz9oL9HT';
var privateKey = bitcore.PrivateKey.fromWIF(privateKeyWIF);
var sourceAddress = privateKey.toAddress(bitcore.Networks.testnet);
console.log(sourceAddress);
var targetAddress = bitcore.Address.fromString('n3gT6yjDmDt3HatG7HwgFCy6NrsmqKPtCi');
if (!bitcore.Address.isValid(targetAddress)) {
console.log('adr invalid');
} else {
console.log('OK adr valid');
}
var insight = new Insight("testnet");
insight.getUnspentUtxos(sourceAddress, function (error, utxos) {
if (error) {
console.log(error);
} else {
console.log(utxos);
// transaction code goes here
var tx = new bitcore.Transaction();
tx.from(utxos);
tx.to(targetAddress, 5700);
tx.change(sourceAddress);
tx.sign(privateKey);
var serializedTX = tx.serialize();
insight.broadcast(serializedTX, function (error, transactionId) {
if (error) {
console.log(error);
} else {
console.log(transactionId);
}
});
}
});