@onekeyfe/kaspa-core-lib 1.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +32 -0
- package/README.md +59 -0
- package/index.d.ts +410 -0
- package/index.js +93 -0
- package/lib/address.js +792 -0
- package/lib/block/block.js +281 -0
- package/lib/block/blockheader.js +296 -0
- package/lib/block/index.js +4 -0
- package/lib/block/merkleblock.js +313 -0
- package/lib/crypto/bn.js +218 -0
- package/lib/crypto/ecdsa.js +296 -0
- package/lib/crypto/hash.js +85 -0
- package/lib/crypto/point.js +166 -0
- package/lib/crypto/random.js +57 -0
- package/lib/crypto/schnorr.js +287 -0
- package/lib/crypto/signature.js +441 -0
- package/lib/encoding/base58.js +70 -0
- package/lib/encoding/base58check.js +95 -0
- package/lib/encoding/bufferreader.js +202 -0
- package/lib/encoding/bufferwriter.js +155 -0
- package/lib/encoding/varint.js +72 -0
- package/lib/errors/index.js +61 -0
- package/lib/errors/spec.js +191 -0
- package/lib/hdprivatekey.js +646 -0
- package/lib/hdpublickey.js +496 -0
- package/lib/networks.js +277 -0
- package/lib/opcode.js +261 -0
- package/lib/privatekey.js +431 -0
- package/lib/publickey.js +375 -0
- package/lib/script/index.js +3 -0
- package/lib/script/interpreter.js +2107 -0
- package/lib/script/script.js +1163 -0
- package/lib/transaction/index.js +7 -0
- package/lib/transaction/input/index.js +6 -0
- package/lib/transaction/input/input.js +375 -0
- package/lib/transaction/input/multisig.js +221 -0
- package/lib/transaction/input/multisigscripthash.js +178 -0
- package/lib/transaction/input/publickey.js +91 -0
- package/lib/transaction/input/publickeyhash.js +100 -0
- package/lib/transaction/output.js +188 -0
- package/lib/transaction/sighash.js +534 -0
- package/lib/transaction/signature.js +89 -0
- package/lib/transaction/transaction.js +1384 -0
- package/lib/transaction/unspentoutput.js +109 -0
- package/lib/unit.js +238 -0
- package/lib/uri.js +223 -0
- package/lib/util/base32.js +63 -0
- package/lib/util/buffer.js +162 -0
- package/lib/util/convertBits.js +62 -0
- package/lib/util/js.js +110 -0
- package/lib/util/log.js +10 -0
- package/lib/util/preconditions.js +34 -0
- package/package.json +62 -0
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _ = require('lodash');
|
|
4
|
+
var inherits = require('inherits');
|
|
5
|
+
var Input = require('./input');
|
|
6
|
+
var Output = require('../output');
|
|
7
|
+
var $ = require('../../util/preconditions');
|
|
8
|
+
|
|
9
|
+
var Script = require('../../script');
|
|
10
|
+
var Signature = require('../../crypto/signature');
|
|
11
|
+
var Sighash = require('../sighash');
|
|
12
|
+
var PublicKey = require('../../publickey');
|
|
13
|
+
var BufferUtil = require('../../util/buffer');
|
|
14
|
+
var TransactionSignature = require('../signature');
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @constructor
|
|
18
|
+
*/
|
|
19
|
+
function MultiSigScriptHashInput(input, pubkeys, threshold, signatures, opts) {
|
|
20
|
+
/* jshint maxstatements:20 */
|
|
21
|
+
opts = opts || {};
|
|
22
|
+
Input.apply(this, arguments);
|
|
23
|
+
var self = this;
|
|
24
|
+
pubkeys = pubkeys || input.publicKeys;
|
|
25
|
+
threshold = threshold || input.threshold;
|
|
26
|
+
signatures = signatures || input.signatures;
|
|
27
|
+
if (opts.noSorting) {
|
|
28
|
+
this.publicKeys = pubkeys
|
|
29
|
+
} else {
|
|
30
|
+
this.publicKeys = _.sortBy(pubkeys, function(publicKey) { return publicKey.toString('hex'); });
|
|
31
|
+
}
|
|
32
|
+
this.redeemScript = Script.buildMultisigOut(this.publicKeys, threshold, opts);
|
|
33
|
+
$.checkState(Script.buildScriptHashOut(this.redeemScript).equals(this.output.script),
|
|
34
|
+
'Provided public keys don\'t hash to the provided output');
|
|
35
|
+
this.publicKeyIndex = {};
|
|
36
|
+
_.each(this.publicKeys, function(publicKey, index) {
|
|
37
|
+
self.publicKeyIndex[publicKey.toString()] = index;
|
|
38
|
+
});
|
|
39
|
+
this.threshold = threshold;
|
|
40
|
+
// Empty array of signatures
|
|
41
|
+
this.signatures = signatures ? this._deserializeSignatures(signatures) : new Array(this.publicKeys.length);
|
|
42
|
+
this.checkBitsField = new Uint8Array(this.publicKeys.length);
|
|
43
|
+
}
|
|
44
|
+
inherits(MultiSigScriptHashInput, Input);
|
|
45
|
+
|
|
46
|
+
MultiSigScriptHashInput.prototype.toObject = function() {
|
|
47
|
+
var obj = Input.prototype.toObject.apply(this, arguments);
|
|
48
|
+
obj.threshold = this.threshold;
|
|
49
|
+
obj.publicKeys = _.map(this.publicKeys, function(publicKey) { return publicKey.toString(); });
|
|
50
|
+
obj.signatures = this._serializeSignatures();
|
|
51
|
+
return obj;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
MultiSigScriptHashInput.prototype._deserializeSignatures = function(signatures) {
|
|
55
|
+
return _.map(signatures, function(signature) {
|
|
56
|
+
if (!signature) {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
return new TransactionSignature(signature);
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
MultiSigScriptHashInput.prototype._serializeSignatures = function() {
|
|
64
|
+
return _.map(this.signatures, function(signature) {
|
|
65
|
+
if (!signature) {
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
return signature.toObject();
|
|
69
|
+
});
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
MultiSigScriptHashInput.prototype.getSignatures = function(transaction, privateKey, index, sigtype, hashData, signingMethod) {
|
|
73
|
+
$.checkState(this.output instanceof Output);
|
|
74
|
+
sigtype = sigtype || (Signature.SIGHASH_ALL | Signature.SIGHASH_FORKID);
|
|
75
|
+
|
|
76
|
+
var self = this;
|
|
77
|
+
var results = [];
|
|
78
|
+
_.each(this.publicKeys, function(publicKey) {
|
|
79
|
+
if (publicKey.toString() === privateKey.publicKey.toString()) {
|
|
80
|
+
results.push(new TransactionSignature({
|
|
81
|
+
publicKey: privateKey.publicKey,
|
|
82
|
+
prevTxId: self.prevTxId,
|
|
83
|
+
outputIndex: self.outputIndex,
|
|
84
|
+
inputIndex: index,
|
|
85
|
+
signature: Sighash.sign(transaction, privateKey, sigtype, index, self.redeemScript, self.output.satoshisBN, undefined, signingMethod),
|
|
86
|
+
sigtype: sigtype
|
|
87
|
+
}));
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
return results;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
MultiSigScriptHashInput.prototype.addSignature = function(transaction, signature, signingMethod) {
|
|
94
|
+
$.checkState(!this.isFullySigned(), 'All needed signatures have already been added');
|
|
95
|
+
$.checkArgument(!_.isUndefined(this.publicKeyIndex[signature.publicKey.toString()]),
|
|
96
|
+
'Signature has no matching public key');
|
|
97
|
+
$.checkState(this.isValidSignature(transaction, signature, signingMethod));
|
|
98
|
+
this.signatures[this.publicKeyIndex[signature.publicKey.toString()]] = signature;
|
|
99
|
+
this.checkBitsField[this.publicKeyIndex[signature.publicKey.toString()]] = (signature !== undefined) ? 1 : 0;
|
|
100
|
+
this._updateScript(signingMethod, this.checkBitsField);
|
|
101
|
+
return this;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
MultiSigScriptHashInput.prototype._updateScript = function(signingMethod, checkBitsField) {
|
|
105
|
+
this.setScript(Script.buildP2SHMultisigIn(
|
|
106
|
+
this.publicKeys,
|
|
107
|
+
this.threshold,
|
|
108
|
+
this._createSignatures(signingMethod),
|
|
109
|
+
{ cachedMultisig: this.redeemScript, checkBits: checkBitsField, signingMethod }
|
|
110
|
+
));
|
|
111
|
+
return this;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
MultiSigScriptHashInput.prototype._createSignatures = function(signingMethod) {
|
|
115
|
+
return _.map(
|
|
116
|
+
_.filter(this.signatures, function(signature) { return !_.isUndefined(signature); }),
|
|
117
|
+
function(signature) {
|
|
118
|
+
return BufferUtil.concat([
|
|
119
|
+
signature.signature.toDER(signingMethod),
|
|
120
|
+
BufferUtil.integerAsSingleByteBuffer(signature.sigtype)
|
|
121
|
+
]);
|
|
122
|
+
}
|
|
123
|
+
);
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
MultiSigScriptHashInput.prototype.clearSignatures = function() {
|
|
127
|
+
this.signatures = new Array(this.publicKeys.length);
|
|
128
|
+
this._updateScript();
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
MultiSigScriptHashInput.prototype.isFullySigned = function() {
|
|
132
|
+
return this.countSignatures() === this.threshold;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
MultiSigScriptHashInput.prototype.countMissingSignatures = function() {
|
|
136
|
+
return this.threshold - this.countSignatures();
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
MultiSigScriptHashInput.prototype.countSignatures = function() {
|
|
140
|
+
return _.reduce(this.signatures, function(sum, signature) {
|
|
141
|
+
return sum + (!!signature);
|
|
142
|
+
}, 0);
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
MultiSigScriptHashInput.prototype.publicKeysWithoutSignature = function() {
|
|
146
|
+
var self = this;
|
|
147
|
+
return _.filter(this.publicKeys, function(publicKey) {
|
|
148
|
+
return !(self.signatures[self.publicKeyIndex[publicKey.toString()]]);
|
|
149
|
+
});
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
MultiSigScriptHashInput.prototype.isValidSignature = function(transaction, signature, signingMethod) {
|
|
153
|
+
// FIXME: Refactor signature so this is not necessary
|
|
154
|
+
signingMethod = signingMethod || "ecdsa";
|
|
155
|
+
signature.signature.nhashtype = signature.sigtype;
|
|
156
|
+
return Sighash.verify(
|
|
157
|
+
transaction,
|
|
158
|
+
signature.signature,
|
|
159
|
+
signature.publicKey,
|
|
160
|
+
signature.inputIndex,
|
|
161
|
+
this.redeemScript,
|
|
162
|
+
this.output.satoshisBN,
|
|
163
|
+
undefined,
|
|
164
|
+
signingMethod
|
|
165
|
+
);
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
MultiSigScriptHashInput.OPCODES_SIZE = 7; // serialized size (<=3) + 0 .. N .. M OP_CHECKMULTISIG
|
|
169
|
+
MultiSigScriptHashInput.SIGNATURE_SIZE = 74; // size (1) + DER (<=72) + sighash (1)
|
|
170
|
+
MultiSigScriptHashInput.PUBKEY_SIZE = 34; // size (1) + DER (<=33)
|
|
171
|
+
|
|
172
|
+
MultiSigScriptHashInput.prototype._estimateSize = function() {
|
|
173
|
+
return MultiSigScriptHashInput.OPCODES_SIZE +
|
|
174
|
+
this.threshold * MultiSigScriptHashInput.SIGNATURE_SIZE +
|
|
175
|
+
this.publicKeys.length * MultiSigScriptHashInput.PUBKEY_SIZE;
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
module.exports = MultiSigScriptHashInput;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var inherits = require('inherits');
|
|
4
|
+
|
|
5
|
+
var $ = require('../../util/preconditions');
|
|
6
|
+
var BufferUtil = require('../../util/buffer');
|
|
7
|
+
|
|
8
|
+
var Input = require('./input');
|
|
9
|
+
var Output = require('../output');
|
|
10
|
+
var Sighash = require('../sighash');
|
|
11
|
+
var Script = require('../../script');
|
|
12
|
+
var Signature = require('../../crypto/signature');
|
|
13
|
+
var TransactionSignature = require('../signature');
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Represents a special kind of input of PayToPublicKey kind.
|
|
17
|
+
* @constructor
|
|
18
|
+
*/
|
|
19
|
+
function PublicKeyInput() {
|
|
20
|
+
Input.apply(this, arguments);
|
|
21
|
+
}
|
|
22
|
+
inherits(PublicKeyInput, Input);
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @param {Transaction} transaction - the transaction to be signed
|
|
26
|
+
* @param {PrivateKey} privateKey - the private key with which to sign the transaction
|
|
27
|
+
* @param {number} index - the index of the input in the transaction input vector
|
|
28
|
+
* @param {number=} sigtype - the type of signature, defaults to Signature.SIGHASH_ALL
|
|
29
|
+
* @param {String} signingMethod - the signing method used to sign tx "ecdsa" or "schnorr"
|
|
30
|
+
* @return {Array} of objects that can be
|
|
31
|
+
*/
|
|
32
|
+
PublicKeyInput.prototype.getSignatures = function(transaction, privateKey, index, sigtype, hashData, signingMethod) {
|
|
33
|
+
$.checkState(this.output instanceof Output);
|
|
34
|
+
sigtype = sigtype || (Signature.SIGHASH_ALL | Signature.SIGHASH_FORKID);
|
|
35
|
+
var publicKey = privateKey.toPublicKey();
|
|
36
|
+
if (publicKey.toString() === this.output.script.getPublicKey().toString('hex')) {
|
|
37
|
+
return [new TransactionSignature({
|
|
38
|
+
publicKey: publicKey,
|
|
39
|
+
prevTxId: this.prevTxId,
|
|
40
|
+
outputIndex: this.outputIndex,
|
|
41
|
+
inputIndex: index,
|
|
42
|
+
signature: Sighash.sign(transaction, privateKey, sigtype, index, this.output.script, this.output.satoshisBN, undefined, signingMethod),
|
|
43
|
+
sigtype: sigtype
|
|
44
|
+
})];
|
|
45
|
+
}
|
|
46
|
+
return [];
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Add the provided signature
|
|
51
|
+
*
|
|
52
|
+
* @param {Object} signature
|
|
53
|
+
* @param {PublicKey} signature.publicKey
|
|
54
|
+
* @param {Signature} signature.signature
|
|
55
|
+
* @param {number=} signature.sigtype
|
|
56
|
+
* @param {String} signingMethod - the method used in signing the tx "ecdsa" or "schnorr"
|
|
57
|
+
* @return {PublicKeyInput} this, for chaining
|
|
58
|
+
*/
|
|
59
|
+
PublicKeyInput.prototype.addSignature = function(transaction, signature, signingMethod) {
|
|
60
|
+
$.checkState(this.isValidSignature(transaction, signature, signingMethod), 'Signature is invalid');
|
|
61
|
+
this.setScript(Script.buildPublicKeyIn(
|
|
62
|
+
signature.signature.toDER(signingMethod),
|
|
63
|
+
signature.sigtype
|
|
64
|
+
));
|
|
65
|
+
return this;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Clear the input's signature
|
|
70
|
+
* @return {PublicKeyInput} this, for chaining
|
|
71
|
+
*/
|
|
72
|
+
PublicKeyInput.prototype.clearSignatures = function() {
|
|
73
|
+
this.setScript(Script.empty());
|
|
74
|
+
return this;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Query whether the input is signed
|
|
79
|
+
* @return {boolean}
|
|
80
|
+
*/
|
|
81
|
+
PublicKeyInput.prototype.isFullySigned = function() {
|
|
82
|
+
return this.script.isPublicKeyIn();
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
PublicKeyInput.SCRIPT_MAX_SIZE = 73; // sigsize (1 + 72)
|
|
86
|
+
|
|
87
|
+
PublicKeyInput.prototype._estimateSize = function() {
|
|
88
|
+
return PublicKeyInput.SCRIPT_MAX_SIZE;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
module.exports = PublicKeyInput;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var inherits = require('inherits');
|
|
4
|
+
|
|
5
|
+
var $ = require('../../util/preconditions');
|
|
6
|
+
var BufferUtil = require('../../util/buffer');
|
|
7
|
+
|
|
8
|
+
var Hash = require('../../crypto/hash');
|
|
9
|
+
var Input = require('./input');
|
|
10
|
+
var Output = require('../output');
|
|
11
|
+
var Sighash = require('../sighash');
|
|
12
|
+
var Script = require('../../script');
|
|
13
|
+
var Signature = require('../../crypto/signature');
|
|
14
|
+
var TransactionSignature = require('../signature');
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Represents a special kind of input of PayToPublicKeyHash kind.
|
|
18
|
+
* @constructor
|
|
19
|
+
*/
|
|
20
|
+
function PublicKeyHashInput() {
|
|
21
|
+
Input.apply(this, arguments);
|
|
22
|
+
}
|
|
23
|
+
inherits(PublicKeyHashInput, Input);
|
|
24
|
+
|
|
25
|
+
/* jshint maxparams: 5 */
|
|
26
|
+
/**
|
|
27
|
+
* @param {Transaction} transaction - the transaction to be signed
|
|
28
|
+
* @param {PrivateKey} privateKey - the private key with which to sign the transaction
|
|
29
|
+
* @param {number} index - the index of the input in the transaction input vector
|
|
30
|
+
* @param {number=} sigtype - the type of signature, defaults to Signature.SIGHASH_ALL
|
|
31
|
+
* @param {Buffer=} hashData - the precalculated hash of the public key associated with the privateKey provided
|
|
32
|
+
* @param {String} signingMethod - the signing method used to sign tx "ecdsa" or "schnorr"
|
|
33
|
+
* @return {Array} of objects that can be
|
|
34
|
+
*/
|
|
35
|
+
PublicKeyHashInput.prototype.getSignatures = function(transaction, privateKey, index, sigtype, hashData, signingMethod) {
|
|
36
|
+
$.checkState(this.output instanceof Output);
|
|
37
|
+
hashData = hashData || Hash.sha256ripemd160(privateKey.publicKey.toBuffer());
|
|
38
|
+
sigtype = sigtype || (Signature.SIGHASH_ALL | Signature.SIGHASH_FORKID);
|
|
39
|
+
|
|
40
|
+
//if (BufferUtil.equals(hashData, this.output.script.getPublicKeyHash())) {
|
|
41
|
+
return [new TransactionSignature({
|
|
42
|
+
publicKey: privateKey.publicKey,
|
|
43
|
+
prevTxId: this.prevTxId,
|
|
44
|
+
outputIndex: this.outputIndex,
|
|
45
|
+
inputIndex: index,
|
|
46
|
+
signature: Sighash.sign(transaction, privateKey, sigtype, index, this.output.script, this.output.satoshisBN, undefined, signingMethod),
|
|
47
|
+
sigtype: sigtype
|
|
48
|
+
})];
|
|
49
|
+
//}
|
|
50
|
+
//return [];
|
|
51
|
+
};
|
|
52
|
+
/* jshint maxparams: 3 */
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Add the provided signature
|
|
56
|
+
*
|
|
57
|
+
* @param {Object} signature
|
|
58
|
+
* @param {PublicKey} signature.publicKey
|
|
59
|
+
* @param {Signature} signature.signature
|
|
60
|
+
* @param {number=} signature.sigtype
|
|
61
|
+
* @param {String} signingMethod "ecdsa" or "schnorr"
|
|
62
|
+
* @return {PublicKeyHashInput} this, for chaining
|
|
63
|
+
*/
|
|
64
|
+
PublicKeyHashInput.prototype.addSignature = function(transaction, signature, signingMethod) {
|
|
65
|
+
|
|
66
|
+
$.checkState(this.isValidSignature(transaction, signature, signingMethod), 'Signature is invalid');
|
|
67
|
+
|
|
68
|
+
this.setScript(Script.buildPublicKeyHashIn(
|
|
69
|
+
signature.publicKey,
|
|
70
|
+
signature.signature.toDER(signingMethod),
|
|
71
|
+
signature.sigtype
|
|
72
|
+
));
|
|
73
|
+
return this;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Clear the input's signature
|
|
78
|
+
* @return {PublicKeyHashInput} this, for chaining
|
|
79
|
+
*/
|
|
80
|
+
PublicKeyHashInput.prototype.clearSignatures = function() {
|
|
81
|
+
debugger
|
|
82
|
+
this.setScript(Script.empty());
|
|
83
|
+
return this;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Query whether the input is signed
|
|
88
|
+
* @return {boolean}
|
|
89
|
+
*/
|
|
90
|
+
PublicKeyHashInput.prototype.isFullySigned = function() {
|
|
91
|
+
return this.script.isPublicKeyHashIn();
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
PublicKeyHashInput.SCRIPT_MAX_SIZE = 73 + 34; // sigsize (1 + 72) + pubkey (1 + 33)
|
|
95
|
+
|
|
96
|
+
PublicKeyHashInput.prototype._estimateSize = function() {
|
|
97
|
+
return PublicKeyHashInput.SCRIPT_MAX_SIZE;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
module.exports = PublicKeyHashInput;
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _ = require('lodash');
|
|
4
|
+
var BN = require('../crypto/bn');
|
|
5
|
+
var buffer = require('buffer');
|
|
6
|
+
var bufferUtil = require('../util/buffer');
|
|
7
|
+
var JSUtil = require('../util/js');
|
|
8
|
+
var BufferWriter = require('../encoding/bufferwriter');
|
|
9
|
+
var Script = require('../script');
|
|
10
|
+
var $ = require('../util/preconditions');
|
|
11
|
+
var errors = require('../errors');
|
|
12
|
+
|
|
13
|
+
var MAX_SAFE_INTEGER = 0x1fffffffffffff;
|
|
14
|
+
|
|
15
|
+
function Output(args) {
|
|
16
|
+
if (!(this instanceof Output)) {
|
|
17
|
+
return new Output(args);
|
|
18
|
+
}
|
|
19
|
+
if (_.isObject(args)) {
|
|
20
|
+
this.satoshis = args.satoshis;
|
|
21
|
+
if (bufferUtil.isBuffer(args.script)) {
|
|
22
|
+
this._scriptBuffer = args.script;
|
|
23
|
+
} else {
|
|
24
|
+
var script;
|
|
25
|
+
if (_.isString(args.script) && JSUtil.isHexa(args.script)) {
|
|
26
|
+
script = Buffer.from(args.script, 'hex');
|
|
27
|
+
} else {
|
|
28
|
+
script = args.script;
|
|
29
|
+
}
|
|
30
|
+
this.setScript(script);
|
|
31
|
+
}
|
|
32
|
+
} else {
|
|
33
|
+
throw new TypeError('Unrecognized argument for Output');
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
Object.defineProperty(Output.prototype, 'script', {
|
|
38
|
+
configurable: false,
|
|
39
|
+
enumerable: true,
|
|
40
|
+
get: function() {
|
|
41
|
+
if (this._script) {
|
|
42
|
+
return this._script;
|
|
43
|
+
} else {
|
|
44
|
+
this.setScriptFromBuffer(this._scriptBuffer);
|
|
45
|
+
return this._script;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
Object.defineProperty(Output.prototype, 'satoshis', {
|
|
52
|
+
configurable: false,
|
|
53
|
+
enumerable: true,
|
|
54
|
+
get: function() {
|
|
55
|
+
return this._satoshis;
|
|
56
|
+
},
|
|
57
|
+
set: function(num) {
|
|
58
|
+
if (num instanceof BN) {
|
|
59
|
+
this._satoshisBN = num;
|
|
60
|
+
this._satoshis = num.toString();
|
|
61
|
+
} else if (_.isString(num)) {
|
|
62
|
+
this._satoshisBN = BN.fromString(num);
|
|
63
|
+
this._satoshis = num;
|
|
64
|
+
} else {
|
|
65
|
+
$.checkArgument(
|
|
66
|
+
JSUtil.isNaturalNumber(num),
|
|
67
|
+
'Output satoshis is not a natural number'
|
|
68
|
+
);
|
|
69
|
+
this._satoshisBN = BN.fromNumber(num);
|
|
70
|
+
this._satoshis = BN.fromNumber(num).toString();
|
|
71
|
+
}
|
|
72
|
+
$.checkState(
|
|
73
|
+
JSUtil.isNaturalNumber(this._satoshis),
|
|
74
|
+
'Output satoshis is not a natural number'
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
Output.prototype.invalidSatoshis = function() {
|
|
80
|
+
if(!BN.fromString(this._satoshis).eq(this._satoshisBN)) {
|
|
81
|
+
return 'transaction txout satoshis has corrupted value';
|
|
82
|
+
}
|
|
83
|
+
if(BN.fromString(this._satoshis).lt(new BN(0))) {
|
|
84
|
+
return 'transaction txout negative';
|
|
85
|
+
}
|
|
86
|
+
return false;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
Object.defineProperty(Output.prototype, 'satoshisBN', {
|
|
91
|
+
configurable: false,
|
|
92
|
+
enumerable: true,
|
|
93
|
+
get: function() {
|
|
94
|
+
return this._satoshisBN;
|
|
95
|
+
},
|
|
96
|
+
set: function(num) {
|
|
97
|
+
this._satoshisBN = num;
|
|
98
|
+
this._satoshis = num.toNumber();
|
|
99
|
+
$.checkState(
|
|
100
|
+
JSUtil.isNaturalNumber(this._satoshis),
|
|
101
|
+
'Output satoshis is not a natural number'
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
Output.prototype.toObject = Output.prototype.toJSON = function toObject() {
|
|
108
|
+
var obj = {
|
|
109
|
+
satoshis: this.satoshis
|
|
110
|
+
};
|
|
111
|
+
obj.script = this._scriptBuffer.toString('hex');
|
|
112
|
+
return obj;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
Output.fromObject = function(data) {
|
|
116
|
+
return new Output(data);
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
Output.prototype.setScriptFromBuffer = function(buffer) {
|
|
120
|
+
this._scriptBuffer = buffer;
|
|
121
|
+
try {
|
|
122
|
+
this._script = Script.fromBuffer(this._scriptBuffer);
|
|
123
|
+
this._script._isOutput = true;
|
|
124
|
+
} catch (e) {
|
|
125
|
+
if (e instanceof errors.Script.InvalidBuffer) {
|
|
126
|
+
this._script = null;
|
|
127
|
+
} else {
|
|
128
|
+
throw e;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
Output.prototype.setScript = function(script) {
|
|
134
|
+
if (script instanceof Script) {
|
|
135
|
+
this._scriptBuffer = script.toBuffer();
|
|
136
|
+
this._script = script;
|
|
137
|
+
this._script._isOutput = true;
|
|
138
|
+
} else if (_.isString(script)) {
|
|
139
|
+
this._script = Script.fromString(script);
|
|
140
|
+
this._scriptBuffer = this._script.toBuffer();
|
|
141
|
+
this._script._isOutput = true;
|
|
142
|
+
} else if (bufferUtil.isBuffer(script)) {
|
|
143
|
+
this.setScriptFromBuffer(script);
|
|
144
|
+
} else {
|
|
145
|
+
throw new TypeError('Invalid argument type: script');
|
|
146
|
+
}
|
|
147
|
+
return this;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
Output.prototype.inspect = function() {
|
|
151
|
+
var scriptStr;
|
|
152
|
+
if (this.script) {
|
|
153
|
+
scriptStr = this.script.inspect();
|
|
154
|
+
} else {
|
|
155
|
+
scriptStr = this._scriptBuffer.toString('hex');
|
|
156
|
+
}
|
|
157
|
+
return '<Output (' + this.satoshis + ' sats) ' + scriptStr + '>';
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
Output.fromBufferReader = function(br) {
|
|
162
|
+
var obj = {};
|
|
163
|
+
obj.satoshis = br.readUInt64LEBN().toString();
|
|
164
|
+
var version = br.readUInt16LE();
|
|
165
|
+
var size = br.readUInt64LEBN().toNumber();
|
|
166
|
+
if (size !== 0) {
|
|
167
|
+
obj.script = br.read(size);
|
|
168
|
+
} else {
|
|
169
|
+
obj.script = Buffer.from([]);
|
|
170
|
+
}
|
|
171
|
+
return new Output(obj);
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
Output.prototype.toBufferWriter = function(writer) {
|
|
176
|
+
if (!writer) {
|
|
177
|
+
writer = new BufferWriter();
|
|
178
|
+
}
|
|
179
|
+
writer.writeUInt64LEBN(this._satoshisBN); //value/amount
|
|
180
|
+
writer.writeUInt16LE(0); //output.ScriptPublicKey.Version (uint 16)
|
|
181
|
+
var script = this._scriptBuffer;
|
|
182
|
+
let bn = BN.fromNumber(script.length);
|
|
183
|
+
writer.writeUInt64LEBN(bn); //script length
|
|
184
|
+
writer.write(script); //script
|
|
185
|
+
return writer;
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
module.exports = Output;
|