Skip to content

Commit

Permalink
fix: unspentoutput number
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteZhang1024 committed Jul 29, 2024
1 parent b97daca commit c020c4e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
7 changes: 4 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ declare module '@onekeyfe/kaspacore-lib' {
readonly txId: string;
readonly outputIndex: number;
readonly script: Script;
readonly satoshis: number;
readonly satoshis: number | string;

constructor(data: object);

Expand All @@ -114,7 +114,8 @@ declare module '@onekeyfe/kaspacore-lib' {

class Output {
readonly script: Script;
readonly satoshis: number;
readonly satoshis: number | string;
readonly satoshisBN: crypto.BN;

constructor(data: object);

Expand Down Expand Up @@ -149,7 +150,7 @@ declare module '@onekeyfe/kaspacore-lib' {
constructor(serialized ? : any);

from(utxos: Transaction.UnspentOutput[]): this;
to(address: Address[] | Address | string, amount: number): this;
to(address: Address[] | Address | string, amount: number | string): this;
change(address: Address | string): this;
fee(amount: number): this;
setVersion(version: number): this;
Expand Down
11 changes: 8 additions & 3 deletions lib/crypto/bn.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ BN.Minus1 = new BN(-1);

BN.fromNumber = function(n) {
$.checkArgument(_.isNumber(n));
if(n <= 0x1fffffffffffff){
return new BN(n);
if (_.isNumber(n)) {
if (n <= 0x1fffffffffffff) {
return new BN(n);
}
return new BN(n.toString(16), 16);
} else if (_.isString(n)) {
return new BN(n, 10);
}
return new BN(n.toString(16), 16);
throw new Error('Invalid input type');
};

BN.fromString = function(str, base) {
Expand Down
17 changes: 7 additions & 10 deletions lib/transaction/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,17 @@ Object.defineProperty(Output.prototype, 'satoshis', {
set: function(num) {
if (num instanceof BN) {
this._satoshisBN = num;
this._satoshis = num.toNumber();
this._satoshis = num.toString();
} else if (_.isString(num)) {
this._satoshis = parseInt(num);
this._satoshisBN = BN.fromNumber(this._satoshis);
this._satoshisBN = BN.fromString(num);
this._satoshis = num;
} else {
$.checkArgument(
JSUtil.isNaturalNumber(num),
'Output satoshis is not a natural number'
);
this._satoshisBN = BN.fromNumber(num);
this._satoshis = num;
this._satoshis = BN.fromNumber(num).toString();
}
$.checkState(
JSUtil.isNaturalNumber(this._satoshis),
Expand All @@ -77,13 +77,10 @@ Object.defineProperty(Output.prototype, 'satoshis', {
});

Output.prototype.invalidSatoshis = function() {
if (this._satoshis > MAX_SAFE_INTEGER) {
return 'transaction txout satoshis greater than max safe integer';
}
if (this._satoshis !== this._satoshisBN.toNumber()) {
if(!BN.fromString(this._satoshis).eq(this._satoshisBN)) {
return 'transaction txout satoshis has corrupted value';
}
if (this._satoshis < 0) {
if(BN.fromString(this._satoshis).lt(new BN(0))) {
return 'transaction txout negative';
}
return false;
Expand Down Expand Up @@ -163,7 +160,7 @@ Output.prototype.inspect = function() {

Output.fromBufferReader = function(br) {
var obj = {};
obj.satoshis = br.readUInt64LEBN();
obj.satoshis = BN.fromNumber(br.readUInt64LEBN()).toString();
var version = br.readUInt16LE();
var size = br.readUInt64LEBN().toNumber();
if (size !== 0) {
Expand Down
4 changes: 2 additions & 2 deletions lib/transaction/unspentoutput.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var Unit = require('../unit');
* @param {string|Script} data.scriptPubKey the script that must be resolved to release the funds
* @param {string|Script=} data.script alias for `scriptPubKey`
* @param {number} data.amount amount of bitcoins associated
* @param {number=} data.satoshis alias for `amount`, but expressed in satoshis (1 BTC = 1e8 satoshis)
* @param {string|number=} data.satoshis alias for `amount`, but expressed in satoshis (1 BTC = 1e8 satoshis)
* @param {string|Address=} data.address the associated address to the script, if provided
*/
function UnspentOutput(data) {
Expand Down Expand Up @@ -53,7 +53,7 @@ function UnspentOutput(data) {
txId: txId,
outputIndex: outputIndex,
script: script,
satoshis: amount
satoshis: amount.toString()
});
}

Expand Down

0 comments on commit c020c4e

Please sign in to comment.