diff --git a/xc-admin/packages/xc-admin-common/src/__tests__/GovernancePayload.test.ts b/xc-admin/packages/xc-admin-common/src/__tests__/GovernancePayload.test.ts index 8dd8b908..c349241b 100644 --- a/xc-admin/packages/xc-admin-common/src/__tests__/GovernancePayload.test.ts +++ b/xc-admin/packages/xc-admin-common/src/__tests__/GovernancePayload.test.ts @@ -60,22 +60,19 @@ test("GovernancePayload ser/de", (done) => { expect(governanceHeader?.action).toBe("SetFee"); // Wrong magic number - governanceHeader = decodeHeader( - Buffer.from([0, 0, 0, 0, 0, 0, 0, 26, 0, 0, 0, 0]) - ); - expect(governanceHeader).toBeUndefined(); + expect(() => + decodeHeader(Buffer.from([0, 0, 0, 0, 0, 0, 0, 26, 0, 0, 0, 0])) + ).toThrow("Wrong magic number"); // Wrong chain - governanceHeader = decodeHeader( - Buffer.from([80, 84, 71, 77, 0, 0, 255, 255, 0, 0, 0, 0]) - ); - expect(governanceHeader).toBeUndefined(); + expect(() => + decodeHeader(Buffer.from([80, 84, 71, 77, 0, 0, 255, 255, 0, 0, 0, 0])) + ).toThrow("Chain Id not found"); // Wrong module/action combination - governanceHeader = decodeHeader( - Buffer.from([80, 84, 71, 77, 0, 1, 0, 26, 0, 0, 0, 0]) - ); - expect(governanceHeader).toBeUndefined(); + expect(() => + decodeHeader(Buffer.from([80, 84, 71, 77, 0, 1, 0, 26, 0, 0, 0, 0])) + ).toThrow("Invalid header, action doesn't match module"); // Decode executePostVaa with empty instructions let expectedExecuteVaaArgs = { diff --git a/xc-admin/packages/xc-admin-common/src/governance_payload/ExecutePostedVaa.ts b/xc-admin/packages/xc-admin-common/src/governance_payload/ExecutePostedVaa.ts index e89c0287..ac166a97 100644 --- a/xc-admin/packages/xc-admin-common/src/governance_payload/ExecutePostedVaa.ts +++ b/xc-admin/packages/xc-admin-common/src/governance_payload/ExecutePostedVaa.ts @@ -83,17 +83,11 @@ export type ExecutePostedVaaArgs = { }; /** Decode ExecutePostedVaaArgs and return undefined if it failed */ -export function decodeExecutePostedVaa( - data: Buffer -): ExecutePostedVaaArgs | undefined { +export function decodeExecutePostedVaa(data: Buffer): ExecutePostedVaaArgs { let deserialized = executePostedVaaLayout.decode(data); let header = verifyHeader(deserialized.header); - if (!header) { - return undefined; - } - let instructions: TransactionInstruction[] = deserialized.instructions.map( (ix) => { let programId: PublicKey = new PublicKey(ix.programId); diff --git a/xc-admin/packages/xc-admin-common/src/governance_payload/index.ts b/xc-admin/packages/xc-admin-common/src/governance_payload/index.ts index 0f589968..70431078 100644 --- a/xc-admin/packages/xc-admin-common/src/governance_payload/index.ts +++ b/xc-admin/packages/xc-admin-common/src/governance_payload/index.ts @@ -75,7 +75,7 @@ export function governanceHeaderLayout(): BufferLayout.Structure< } /** Decode Pyth Governance Header and return undefined if the header is invalid */ -export function decodeHeader(data: Buffer): PythGovernanceHeader | undefined { +export function decodeHeader(data: Buffer): PythGovernanceHeader { let deserialized = governanceHeaderLayout().decode(data); return verifyHeader(deserialized); } @@ -111,27 +111,23 @@ export function verifyHeader( action: number; chain: ChainId; }> -) { +): PythGovernanceHeader { if (deserialized.magicNumber !== MAGIC_NUMBER) { - return undefined; + throw new Error("Wrong magic number"); } if (!toChainName(deserialized.chain)) { - return undefined; + throw new Error("Chain Id not found"); } - try { - let governanceHeader: PythGovernanceHeader = { - targetChainId: toChainName(deserialized.chain), - action: toActionName({ - actionId: deserialized.action, - moduleId: deserialized.module, - }), - }; - return governanceHeader; - } catch { - return undefined; - } + let governanceHeader: PythGovernanceHeader = { + targetChainId: toChainName(deserialized.chain), + action: toActionName({ + actionId: deserialized.action, + moduleId: deserialized.module, + }), + }; + return governanceHeader; } export { decodeExecutePostedVaa } from "./ExecutePostedVaa";