-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1913 from trilitech/improve-beacon-payload-decoder
Improve Beacon payload parsing
- Loading branch information
Showing
10 changed files
with
378 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,98 @@ | ||
import { SigningType } from "@airgap/beacon-wallet"; | ||
|
||
import { decodeBeaconPayload } from "./decodeBeaconPayload"; | ||
|
||
describe("decodeBeaconPayload", () => { | ||
it("returns an error message if the payload is not a valid utf8", () => { | ||
const payload = | ||
"05010000004354657a6f73205369676e6564204d6573736167653a20496e76616c696420555446383a20c380c2afc3bfc3bec3bd3b204e6f6e7072696e7461626c653a20000115073b"; | ||
|
||
expect(decodeBeaconPayload(payload, SigningType.MICHELINE)).toEqual({ | ||
result: payload, | ||
error: "Cannot parse Beacon payload", | ||
}); | ||
}); | ||
|
||
it("decodes a valid payload with '0501' prefix and padding", () => { | ||
const payload = | ||
"05010000004254657a6f73205369676e6564204d6573736167653a206d79646170702e636f6d20323032312d30312d31345431353a31363a30345a2048656c6c6f20776f726c6421"; | ||
const expected = "Tezos Signed Message: mydapp.com 2021-01-14T15:16:04Z Hello world!"; | ||
expect(decodeBeaconPayload(payload)).toEqual(expected); | ||
|
||
expect(decodeBeaconPayload(payload, SigningType.MICHELINE)).toEqual({ result: expected }); | ||
}); | ||
|
||
it("decodes a payload without '0501' prefix", () => { | ||
it.each([SigningType.MICHELINE, SigningType.OPERATION])( | ||
"throws without a valid prefix for %s", | ||
signingType => { | ||
const payload = | ||
"010000004254657a6f73205369676e6564204d6573736167653a206d79646170702e636f6d20323032312d30312d31345431353a31363a30345a2048656c6c6f20776f726c6421"; | ||
|
||
expect(decodeBeaconPayload(payload, signingType)).toEqual({ | ||
error: "Cannot parse Beacon payload", | ||
result: | ||
"010000004254657a6f73205369676e6564204d6573736167653a206d79646170702e636f6d20323032312d30312d31345431353a31363a30345a2048656c6c6f20776f726c6421", | ||
}); | ||
} | ||
); | ||
|
||
it("decodes a raw payload", () => { | ||
const payload = | ||
"54657a6f73205369676e6564204d6573736167653a206d79646170702e636f6d20323032312d30312d31345431353a31363a30345a2048656c6c6f20776f726c6421"; | ||
const expected = "Tezos Signed Message: mydapp.com 2021-01-14T15:16:04Z Hello world!"; | ||
expect(decodeBeaconPayload(payload)).toEqual(expected); | ||
const expected = { | ||
result: "Tezos Signed Message: mydapp.com 2021-01-14T15:16:04Z Hello world!", | ||
}; | ||
expect(decodeBeaconPayload(payload, SigningType.RAW)).toEqual(expected); | ||
}); | ||
|
||
it("parses a Michelson expression", () => { | ||
const raw = | ||
"95564d877fe9b5d1f90ad501799e63d25ed0f676381b7b4678f404c9e1d8bd9a6c00e7cf51bc4b6068aae1f385c22c85ec281eba46ecac02ddfd940580bd3fe0d403c096b1020000e7cf51bc4b6068aae1f385c22c85ec281eba46ec00"; | ||
const result = JSON.stringify({ | ||
branch: "BLr3xAWdfd7BEnhUXU7vzcTBMy52upMWLiLDvwd8VdHyzKnmho4", | ||
contents: [ | ||
{ | ||
kind: "transaction", | ||
source: "tz1gmj9EBXqQqQBmEVaCHBfunPJ67N82YJcz", | ||
fee: "300", | ||
counter: "10829533", | ||
gas_limit: "1040000", | ||
storage_limit: "60000", | ||
amount: "5000000", | ||
destination: "tz1gmj9EBXqQqQBmEVaCHBfunPJ67N82YJcz", | ||
}, | ||
], | ||
}); | ||
|
||
expect(decodeBeaconPayload(raw, SigningType.RAW)).toEqual({ result }); | ||
}); | ||
|
||
it("returns original payload if length is invalid", () => { | ||
const payload = "05010000005254657a6f73205369676e6564204d6573736167653a20696e76616c69642e"; | ||
expect(decodeBeaconPayload(payload)).toEqual(payload); | ||
const payload = "050100000053254657a6f73205369676e6564204d6573736167653a20696e76616c69642e"; | ||
expect(decodeBeaconPayload(payload, SigningType.RAW)).toEqual({ | ||
result: payload, | ||
error: "Cannot parse Beacon payload", | ||
}); | ||
}); | ||
|
||
it("returns original payload if an error occurs during decoding", () => { | ||
it("can parse a message without a body", () => { | ||
const invalidPayload = "0501000000"; | ||
expect(decodeBeaconPayload(invalidPayload)).toEqual(invalidPayload); | ||
expect(decodeBeaconPayload(invalidPayload, SigningType.RAW)).toEqual({ | ||
result: '{"branch":"JJhMAmr4o7mqnC","contents":[]}', | ||
}); | ||
}); | ||
|
||
it("handles an empty payload", () => { | ||
const emptyPayload = ""; | ||
expect(decodeBeaconPayload(emptyPayload)).toEqual(emptyPayload); | ||
expect(decodeBeaconPayload(emptyPayload, SigningType.RAW)).toEqual({ | ||
result: "", | ||
}); | ||
}); | ||
|
||
it("handles a payload with non-hex characters", () => { | ||
const nonHexPayload = "0501ZZZZ"; | ||
expect(decodeBeaconPayload(nonHexPayload)).toEqual(nonHexPayload); | ||
expect(decodeBeaconPayload(nonHexPayload, SigningType.RAW)).toEqual({ | ||
error: "Cannot parse Beacon payload", | ||
result: "0501ZZZZ", | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
405f663
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.