diff --git a/index.d.ts b/index.d.ts index 2d0e50f..e271145 100644 --- a/index.d.ts +++ b/index.d.ts @@ -93,7 +93,7 @@ declare module '@onekeyfe/kaspacore-lib' { export namespace Transaction { static class sighash { - static sign(transaction, privateKey, sighashType, inputIndex, subscript, satoshisBN, flags, signingMethod); + // static sign(transaction, privateKey, sighashType, inputIndex, subscript, satoshisBN, flags, signingMethod); static sighash(transaction, sighashType, inputNumber, subscript, satoshisBN, flags): Buffer; } class UnspentOutput { @@ -155,7 +155,7 @@ declare module '@onekeyfe/kaspacore-lib' { fee(amount: number): this; setVersion(version: number): this; feePerKb(amount: number): this; - sign(privateKey: PrivateKey|PrivateKey[] | string|string[], sigtype:number, signingMethod:string|undefined): this; + // sign(privateKey: PrivateKey|PrivateKey[] | string|string[], sigtype:number, signingMethod:string|undefined): this; applySignature(sig: crypto.Signature): this; addInput(input: Transaction.Input): this; addOutput(output: Transaction.Output): this; diff --git a/lib/transaction/output.js b/lib/transaction/output.js index 8b7e9b6..55ab77f 100644 --- a/lib/transaction/output.js +++ b/lib/transaction/output.js @@ -160,7 +160,7 @@ Output.prototype.inspect = function() { Output.fromBufferReader = function(br) { var obj = {}; - obj.satoshis = BN.fromNumber(br.readUInt64LEBN()).toString(); + obj.satoshis = br.readUInt64LEBN().toString(); var version = br.readUInt16LE(); var size = br.readUInt64LEBN().toNumber(); if (size !== 0) { diff --git a/lib/transaction/sighash.js b/lib/transaction/sighash.js index 2f90ee7..34d1979 100644 --- a/lib/transaction/sighash.js +++ b/lib/transaction/sighash.js @@ -529,6 +529,6 @@ function verify(transaction, signature, publicKey, inputIndex, subscript, satosh */ module.exports = { sighash: sighash, - sign: sign, - verify: verify + // sign: sign, + // verify: verify }; \ No newline at end of file diff --git a/lib/transaction/unspentoutput.js b/lib/transaction/unspentoutput.js index 933cb17..e142cad 100644 --- a/lib/transaction/unspentoutput.js +++ b/lib/transaction/unspentoutput.js @@ -7,6 +7,7 @@ var JSUtil = require('../util/js'); var Script = require('../script'); var Address = require('../address'); var Unit = require('../unit'); +var BN = require('../crypto/bn'); /** * Represents an unspent output information: its script, associated amount and address, @@ -46,8 +47,16 @@ function UnspentOutput(data) { var script = new Script(data.scriptPubKey || data.script); $.checkArgument(!_.isUndefined(data.amount) || !_.isUndefined(data.satoshis), 'Must provide an amount for the output'); - var amount = !_.isUndefined(data.amount) ? new Unit.fromBTC(data.amount).toSatoshis() : data.satoshis; - $.checkArgument(_.isNumber(amount), 'Amount must be a number'); + + var amount; + if (!_.isUndefined(data.amount)) { + amount = BN.fromNumber(data.amount); + amount = amount.mul(new BN(1e8)).toString(); + } else { + amount = BN.fromNumber(data.satoshis).toString(); + } + $.checkArgument(!amount.isNaN() && amount.gte(0) && amount.includes('.'), 'Amount must be a number'); + JSUtil.defineImmutable(this, { address: address, txId: txId, diff --git a/lib/util/js.js b/lib/util/js.js index af9395c..316f3e3 100644 --- a/lib/util/js.js +++ b/lib/util/js.js @@ -3,6 +3,8 @@ var _ = require('lodash'); var logLevels = require('./log').logLevels; +const UINT64_MAX = new BN('18446744073709551615'); + /** * Determines whether a string contains only hexadecimal values * @@ -85,10 +87,22 @@ const JSUtil = { * @return {Boolean} */ isNaturalNumber: function isNaturalNumber(value) { - return typeof value === 'number' && - isFinite(value) && - Math.floor(value) === value && - value >= 0; + if (typeof value === 'string') { + if (!/^\d*$/.test(value)) { + return false; + } + } + + if(typeof value === 'number' || typeof value === 'string'){ + try { + var bnValue = BN.fromNumber(value); + return bnValue.lte(UINT64_MAX); + } catch (e) { + return false; + } + } + + return false; } }; diff --git a/package.json b/package.json index 0f0a0aa..c6a6b0c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@onekeyfe/kaspacore-lib", - "version": "1.0.0-alpha.1", + "version": "1.0.0-alpha.3", "main": "index.js", "authors": [ {