@turnkey/eip-1193-provider
Version:
EIP-1193 Provider for Turnkey.
108 lines (105 loc) • 3.86 kB
JavaScript
import { isHttpClient } from '@turnkey/http';
import { signatureToHex } from 'viem';
import { pad } from 'viem/utils';
import { TURNKEY_ERROR_CODE } from './constants.mjs';
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 (isHttpClient(client)) {
activityResponse = await client.signRawPayload({
type: "ACTIVITY_TYPE_SIGN_RAW_PAYLOAD_V2",
organizationId,
parameters: {
signWith,
payload: 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, {
errorMessage: "Error signing message",
});
if (!signature) {
// @todo update error message
throw "Error signing message";
}
return signatureToHex({
r: `0x${signature.r}`,
s: `0x${signature.s}`,
v: BigInt(signature.v) + 27n,
});
}
async function signTransaction({ client, unsignedTransaction, organizationId, signWith, }) {
let activityResponse;
if (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, {
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 } = TURNKEY_ERROR_CODE;
return [WALLET_NOT_FOUND, ORG_NOT_FOUND].includes(error.code);
};
export { signMessage, signTransaction, turnkeyIsDisconnected, unwrapActivityResult };
//# sourceMappingURL=turnkey.mjs.map