@helios-lang/ledger
Advanced tools
Comparing version 0.2.2 to 0.2.3
{ | ||
"name": "@helios-lang/ledger", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"description": "Ledger types (eg. for building transactions)", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -101,3 +101,8 @@ import { | ||
/** | ||
* Number of bytes | ||
* Number of bytes of CBOR encoding of Tx | ||
* | ||
* Is used for two things: | ||
* - tx fee calculation | ||
* - tx size validation | ||
* | ||
* @param {boolean} forFeeCalculation - see comment in `this.toCbor()` | ||
@@ -107,3 +112,17 @@ * @returns {number} | ||
calcSize(forFeeCalculation = false) { | ||
return this.toCbor(forFeeCalculation).length | ||
// add dummy signatures to make sure the tx has the correct size | ||
let nDummy = 0 | ||
if (forFeeCalculation) { | ||
nDummy = this.countMissingSignatures() | ||
this.witnesses.addDummySignatures(nDummy) | ||
} | ||
const s = this.toCbor(forFeeCalculation).length | ||
if (forFeeCalculation) { | ||
this.witnesses.removeDummySignatures(nDummy) | ||
} | ||
return s | ||
} | ||
@@ -171,7 +190,2 @@ | ||
// add dummy signatures to make sure the tx has the correct size | ||
if (!this.valid) { | ||
this.witnesses.addDummySignatures(this.body.countUniqueSigners()) | ||
} | ||
const [a, b] = helper.txFeeParams | ||
@@ -181,7 +195,2 @@ | ||
// clean up the dummy signatures | ||
if (!this.valid) { | ||
this.witnesses.removeDummySignatures() | ||
} | ||
const exFee = this.witnesses.calcExFee(params) | ||
@@ -387,2 +396,13 @@ | ||
/** | ||
* @private | ||
* @returns {number} | ||
*/ | ||
countMissingSignatures() { | ||
return ( | ||
this.body.countUniqueSigners() - | ||
this.witnesses.countNonDummySignatures() | ||
) | ||
} | ||
/** | ||
* Throws an error if there isn't enough collateral | ||
@@ -389,0 +409,0 @@ * Also throws an error if the script doesn't require collateral, but collateral was actually included |
@@ -296,7 +296,13 @@ import { | ||
let nWorstCase = 0 | ||
this.inputs.concat(this.collateral).forEach((utxo) => { | ||
const pubKeyHash = utxo.output.address.pubKeyHash | ||
try { | ||
const pubKeyHash = utxo.output.address.pubKeyHash | ||
if (pubKeyHash) { | ||
set.add(pubKeyHash.toHex()) | ||
if (pubKeyHash) { | ||
set.add(pubKeyHash.toHex()) | ||
} | ||
} catch (_e) { | ||
nWorstCase += 1 | ||
} | ||
@@ -307,3 +313,3 @@ }) | ||
return set.size | ||
return set.size + nWorstCase | ||
} | ||
@@ -310,0 +316,0 @@ |
@@ -147,8 +147,10 @@ import { | ||
* Used to calculate the correct min fee | ||
* @param {number} n | ||
* @param {number} n - number of dummy signatures to add | ||
*/ | ||
addDummySignatures(n) { | ||
const n0 = this.signatures.length | ||
if (n == 0) { | ||
return | ||
} | ||
for (let i = n0; i < n; i++) { | ||
for (let i = 0; i < n; i++) { | ||
this.signatures.push(Signature.dummy()) | ||
@@ -185,2 +187,9 @@ } | ||
/** | ||
* @returns {number} | ||
*/ | ||
countNonDummySignatures() { | ||
return this.signatures.reduce((n, s) => (s.isDummy() ? n : n + 1), 0) | ||
} | ||
/** | ||
* @returns {Object} | ||
@@ -265,7 +274,32 @@ */ | ||
* Used to removed any dummy signatures added while calculating the tx fee | ||
* @param {number} n | ||
*/ | ||
removeDummySignatures() { | ||
this.signatures = this.signatures.filter( | ||
(signature) => !signature.isDummy() | ||
) | ||
removeDummySignatures(n) { | ||
if (n == 0) { | ||
return | ||
} | ||
/** | ||
* @type {Signature[]} | ||
*/ | ||
const res = [] | ||
let j = 0 | ||
for (let i = 0; i < this.signatures.length; i++) { | ||
const signature = this.signatures[i] | ||
if (signature.isDummy() && j < n) { | ||
j++ | ||
} else { | ||
res.push(signature) | ||
} | ||
} | ||
if (j != n) { | ||
throw new Error( | ||
`internal error: unable to remove ${n} dummy signatures` | ||
) | ||
} | ||
this.signatures = res | ||
} | ||
@@ -272,0 +306,0 @@ |
@@ -53,3 +53,8 @@ /** | ||
/** | ||
* Number of bytes | ||
* Number of bytes of CBOR encoding of Tx | ||
* | ||
* Is used for two things: | ||
* - tx fee calculation | ||
* - tx size validation | ||
* | ||
* @param {boolean} forFeeCalculation - see comment in `this.toCbor()` | ||
@@ -168,2 +173,7 @@ * @returns {number} | ||
/** | ||
* @private | ||
* @returns {number} | ||
*/ | ||
private countMissingSignatures; | ||
/** | ||
* Throws an error if there isn't enough collateral | ||
@@ -170,0 +180,0 @@ * Also throws an error if the script doesn't require collateral, but collateral was actually included |
@@ -72,3 +72,3 @@ /** | ||
* Used to calculate the correct min fee | ||
* @param {number} n | ||
* @param {number} n - number of dummy signatures to add | ||
*/ | ||
@@ -86,2 +86,6 @@ addDummySignatures(n: number): void; | ||
/** | ||
* @returns {number} | ||
*/ | ||
countNonDummySignatures(): number; | ||
/** | ||
* @returns {Object} | ||
@@ -105,4 +109,5 @@ */ | ||
* Used to removed any dummy signatures added while calculating the tx fee | ||
* @param {number} n | ||
*/ | ||
removeDummySignatures(): void; | ||
removeDummySignatures(n: number): void; | ||
/** | ||
@@ -109,0 +114,0 @@ * @returns {number[]} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
647692
18524