ethers.js/lib.esm/transaction/accesslist.js

37 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2022-09-05 16:57:11 -04:00
import { getAddress } from "../address/index.js";
2022-11-30 15:44:23 -05:00
import { assertArgument, isHexString } from "../utils/index.js";
2022-09-05 16:57:11 -04:00
function accessSetify(addr, storageKeys) {
return {
address: getAddress(addr),
2022-11-30 15:44:23 -05:00
storageKeys: storageKeys.map((storageKey, index) => {
assertArgument(isHexString(storageKey, 32), "invalid slot", `storageKeys[${index}]`, storageKey);
2022-09-05 16:57:11 -04:00
return storageKey.toLowerCase();
})
};
}
2022-12-02 21:27:06 -05:00
/**
* Returns a [[AccessList]] from any ethers-supported access-list structure.
*/
2022-09-05 16:57:11 -04:00
export function accessListify(value) {
if (Array.isArray(value)) {
return value.map((set, index) => {
if (Array.isArray(set)) {
2022-11-30 15:44:23 -05:00
assertArgument(set.length === 2, "invalid slot set", `value[${index}]`, set);
2022-09-05 16:57:11 -04:00
return accessSetify(set[0], set[1]);
}
2022-11-30 15:44:23 -05:00
assertArgument(set != null && typeof (set) === "object", "invalid address-slot set", "value", value);
2022-09-05 16:57:11 -04:00
return accessSetify(set.address, set.storageKeys);
});
}
2022-11-30 15:44:23 -05:00
assertArgument(value != null && typeof (value) === "object", "invalid access list", "value", value);
2022-09-05 16:57:11 -04:00
const result = Object.keys(value).map((addr) => {
const storageKeys = value[addr].reduce((accum, storageKey) => {
accum[storageKey] = true;
return accum;
}, {});
return accessSetify(addr, Object.keys(storageKeys).sort());
});
result.sort((a, b) => (a.address.localeCompare(b.address)));
return result;
}
//# sourceMappingURL=accesslist.js.map