Skip to content

Commit

Permalink
fix(crypto): validate htlc-lock recipient network (#4275)
Browse files Browse the repository at this point in the history
  • Loading branch information
rainydio authored Jan 22, 2021
1 parent a2bcbac commit 210b419
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { configManager } from "@packages/crypto/src/managers";
import { BuilderFactory } from "@packages/crypto/src/transactions";
import { HtlcLockBuilder } from "@packages/crypto/src/transactions/builders/transactions/htlc-lock";
import { Two } from "@packages/crypto/src/transactions/types";
import { Address } from "@packages/crypto/src/identities";

const { EpochTimestamp } = HtlcLockExpirationType;

Expand All @@ -25,30 +26,45 @@ describe("Htlc lock Transaction", () => {
expect(builder).toHaveProperty("data.asset", {});
});

const htlcLockAsset = {
secretHash: "0f128d401958b1b30ad0d10406f47f9489321017b4614e6cb993fc63913c5454",
expiration: {
type: EpochTimestamp,
value: Math.floor(Date.now() / 1000),
},
};

describe("htlcLockAsset", () => {
it("should set the htlc lock asset", () => {
const htlcLockAsset = {
secretHash: "0f128d401958b1b30ad0d10406f47f9489321017b4614e6cb993fc63913c5454",
expiration: {
type: EpochTimestamp,
value: Math.floor(Date.now() / 1000),
},
};

builder.htlcLockAsset(htlcLockAsset);

expect(builder.data.asset.lock).toEqual(htlcLockAsset);
});
});

describe("recipientId", () => {
it("should not throw when recipientId network is valid", () => {
builder
.htlcLockAsset(htlcLockAsset)
.amount("100")
.recipientId(Address.fromPassphrase("secret", 23))
.sign("dummy passphrase");

expect(() => builder.build()).not.toThrow();
});

it("should throw when recipientId network is invalid", () => {
builder
.htlcLockAsset(htlcLockAsset)
.amount("100")
.recipientId(Address.fromPassphrase("secret", 30))
.sign("dummy passphrase");

expect(() => builder.build()).toThrow();
});
});

describe("verify", () => {
const htlcLockAsset = {
secretHash: "0f128d401958b1b30ad0d10406f47f9489321017b4614e6cb993fc63913c5454",
expiration: {
type: EpochTimestamp,
value: Math.floor(Date.now() / 1000),
},
};
const address = "AVzsSFwicz5gYLqCzZNL8N1RztkWQSMovK";

it("should be valid with a signature", () => {
Expand Down
8 changes: 7 additions & 1 deletion packages/crypto/src/transactions/types/two/htlc-lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ export abstract class HtlcLockTransaction extends Transaction {
}

if (data.recipientId) {
buffer.append(Address.toBuffer(data.recipientId).addressBuffer);
const { addressBuffer, addressError } = Address.toBuffer(data.recipientId);

if (options) {
options.addressError = addressError;
}

buffer.append(addressBuffer);
}

return buffer;
Expand Down

0 comments on commit 210b419

Please sign in to comment.