UNPKG

@turnkey/eip-1193-provider

Version:

EIP-1193 Provider for Turnkey.

113 lines (109 loc) 4.17 kB
'use strict'; var http = require('@turnkey/http'); var viem = require('viem'); var utils = require('viem/utils'); var constants = require('./constants.js'); function unwrapActivityResult(activityResponse, { errorMessage }) { const { activity } = activityResponse; switch (activity.status) { case "ACTIVITY_STATUS_CONSENSUS_NEEDED": { throw "Consensus needed"; } case "ACTIVITY_STATUS_COMPLETED": { const result = activity.result; if (result === undefined) { throw "Activity result is undefined"; } return result; } default: { throw errorMessage; } } } async function signMessage({ client, message, organizationId, signWith, }) { let activityResponse; if (http.isHttpClient(client)) { activityResponse = await client.signRawPayload({ type: "ACTIVITY_TYPE_SIGN_RAW_PAYLOAD_V2", organizationId, parameters: { signWith, payload: utils.pad(message), encoding: "PAYLOAD_ENCODING_HEXADECIMAL", hashFunction: "HASH_FUNCTION_NO_OP", }, timestampMs: String(Date.now()), // millisecond timestamp }); } else { activityResponse = await client.signRawPayload({ signWith, payload: message, encoding: "PAYLOAD_ENCODING_HEXADECIMAL", hashFunction: "HASH_FUNCTION_NO_OP", }); } const { signRawPayloadResult: signature } = unwrapActivityResult(activityResponse /* Type casting is ok here. The invalid types are both actually strings. TS is too strict here! */, { errorMessage: "Error signing message", }); if (!signature) { // @todo update error message throw "Error signing message"; } return viem.signatureToHex({ r: `0x${signature.r}`, s: `0x${signature.s}`, v: BigInt(signature.v) + 27n, }); } async function signTransaction({ client, unsignedTransaction, organizationId, signWith, }) { let activityResponse; if (http.isHttpClient(client)) { activityResponse = await client.signTransaction({ type: "ACTIVITY_TYPE_SIGN_TRANSACTION_V2", organizationId: organizationId, parameters: { signWith, type: "TRANSACTION_TYPE_ETHEREUM", unsignedTransaction: unsignedTransaction, }, timestampMs: String(Date.now()), // millisecond timestamp }); } else { activityResponse = await client.signTransaction({ signWith, type: "TRANSACTION_TYPE_ETHEREUM", unsignedTransaction: unsignedTransaction, }); } const { signTransactionResult } = unwrapActivityResult(activityResponse /* Type casting is ok here. The invalid types are both actually strings. TS is too strict here! */, { errorMessage: "Error signing transaction", }); if (!signTransactionResult) { // @todo update error handling (e.g. consensus) throw "Error signing transaction"; } return signTransactionResult.signedTransaction; } /** * Checks if the error code corresponds to a disconnected state. * * Determines if provided error code is one of the known * error codes that signify a disconnected state, specifically if the wallet * or organization was not found. * * @param {Object} error - The error object containing the error code. * @param {number} error.code - The error code to check against known disconnected state codes. * @returns {boolean} - Returns true if the error code is for a disconnected state, otherwise false. */ const turnkeyIsDisconnected = (error) => { const { WALLET_NOT_FOUND, ORG_NOT_FOUND } = constants.TURNKEY_ERROR_CODE; return [WALLET_NOT_FOUND, ORG_NOT_FOUND].includes(error.code); }; exports.signMessage = signMessage; exports.signTransaction = signTransaction; exports.turnkeyIsDisconnected = turnkeyIsDisconnected; exports.unwrapActivityResult = unwrapActivityResult; //# sourceMappingURL=turnkey.js.map