From 1ea5ba09127e280c8e4fea77cc17262a71db885a Mon Sep 17 00:00:00 2001 From: Crystal Lemire Date: Thu, 3 Oct 2024 17:28:22 -0700 Subject: [PATCH] Fix decode of empty rule data v2 to work for constructing legacy spaces in harmony tests (#1220) --- packages/web3/src/entitlement.test.ts | 8 ++++++++ packages/web3/src/entitlement.ts | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/packages/web3/src/entitlement.test.ts b/packages/web3/src/entitlement.test.ts index a24311988..db22cd4cb 100644 --- a/packages/web3/src/entitlement.test.ts +++ b/packages/web3/src/entitlement.test.ts @@ -24,6 +24,7 @@ import { decodeERC1155Params, createOperationsTree, XchainConfig, + EncodedNoopRuleData, } from './entitlement' import { MOCK_ADDRESS, MOCK_ADDRESS_2, MOCK_ADDRESS_3 } from './Utils' import { zeroAddress } from 'viem' @@ -989,6 +990,13 @@ test('encode/decode rule data v2', async () => { expect(randomTree.opType === newTree.opType).toBeTruthy() }) +test('decode empty ruledata v2 to NoopRuleData v1', async () => { + const converted = convertRuleDataV2ToV1(decodeRuleDataV2(EncodedNoopRuleData)) + expect(converted.operations).toHaveLength(0) + expect(converted.checkOperations).toHaveLength(0) + expect(converted.logicalOperations).toHaveLength(0) +}) + // encode/decode should respect address equality semantics but may not maintain case function addressesEqual(a: string, b: string): boolean { return a.toLowerCase() === b.toLowerCase() diff --git a/packages/web3/src/entitlement.ts b/packages/web3/src/entitlement.ts index fde3bb49c..f757da5d4 100644 --- a/packages/web3/src/entitlement.ts +++ b/packages/web3/src/entitlement.ts @@ -308,6 +308,14 @@ export function encodeRuleDataV2(ruleData: IRuleEntitlementV2Base.RuleDataV2Stru } export function decodeRuleDataV2(entitlementData: Hex): IRuleEntitlementV2Base.RuleDataV2Struct { + if (entitlementData === '0x') { + return { + operations: [], + checkOperations: [], + logicalOperations: [], + } as IRuleEntitlementV2Base.RuleDataV2Struct + } + const getRuleDataV2Abi: ExtractAbiFunction = getAbiItem({ abi: IRuleEntitlementV2Abi,