UNPKG

@oasisprotocol/sapphire-viem-v2

Version:
81 lines 3.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SAPPHIRE_WRAPPED_VIEM_SERIALIZER = exports.SAPPHIRE_WRAPPED_VIEM_TRANSPORT = exports.sapphireLocalnet = void 0; exports.sapphireHttpTransport = sapphireHttpTransport; exports.createSapphireSerializer = createSapphireSerializer; exports.wrapWalletClient = wrapWalletClient; const sapphire_paratime_1 = require("@oasisprotocol/sapphire-paratime"); const viem_1 = require("viem"); exports.sapphireLocalnet = (0, viem_1.defineChain)({ id: sapphire_paratime_1.NETWORKS.localnet.chainId, name: "Oasis Sapphire Localnet", network: "sapphire-localnet", nativeCurrency: { name: "Sapphire Local Rose", symbol: "TEST", decimals: 18 }, rpcUrls: { default: { http: [sapphire_paratime_1.NETWORKS.localnet.defaultGateway], }, }, testnet: true, }); exports.SAPPHIRE_WRAPPED_VIEM_TRANSPORT = Symbol("#SAPPHIRE_WRAPPED_VIEM_TRANSPORT"); function sapphireHttpTransport(sapphireConfig, overrideUrl, httpConfig) { const cachedProviders = {}; return ((params) => { const defaultUrl = params.chain?.rpcUrls.default.http[0]; const url = overrideUrl || defaultUrl; if (!url) { throw new Error("sapphireHttpTransport() needs a chain.rpcUrls.default.http[0] to be set or explicit url"); } if (!(url in cachedProviders)) { const x = (0, sapphire_paratime_1.wrapEthereumProvider)((0, viem_1.http)(url, httpConfig)(params), sapphireConfig); Reflect.set(x, exports.SAPPHIRE_WRAPPED_VIEM_TRANSPORT, true); cachedProviders[url] = x; } return cachedProviders[url]; }); } async function createSapphireSerializer(client, originalSerializer) { if (originalSerializer && Reflect.has(originalSerializer, exports.SAPPHIRE_WRAPPED_VIEM_SERIALIZER)) { return originalSerializer; } const fetcher = new sapphire_paratime_1.KeyFetcher(); const provider = client; await fetcher.fetch(provider); const intervalId = setInterval(async () => { await fetcher.fetch(provider); }, fetcher.timeoutMilliseconds); if (typeof intervalId.unref === "function") { intervalId.unref(); } const wrappedSerializer = ((tx, sig) => { if (!sig) { const cipher = fetcher.cipherSync(); const encryptedData = cipher.encryptCall(tx.data); tx.data = encryptedData; } if (originalSerializer) { return originalSerializer(tx, sig); } return (0, viem_1.serializeTransaction)(tx, sig); }); Reflect.set(wrappedSerializer, exports.SAPPHIRE_WRAPPED_VIEM_SERIALIZER, true); return wrappedSerializer; } exports.SAPPHIRE_WRAPPED_VIEM_SERIALIZER = Symbol("#SAPPHIRE_WRAPPED_VIEM_SERIALIZER"); async function wrapWalletClient(client) { if (!client.chain) { throw new Error("No chain defined in client"); } const originalSerializer = client.chain?.serializers?.transaction; if (!originalSerializer || !Reflect.get(originalSerializer, exports.SAPPHIRE_WRAPPED_VIEM_SERIALIZER)) { if (!client.chain.serializers) { client.chain.serializers = {}; } client.chain.serializers.transaction = await createSapphireSerializer(client, originalSerializer); } return client; } //# sourceMappingURL=index.js.map