UNPKG

viem

Version:

TypeScript Interface for Ethereum

84 lines 3.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InvalidStoreValueError = void 0; exports.read = read; exports.write = write; const Address = require("ox/Address"); const Hash = require("ox/Hash"); const Json = require("ox/Json"); const tempo_1 = require("ox/tempo"); const base_js_1 = require("../../errors/base.js"); const Addresses_js_1 = require("../Addresses.js"); const maxStoredValueLength = 65_536; const zeroCommitment = `0x${'00'.repeat(32)}`; async function read(store, options) { const { address, commitment } = options; const value = await store.getItem(key({ address, commitment })); if (value === null || value === undefined) return null; const config = deserialize(value); assertKey({ address, commitment, config }); return config; } async function write(store, options) { const { address, commitment } = options; const config = tempo_1.MultisigConfig.from(options.config); assertKey({ address, commitment, config }); const value = serialize(config); await store.setItem(key({ address, commitment }), value); if (commitment.toLowerCase() === zeroCommitment) await store.setItem(key({ address, commitment: tempo_1.MultisigConfig.getCommitment(config) }), value); } function assertKey(options) { const { address, commitment, config } = options; if (!Address.validate(address) || !Hash.validate(commitment)) throw new InvalidStoreValueError(); if (config.version === 0n) { if (!Address.isEqual(tempo_1.MultisigConfig.getAddress(config, { factory: Addresses_js_1.nativeMultisigFactory }), address)) throw new InvalidStoreValueError(); if (commitment.toLowerCase() === zeroCommitment) return; } if (tempo_1.MultisigConfig.getCommitment(config).toLowerCase() !== commitment.toLowerCase()) throw new InvalidStoreValueError(); } function deserialize(value) { try { if (value.length > maxStoredValueLength) throw new InvalidStoreValueError(); return tempo_1.MultisigConfig.fromRpc(Json.parse(value)); } catch (cause) { if (cause instanceof InvalidStoreValueError) throw cause; throw new InvalidStoreValueError({ cause }); } } function key(options) { const { address, commitment } = options; return `multisig:config:${address.toLowerCase()}:${commitment.toLowerCase()}`; } function serialize(config) { try { const value = Json.stringify(tempo_1.MultisigConfig.toRpc(config)); if (value.length > maxStoredValueLength) throw new InvalidStoreValueError(); return value; } catch (cause) { if (cause instanceof InvalidStoreValueError) throw cause; throw new InvalidStoreValueError({ cause }); } } class InvalidStoreValueError extends base_js_1.BaseError { constructor(options = {}) { super('Stored multisig config is malformed or mismatched.', { cause: options.cause, name: 'Multisig.Config.InvalidStoreValueError', }); } } exports.InvalidStoreValueError = InvalidStoreValueError; //# sourceMappingURL=Config.js.map