@turnkey/viem
Advanced tools
Comparing version
@@ -10,8 +10,17 @@ "use strict"; | ||
async function createAccount(input) { | ||
const { client, organizationId, privateKeyId } = input; | ||
const { client, organizationId, signWith } = input; | ||
let { ethereumAddress } = input; | ||
// Fetch the address if we don't have it | ||
if (ethereumAddress === undefined) { | ||
if (!signWith) { | ||
throw new http_1.TurnkeyActivityError({ | ||
message: `Missing signWith parameter`, | ||
}); | ||
} | ||
if ((0, viem_2.isAddress)(signWith)) { | ||
// override provided `ethereumAddress` | ||
ethereumAddress = signWith; | ||
} | ||
else if (!ethereumAddress) { | ||
// we have a private key ID, but not an ethereumAddress | ||
const data = await client.getPrivateKey({ | ||
privateKeyId: privateKeyId, | ||
privateKeyId: signWith, | ||
organizationId: organizationId, | ||
@@ -22,3 +31,3 @@ }); | ||
throw new http_1.TurnkeyActivityError({ | ||
message: `Unable to find Ethereum address for key ${privateKeyId} under organization ${organizationId}`, | ||
message: `Unable to find Ethereum address for key ${signWith} under organization ${organizationId}`, | ||
}); | ||
@@ -30,3 +39,3 @@ } | ||
signMessage: function ({ message, }) { | ||
return signMessage(client, message, organizationId, privateKeyId); | ||
return signMessage(client, message, organizationId, signWith); | ||
}, | ||
@@ -37,6 +46,6 @@ signTransaction: function (transaction, args) { | ||
: args.serializer; | ||
return signTransaction(client, transaction, serializer, organizationId, privateKeyId); | ||
return signTransaction(client, transaction, serializer, organizationId, signWith); | ||
}, | ||
signTypedData: function (typedData) { | ||
return signTypedData(client, typedData, organizationId, privateKeyId); | ||
return signTypedData(client, typedData, organizationId, signWith); | ||
}, | ||
@@ -86,20 +95,20 @@ }); | ||
exports.createApiKeyAccount = createApiKeyAccount; | ||
async function signMessage(client, message, organizationId, privateKeyId) { | ||
async function signMessage(client, message, organizationId, signWith) { | ||
const hashedMessage = (0, viem_2.hashMessage)(message); | ||
const signedMessage = await signMessageWithErrorWrapping(client, hashedMessage, organizationId, privateKeyId); | ||
const signedMessage = await signMessageWithErrorWrapping(client, hashedMessage, organizationId, signWith); | ||
return `${signedMessage}`; | ||
} | ||
async function signTransaction(client, transaction, serializer, organizationId, privateKeyId) { | ||
async function signTransaction(client, transaction, serializer, organizationId, signWith) { | ||
const serializedTx = serializer(transaction); | ||
const nonHexPrefixedSerializedTx = serializedTx.replace(/^0x/, ""); | ||
return await signTransactionWithErrorWrapping(client, nonHexPrefixedSerializedTx, organizationId, privateKeyId); | ||
return await signTransactionWithErrorWrapping(client, nonHexPrefixedSerializedTx, organizationId, signWith); | ||
} | ||
async function signTypedData(client, data, organizationId, privateKeyId) { | ||
async function signTypedData(client, data, organizationId, signWith) { | ||
const hashToSign = (0, viem_1.hashTypedData)(data); | ||
return await signMessageWithErrorWrapping(client, hashToSign, organizationId, privateKeyId); | ||
return await signMessageWithErrorWrapping(client, hashToSign, organizationId, signWith); | ||
} | ||
async function signTransactionWithErrorWrapping(client, unsignedTransaction, organizationId, privateKeyId) { | ||
async function signTransactionWithErrorWrapping(client, unsignedTransaction, organizationId, signWith) { | ||
let signedTx; | ||
try { | ||
signedTx = await signTransactionImpl(client, unsignedTransaction, organizationId, privateKeyId); | ||
signedTx = await signTransactionImpl(client, unsignedTransaction, organizationId, signWith); | ||
} | ||
@@ -117,3 +126,3 @@ catch (error) { | ||
} | ||
async function signTransactionImpl(client, unsignedTransaction, organizationId, privateKeyId) { | ||
async function signTransactionImpl(client, unsignedTransaction, organizationId, signWith) { | ||
const { activity } = await client.signTransaction({ | ||
@@ -123,3 +132,3 @@ type: "ACTIVITY_TYPE_SIGN_TRANSACTION_V2", | ||
parameters: { | ||
signWith: privateKeyId, | ||
signWith, | ||
type: "TRANSACTION_TYPE_ETHEREUM", | ||
@@ -141,6 +150,6 @@ unsignedTransaction: unsignedTransaction, | ||
} | ||
async function signMessageWithErrorWrapping(client, message, organizationId, privateKeyId) { | ||
async function signMessageWithErrorWrapping(client, message, organizationId, signWith) { | ||
let signedMessage; | ||
try { | ||
signedMessage = await signMessageImpl(client, message, organizationId, privateKeyId); | ||
signedMessage = await signMessageImpl(client, message, organizationId, signWith); | ||
} | ||
@@ -158,3 +167,3 @@ catch (error) { | ||
} | ||
async function signMessageImpl(client, message, organizationId, privateKeyId) { | ||
async function signMessageImpl(client, message, organizationId, signWith) { | ||
const { activity } = await client.signRawPayload({ | ||
@@ -164,3 +173,3 @@ type: "ACTIVITY_TYPE_SIGN_RAW_PAYLOAD_V2", | ||
parameters: { | ||
signWith: privateKeyId, | ||
signWith, | ||
payload: message, | ||
@@ -167,0 +176,0 @@ encoding: "PAYLOAD_ENCODING_HEXADECIMAL", |
# @turnkey/viem | ||
## 0.3.0 | ||
### Minor Changes | ||
- cf8631a: Update interface to support `signWith` | ||
This change supports signing with wallet account addresses, private key addresses, or private key IDs. See below for an example: | ||
```js | ||
const httpClient = new TurnkeyClient( | ||
{ | ||
baseUrl: "https://api.turnkey.com", | ||
}, | ||
// This uses API key credentials. | ||
// If you're using passkeys, use `@turnkey/webauthn-stamper` to collect webauthn signatures: | ||
// new WebauthnStamper({...options...}) | ||
new ApiKeyStamper({ | ||
apiPublicKey: "...", | ||
apiPrivateKey: "...", | ||
}) | ||
); | ||
// Create the Viem custom account | ||
const turnkeyAccount = await createAccount({ | ||
client: httpClient, | ||
organizationId: "...", | ||
signWith: "...", | ||
// optional; will be fetched from Turnkey if not provided | ||
ethereumAddress: "...", | ||
}); | ||
``` | ||
## 0.2.7 | ||
@@ -4,0 +36,0 @@ |
@@ -6,3 +6,3 @@ import type { LocalAccount } from "viem"; | ||
organizationId: string; | ||
privateKeyId: string; | ||
signWith: string; | ||
ethereumAddress?: string; | ||
@@ -9,0 +9,0 @@ }): Promise<LocalAccount>; |
@@ -10,8 +10,17 @@ "use strict"; | ||
async function createAccount(input) { | ||
const { client, organizationId, privateKeyId } = input; | ||
const { client, organizationId, signWith } = input; | ||
let { ethereumAddress } = input; | ||
// Fetch the address if we don't have it | ||
if (ethereumAddress === undefined) { | ||
if (!signWith) { | ||
throw new http_1.TurnkeyActivityError({ | ||
message: `Missing signWith parameter`, | ||
}); | ||
} | ||
if ((0, viem_2.isAddress)(signWith)) { | ||
// override provided `ethereumAddress` | ||
ethereumAddress = signWith; | ||
} | ||
else if (!ethereumAddress) { | ||
// we have a private key ID, but not an ethereumAddress | ||
const data = await client.getPrivateKey({ | ||
privateKeyId: privateKeyId, | ||
privateKeyId: signWith, | ||
organizationId: organizationId, | ||
@@ -22,3 +31,3 @@ }); | ||
throw new http_1.TurnkeyActivityError({ | ||
message: `Unable to find Ethereum address for key ${privateKeyId} under organization ${organizationId}`, | ||
message: `Unable to find Ethereum address for key ${signWith} under organization ${organizationId}`, | ||
}); | ||
@@ -30,3 +39,3 @@ } | ||
signMessage: function ({ message, }) { | ||
return signMessage(client, message, organizationId, privateKeyId); | ||
return signMessage(client, message, organizationId, signWith); | ||
}, | ||
@@ -37,6 +46,6 @@ signTransaction: function (transaction, args) { | ||
: args.serializer; | ||
return signTransaction(client, transaction, serializer, organizationId, privateKeyId); | ||
return signTransaction(client, transaction, serializer, organizationId, signWith); | ||
}, | ||
signTypedData: function (typedData) { | ||
return signTypedData(client, typedData, organizationId, privateKeyId); | ||
return signTypedData(client, typedData, organizationId, signWith); | ||
}, | ||
@@ -86,20 +95,20 @@ }); | ||
exports.createApiKeyAccount = createApiKeyAccount; | ||
async function signMessage(client, message, organizationId, privateKeyId) { | ||
async function signMessage(client, message, organizationId, signWith) { | ||
const hashedMessage = (0, viem_2.hashMessage)(message); | ||
const signedMessage = await signMessageWithErrorWrapping(client, hashedMessage, organizationId, privateKeyId); | ||
const signedMessage = await signMessageWithErrorWrapping(client, hashedMessage, organizationId, signWith); | ||
return `${signedMessage}`; | ||
} | ||
async function signTransaction(client, transaction, serializer, organizationId, privateKeyId) { | ||
async function signTransaction(client, transaction, serializer, organizationId, signWith) { | ||
const serializedTx = serializer(transaction); | ||
const nonHexPrefixedSerializedTx = serializedTx.replace(/^0x/, ""); | ||
return await signTransactionWithErrorWrapping(client, nonHexPrefixedSerializedTx, organizationId, privateKeyId); | ||
return await signTransactionWithErrorWrapping(client, nonHexPrefixedSerializedTx, organizationId, signWith); | ||
} | ||
async function signTypedData(client, data, organizationId, privateKeyId) { | ||
async function signTypedData(client, data, organizationId, signWith) { | ||
const hashToSign = (0, viem_1.hashTypedData)(data); | ||
return await signMessageWithErrorWrapping(client, hashToSign, organizationId, privateKeyId); | ||
return await signMessageWithErrorWrapping(client, hashToSign, organizationId, signWith); | ||
} | ||
async function signTransactionWithErrorWrapping(client, unsignedTransaction, organizationId, privateKeyId) { | ||
async function signTransactionWithErrorWrapping(client, unsignedTransaction, organizationId, signWith) { | ||
let signedTx; | ||
try { | ||
signedTx = await signTransactionImpl(client, unsignedTransaction, organizationId, privateKeyId); | ||
signedTx = await signTransactionImpl(client, unsignedTransaction, organizationId, signWith); | ||
} | ||
@@ -117,3 +126,3 @@ catch (error) { | ||
} | ||
async function signTransactionImpl(client, unsignedTransaction, organizationId, privateKeyId) { | ||
async function signTransactionImpl(client, unsignedTransaction, organizationId, signWith) { | ||
const { activity } = await client.signTransaction({ | ||
@@ -123,3 +132,3 @@ type: "ACTIVITY_TYPE_SIGN_TRANSACTION_V2", | ||
parameters: { | ||
signWith: privateKeyId, | ||
signWith, | ||
type: "TRANSACTION_TYPE_ETHEREUM", | ||
@@ -141,6 +150,6 @@ unsignedTransaction: unsignedTransaction, | ||
} | ||
async function signMessageWithErrorWrapping(client, message, organizationId, privateKeyId) { | ||
async function signMessageWithErrorWrapping(client, message, organizationId, signWith) { | ||
let signedMessage; | ||
try { | ||
signedMessage = await signMessageImpl(client, message, organizationId, privateKeyId); | ||
signedMessage = await signMessageImpl(client, message, organizationId, signWith); | ||
} | ||
@@ -158,3 +167,3 @@ catch (error) { | ||
} | ||
async function signMessageImpl(client, message, organizationId, privateKeyId) { | ||
async function signMessageImpl(client, message, organizationId, signWith) { | ||
const { activity } = await client.signRawPayload({ | ||
@@ -164,3 +173,3 @@ type: "ACTIVITY_TYPE_SIGN_RAW_PAYLOAD_V2", | ||
parameters: { | ||
signWith: privateKeyId, | ||
signWith, | ||
payload: message, | ||
@@ -167,0 +176,0 @@ encoding: "PAYLOAD_ENCODING_HEXADECIMAL", |
{ | ||
"name": "@turnkey/viem", | ||
"version": "0.2.7", | ||
"version": "0.3.0", | ||
"main": "./dist/index.js", | ||
@@ -5,0 +5,0 @@ "types": "./dist/index.d.ts", |
@@ -43,3 +43,3 @@ # @turnkey/viem | ||
organizationId: "...", | ||
privateKeyId: "...", | ||
signWith: "...", | ||
// optional; will be fetched from Turnkey if not provided | ||
@@ -46,0 +46,0 @@ ethereumAddress: "...", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
41086
3.29%428
4.39%