Skip to content

Commit

Permalink
Fix HTLC conversion to and from plain format (#568)
Browse files Browse the repository at this point in the history
* Encode hash algorithm in plain HTLC
* Add test for HTLC conversion to and from plain format
* Update types
  • Loading branch information
sisou authored Oct 12, 2020
1 parent ee768a1 commit d257307
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
2 changes: 2 additions & 0 deletions dist/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,7 @@ export class Hash extends Serializable {
SHA256: 3;
SHA512: 4;
toString(hashAlgorithm: Hash.Algorithm): string;
fromString(str: string): Hash.Algorithm;
};
public static light(arr: Uint8Array): Hash;
public static blake2b(arr: Uint8Array): Hash;
Expand Down Expand Up @@ -1408,6 +1409,7 @@ export class HashedTimeLockedContract extends Contract {
balance: number,
sender: string,
recipient: string,
hashAlgorithm: string,
hashRoot: string,
hashCount: number,
timeout: number,
Expand Down
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), 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.fromString(plain.hashAlgorithm)), plain.hashCount, plain.timeout, plain.totalAmount);
}


Expand Down Expand Up @@ -150,6 +150,7 @@ class HashedTimeLockedContract extends Contract {
const plain = super.toPlain();
plain.sender = this.sender.toPlain();
plain.recipient = this.recipient.toPlain();
plain.hashAlgorithm = Hash.Algorithm.toString(this.hashRoot.algorithm);
plain.hashRoot = this.hashRoot.toPlain();
plain.hashCount = this.hashCount;
plain.timeout = this.timeout;
Expand Down
14 changes: 14 additions & 0 deletions src/main/generic/consensus/base/primitive/Hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,20 @@ Hash.Algorithm.toString = function(hashAlgorithm) {
throw new Error('Invalid hash algorithm');
};

/**
* @param {string} str
* @returns {Hash.Algorithm}
*/
Hash.Algorithm.fromString = function (str) {
switch (str) {
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');
}

/**
* @type {Map<Hash.Algorithm, number>}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,4 +531,27 @@ describe('HashedTimeLockedContract', () => {
expect(plain.creatorPublicKey).toEqual(keyPair.publicKey.toHex());
expect(plain.creatorPathLength).toEqual(0);
});

it('can convert itself to and from plain', () => {
const keyPair1 = KeyPair.generate();
const addr1 = keyPair1.publicKey.toAddress();
const keyPair2 = KeyPair.generate();
const addr2 = keyPair2.publicKey.toAddress();

const hashRoot = Hash.sha256(Hash.NULL.array);

const htlc = new HashedTimeLockedContract(1e5, addr1, addr2, hashRoot, 1, 1000);
const plain = htlc.toPlain();
const revived = HashedTimeLockedContract.fromPlain(plain);

expect(revived.sender.equals(addr1)).toBe(true);
expect(revived.recipient.equals(addr2)).toBe(true);
expect(revived.balance).toBe(1e5);
expect(revived.hashRoot.equals(hashRoot)).toBe(true);
expect(revived.hashRoot.algorithm).toBe(Hash.Algorithm.SHA256);
expect(revived.hashCount).toBe(1);
expect(revived.totalAmount).toBe(1e5);

expect(htlc.equals(revived)).toBe(true);
});
});

0 comments on commit d257307

Please sign in to comment.