-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathTransactionBuilder.js
More file actions
62 lines (54 loc) · 1.57 KB
/
TransactionBuilder.js
File metadata and controls
62 lines (54 loc) · 1.57 KB
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
import Bitcoin from 'bitcoincashjs-lib';
import bchaddr from 'bchaddrjs';
import coininfo from'coininfo';
import bip68 from 'bip68';
class TransactionBuilder {
constructor(network = 'bitcoincash') {
let bitcoincash;
if(network === 'bitcoincash') {
bitcoincash = coininfo.bitcoincash.main;
} else {
bitcoincash = coininfo.bitcoincash.test;
}
let bitcoincashBitcoinJSLib = bitcoincash.toBitcoinJS();
this.transaction = new Bitcoin.TransactionBuilder(bitcoincashBitcoinJSLib);
this.DEFAULT_SEQUENCE = 0xffffffff;
this.hashTypes = {
SIGHASH_ALL: 0x01,
SIGHASH_NONE: 0x02,
SIGHASH_SINGLE: 0x03,
SIGHASH_ANYONECANPAY: 0x80,
SIGHASH_BITCOINCASH_BIP143: 0x40,
ADVANCED_TRANSACTION_MARKER: 0x00,
ADVANCED_TRANSACTION_FLAG: 0x01
};
this.bip68 = bip68;
}
addInput(txHash, vout, sequence = this.DEFAULT_SEQUENCE, prevOutScript) {
this.transaction.addInput(
txHash,
vout,
sequence,
prevOutScript
);
}
addOutput(scriptPubKey, amount) {
try {
this.transaction.addOutput(bchaddr.toLegacyAddress(scriptPubKey), amount);
}
catch(error) {
this.transaction.addOutput(scriptPubKey, amount);
}
}
setLockTime(locktime) {
this.transaction.setLockTime(locktime);
}
sign(vin, keyPair, redeemScript, hashType = this.hashTypes.SIGHASH_ALL, value) {
let witnessScript;
this.transaction.sign(vin, keyPair, redeemScript, hashType, value, witnessScript);
}
build() {
return this.transaction.build();
}
}
export default TransactionBuilder;