clients/js: Dump payload in case of parse failure

This commit is contained in:
Csongor Kiss 2022-07-01 00:02:16 +01:00 committed by Csongor Kiss
parent 3b6292af29
commit e8620ccea9
4 changed files with 103 additions and 66 deletions

View File

@ -452,9 +452,7 @@ yargs(hideBin(process.argv))
const buf = Buffer.from(vaa_hex, "hex");
const parsed_vaa = vaa.parse(buf);
if (!vaa.hasPayload(parsed_vaa)) {
throw Error("Couldn't parse VAA payload");
}
vaa.assertKnownPayload(parsed_vaa);
console.log(parsed_vaa.payload);

View File

@ -0,0 +1,22 @@
{
version: 1,
guardianSetIndex: 0,
signatures: [
{
guardianSetIndex: 0,
signature: 'e29d3ad180b153d68c3f445d75eaa62fcc99690945baaf4ad0463e9ce44f27f75da3d49f79722920aac81ba2be80f688895f174942fedc403bc4e5ce3555b77b00'
}
],
timestamp: 1656354705,
nonce: 0,
emitterChain: 2,
emitterAddress: '0x000000000000000000000000f19a2a01b70519f67adb309a994ec8c69a967e8b',
sequence: 0n,
consistencyLevel: 1,
payload: {
type: 'Other',
hex: '46726f6d3a2065766d305c6e4d73673a2048656c6c6f20576f726c6421',
ascii: 'From: evm0\\nMsg: Hello World!'
},
digest: '0x8b7781f662ff1eed4827b770c0e735288948f0b56611f8cd73bf65e6b2a7a8ad'
}

View File

@ -0,0 +1 @@
AQAAAAABAOKdOtGAsVPWjD9EXXXqpi/MmWkJRbqvStBGPpzkTyf3XaPUn3lyKSCqyBuivoD2iIlfF0lC/txAO8TlzjVVt3sAYrn3kQAAAAAAAgAAAAAAAAAAAAAAAPGaKgG3BRn2etswmplOyMaaln6LAAAAAAAAAAABRnJvbTogZXZtMFxuTXNnOiBIZWxsbyBXb3JsZCE=

View File

@ -52,6 +52,12 @@ class P<T> {
}
}
export interface Other {
type: "Other",
hex: string,
ascii?: string
}
// All the different types of payloads
export type Payload =
GuardianSetUpgrade
@ -70,7 +76,7 @@ export type ContractUpgrade =
| PortalContractUpgrade<"TokenBridge">
| PortalContractUpgrade<"NFTBridge">
export function parse(buffer: Buffer): VAA<Payload | null> {
export function parse(buffer: Buffer): VAA<Payload | Other> {
const vaa = parseEnvelope(buffer)
const parser = guardianSetUpgradeParser
.or(coreContractUpgradeParser)
@ -82,15 +88,21 @@ export function parse(buffer: Buffer): VAA<Payload | null> {
.or(tokenBridgeTransferWithPayloadParser())
.or(tokenBridgeAttestMetaParser())
.or(nftBridgeTransferParser())
const payload = parser.parse(vaa.payload)
delete payload['tokenURILength']
let payload : Payload | Other | null = parser.parse(vaa.payload)
if (payload === null) {
payload = {type: "Other", hex: Buffer.from(vaa.payload).toString("hex"), ascii: Buffer.from(vaa.payload).toString('utf8')}
} else {
delete payload['tokenURILength']
}
var myVAA = { ...vaa, payload }
return myVAA
}
export function hasPayload(vaa: VAA<Payload | null>): vaa is VAA<Payload> {
return vaa.payload !== null
export function assertKnownPayload(vaa: VAA<Payload | Other>): asserts vaa is VAA<Payload> {
if (vaa.payload.type === "Other") {
throw Error(`Couldn't parse VAA payload: ${vaa.payload.hex}`);
}
}
// Parse the VAA envelope without looking into the payload.
@ -161,68 +173,72 @@ export function serialiseVAA(vaa: VAA<Payload>) {
return body.join("")
}
export function vaaDigest(vaa: VAA<Payload>) {
export function vaaDigest(vaa: VAA<Payload | Other>) {
return solidityKeccak256(["bytes"], [solidityKeccak256(["bytes"], ["0x" + vaaBody(vaa)])])
}
function vaaBody(vaa: VAA<Payload>) {
let payload = vaa.payload
function vaaBody(vaa: VAA<Payload | Other>) {
let payload_str: string
switch (payload.module) {
case "Core":
switch (payload.type) {
case "GuardianSetUpgrade":
payload_str = serialiseGuardianSetUpgrade(payload)
break
case "ContractUpgrade":
payload_str = serialiseCoreContractUpgrade(payload)
break
default:
impossible(payload)
break
}
break
case "NFTBridge":
switch (payload.type) {
case "ContractUpgrade":
payload_str = serialisePortalContractUpgrade(payload)
break
case "RegisterChain":
payload_str = serialisePortalRegisterChain(payload)
break
case "Transfer":
payload_str = serialiseNFTBridgeTransfer(payload)
break
default:
impossible(payload)
break
}
break
case "TokenBridge":
switch (payload.type) {
case "ContractUpgrade":
payload_str = serialisePortalContractUpgrade(payload)
break
case "RegisterChain":
payload_str = serialisePortalRegisterChain(payload)
break
case "Transfer":
payload_str = serialiseTokenBridgeTransfer(payload)
break
case "TransferWithPayload":
payload_str = serialiseTokenBridgeTransferWithPayload(payload)
break
case "AttestMeta":
payload_str = serialiseTokenBridgeAttestMeta(payload)
break
default:
impossible(payload)
break
}
break
default:
impossible(payload)
break
if (vaa.payload.type === "Other") {
payload_str = vaa.payload.hex
} else {
let payload = vaa.payload;
switch (payload.module) {
case "Core":
switch (payload.type) {
case "GuardianSetUpgrade":
payload_str = serialiseGuardianSetUpgrade(payload)
break
case "ContractUpgrade":
payload_str = serialiseCoreContractUpgrade(payload)
break
default:
impossible(payload)
break
}
break
case "NFTBridge":
switch (payload.type) {
case "ContractUpgrade":
payload_str = serialisePortalContractUpgrade(payload)
break
case "RegisterChain":
payload_str = serialisePortalRegisterChain(payload)
break
case "Transfer":
payload_str = serialiseNFTBridgeTransfer(payload)
break
default:
impossible(payload)
break
}
break
case "TokenBridge":
switch (payload.type) {
case "ContractUpgrade":
payload_str = serialisePortalContractUpgrade(payload)
break
case "RegisterChain":
payload_str = serialisePortalRegisterChain(payload)
break
case "Transfer":
payload_str = serialiseTokenBridgeTransfer(payload)
break
case "TransferWithPayload":
payload_str = serialiseTokenBridgeTransferWithPayload(payload)
break
case "AttestMeta":
payload_str = serialiseTokenBridgeAttestMeta(payload)
break
default:
impossible(payload)
break
}
break
default:
impossible(payload)
break
}
}
const body = [
encode("uint32", vaa.timestamp),