Skip to content

Commit

Permalink
Fix HTLC.fromPlain() for HTLC instance arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
styppo committed Oct 12, 2020
1 parent d257307 commit f8d2639
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down Expand Up @@ -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;
Expand Down
11 changes: 6 additions & 5 deletions src/main/generic/consensus/base/primitive/Hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Hash.Algorithm, number>}
Expand Down

0 comments on commit f8d2639

Please sign in to comment.