Skip to content

Commit

Permalink
allow access list to be used
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronmgdr authored Jun 6, 2024
2 parents f2be773 + 5360984 commit 91b6357
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 12 deletions.
39 changes: 27 additions & 12 deletions src/lib/transactions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
AccessList,
accessListify,
AccessListish,
assertArgument,
Expand Down Expand Up @@ -106,6 +107,7 @@ export const celoTransactionFields: Record<CeloFieldName, Field> = {
maxFeePerGas: { maxLength: 32, numeric: true } as Field,
maxPriorityFeePerGas: { maxLength: 32, numeric: true } as Field,
maxFeeInFeeCurrency: { maxLength: 32, numeric: true } as Field,

} as const;

function formatCeloField(name: CeloFieldName, value: any) {
Expand Down Expand Up @@ -170,8 +172,7 @@ function prepareEncodeTx(tx: CeloTransaction, signature?: Signature): RlpStructu
tx.to || "0x",
tx.value ? toBeHex(tx.value) : "0x",
tx.data || "0x",
// @ts-expect-error
tx.accessList || [],
formatAccessList(tx.accessList || []),
(tx as CeloTransactionCip66).feeCurrency || "0x",
(tx as CeloTransactionCip66).maxFeeInFeeCurrency ? toBeHex((tx as CeloTransactionCip66).maxFeeInFeeCurrency) : "0x",
];
Expand All @@ -188,8 +189,7 @@ function prepareEncodeTx(tx: CeloTransaction, signature?: Signature): RlpStructu
tx.to || "0x",
tx.value ? toBeHex(tx.value) : "0x",
tx.data || "0x",
// @ts-expect-error
tx.accessList || [],
formatAccessList(tx.accessList || []),
(tx as CeloTransactionCip64).feeCurrency || "0x",
];
break;
Expand All @@ -205,8 +205,7 @@ function prepareEncodeTx(tx: CeloTransaction, signature?: Signature): RlpStructu
tx.to || "0x",
tx.value ? toBeHex(tx.value) : "0x",
tx.data || "0x",
// @ts-expect-error
tx.accessList || [],
formatAccessList(tx.accessList || []),
];
break;
default:
Expand Down Expand Up @@ -258,6 +257,8 @@ export function serializeCeloTransaction(
if (fieldName in celoTransactionFields) {
// @ts-expect-error
txArgs[fieldName as CeloFieldName] = formatCeloField(fieldName as CeloFieldName, fieldValue);
} else if (fieldName === 'accessList') {
txArgs[fieldName] = fieldValue
}
});

Expand Down Expand Up @@ -339,7 +340,7 @@ export function parseCeloTransaction(rawTransaction: BytesLike): CeloTransaction
to: handleAddress(transaction[5] as string),
value: handleBigInt(transaction[6] as string),
data: transaction[7] as string,
accessList: handleAccessList(transaction[8] as string),
accessList: handleAccessList(transaction[8] as Array<[ string, Array<string> ]>),
feeCurrency: handleAddress(transaction[9] as string),
maxFeeInFeeCurrency: handleBigInt(transaction[10] as string),
} as CeloTransactionCip66;
Expand All @@ -355,7 +356,7 @@ export function parseCeloTransaction(rawTransaction: BytesLike): CeloTransaction
to: handleAddress(transaction[5] as string),
value: handleBigInt(transaction[6] as string),
data: transaction[7],
accessList: handleAccessList(transaction[8] as string),
accessList: handleAccessList(transaction[8] as Array<[ string, Array<string> ]>),
feeCurrency: handleAddress(transaction[9] as string),
} as CeloTransactionCip64;
break;
Expand All @@ -371,7 +372,7 @@ export function parseCeloTransaction(rawTransaction: BytesLike): CeloTransaction
to: handleAddress(transaction[5] as string),
value: handleBigInt(transaction[6] as string),
data: transaction[7] as string,
accessList: handleAccessList(transaction[8] as string),
accessList: handleAccessList(transaction[8] as Array<[ string, Array<string> ]>),
} as CeloTransactionEip1559;
break;
default:
Expand Down Expand Up @@ -469,12 +470,26 @@ function handleBigInt(value: string): bigint {
return getBigInt(value);
}

function handleAccessList(value: string): AccessListish | "0x" {
function formatAccessList(value: AccessListish): Array<[ string, Array<string> ]> {
return accessListify(value).map((set) => [ set.address, set.storageKeys ]);
}

/*
@param value [
[
'0x0000000000000000000000000000000000000000',
[
'0x0000000000000000000000000000000000000000000000000000000000000001',
'0x60fdd29ff912ce880cd3edaf9f932dc61d3dae823ea77e0323f94adb9f6a72fe'
]
]
]
*/
function handleAccessList(value: Array<[ string, Array<string> ]> | '0x'): AccessList {
if (value === "0x") {
return accessListify([]);
}
// TODO: use value
return accessListify([]);
return accessListify(value);
}

const baseTxLengths = {
Expand Down
28 changes: 28 additions & 0 deletions tests/transactions.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,34 @@ describe('serializeCeloTransaction', () => {

expect(serializeCeloTransaction(tx)).toMatchInlineSnapshot(`"0x7af84b82a4ec01847735940084773594008094f653a42ef024d174bb4be98b67690e5886d01f5f880de0b6b3a764000080c094765de816845861e75a25fca122bb6898b8b1282a860b3a4b56fa00"`);
});
it('can handle accessList', () => {
const transaction = serializeCeloTransaction({
to: "0xF653A42ef024d174Bb4bE98B67690E5886d01F5F",
chainId: 42220,
nonce: 1,
accessList: [
{
address: '0x0000000000000000000000000000000000000000',
storageKeys: [
'0x0000000000000000000000000000000000000000000000000000000000000001',
'0x60fdd29ff912ce880cd3edaf9f932dc61d3dae823ea77e0323f94adb9f6a72fe',
],
},
],
})
expect(transaction).toMatchInlineSnapshot(`"0x02f87b82a4ec0180808094f653a42ef024d174bb4be98b67690e5886d01f5f8080f85bf859940000000000000000000000000000000000000000f842a00000000000000000000000000000000000000000000000000000000000000001a060fdd29ff912ce880cd3edaf9f932dc61d3dae823ea77e0323f94adb9f6a72fe"`)
expect(parseCeloTransaction(transaction).accessList).toMatchInlineSnapshot(`
[
{
"address": "0x0000000000000000000000000000000000000000",
"storageKeys": [
"0x0000000000000000000000000000000000000000000000000000000000000001",
"0x60fdd29ff912ce880cd3edaf9f932dc61d3dae823ea77e0323f94adb9f6a72fe",
],
},
]
`)
})
});
describe('parseCeloTransaction', () => {
it('serializes CIP-66 transaction', () => {
Expand Down

0 comments on commit 91b6357

Please sign in to comment.