From f8d263908e8c0a2cdf56fc70aaf51fb5e42ad117 Mon Sep 17 00:00:00 2001 From: styppo Date: Mon, 12 Oct 2020 17:44:44 +0200 Subject: [PATCH] Fix HTLC.fromPlain() for HTLC instance arguments --- .../base/account/HashedTimeLockedContract.js | 7 ++++++- src/main/generic/consensus/base/primitive/Hash.js | 11 ++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/main/generic/consensus/base/account/HashedTimeLockedContract.js b/src/main/generic/consensus/base/account/HashedTimeLockedContract.js index 7fced28ae..d028a29e3 100644 --- a/src/main/generic/consensus/base/account/HashedTimeLockedContract.js +++ b/src/main/generic/consensus/base/account/HashedTimeLockedContract.js @@ -73,7 +73,7 @@ class HashedTimeLockedContract extends Contract { */ static fromPlain(plain) { if (!plain) throw new Error('Invalid account'); - return new HashedTimeLockedContract(plain.balance, Address.fromAny(plain.sender), Address.fromAny(plain.recipient), Hash.fromAny(plain.hashRoot, Hash.Algorithm.fromString(plain.hashAlgorithm)), plain.hashCount, plain.timeout, plain.totalAmount); + return new HashedTimeLockedContract(plain.balance, Address.fromAny(plain.sender), Address.fromAny(plain.recipient), Hash.fromAny(plain.hashRoot, Hash.Algorithm.fromAny(plain.hashAlgorithm)), plain.hashCount, plain.timeout, plain.totalAmount); } @@ -119,6 +119,11 @@ class HashedTimeLockedContract extends Contract { return this._recipient; } + /** @type {Hash.Algorithm} */ + get hashAlgorithm() { + return this._hashRoot.algorithm; + } + /** @type {Hash} */ get hashRoot() { return this._hashRoot; diff --git a/src/main/generic/consensus/base/primitive/Hash.js b/src/main/generic/consensus/base/primitive/Hash.js index 4e774d92d..d84d99c15 100644 --- a/src/main/generic/consensus/base/primitive/Hash.js +++ b/src/main/generic/consensus/base/primitive/Hash.js @@ -335,18 +335,19 @@ Hash.Algorithm.toString = function(hashAlgorithm) { }; /** - * @param {string} str + * @param {Hash.Algorithm|string} algorithm * @returns {Hash.Algorithm} */ -Hash.Algorithm.fromString = function (str) { - switch (str) { +Hash.Algorithm.fromAny = function (algorithm) { + if (typeof algorithm === 'number') return algorithm; + switch (algorithm) { case 'blake2b': return Hash.Algorithm.BLAKE2B; case 'argon2d': return Hash.Algorithm.ARGON2D; case 'sha256': return Hash.Algorithm.SHA256; case 'sha512': return Hash.Algorithm.SHA512; } - throw new Error('Invalid string'); -} + throw new Error('Invalid hash algorithm'); +}; /** * @type {Map}