@turnkey/viem
Advanced tools
Comparing version 0.1.1 to 0.2.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createApiKeyAccount = void 0; | ||
exports.createApiKeyAccount = exports.createAccount = void 0; | ||
const viem_1 = require("viem"); | ||
@@ -8,17 +8,52 @@ const accounts_1 = require("viem/accounts"); | ||
const http_1 = require("@turnkey/http"); | ||
const api_key_stamper_1 = require("@turnkey/api-key-stamper"); | ||
async function createAccount(input) { | ||
const { client, organizationId, privateKeyId } = input; | ||
let { ethereumAddress } = input; | ||
// Fetch the address if we don't have it | ||
if (ethereumAddress === undefined) { | ||
const data = await client.getPrivateKey({ | ||
privateKeyId: privateKeyId, | ||
organizationId: organizationId, | ||
}); | ||
ethereumAddress = data.privateKey.addresses.find((item) => item.format === "ADDRESS_FORMAT_ETHEREUM")?.address; | ||
if (typeof ethereumAddress !== "string" || !ethereumAddress) { | ||
throw new http_1.TurnkeyActivityError({ | ||
message: `Unable to find Ethereum address for key ${privateKeyId} under organization ${organizationId}`, | ||
}); | ||
} | ||
} | ||
return (0, accounts_1.toAccount)({ | ||
address: ethereumAddress, | ||
signMessage: function ({ message, }) { | ||
return signMessage(client, message, organizationId, privateKeyId); | ||
}, | ||
signTransaction: function (transaction, args) { | ||
const serializer = !args?.serializer | ||
? viem_1.serializeTransaction | ||
: args.serializer; | ||
return signTransaction(client, transaction, serializer, organizationId, privateKeyId); | ||
}, | ||
signTypedData: function (typedData) { | ||
return signTypedData(client, typedData, organizationId, privateKeyId); | ||
}, | ||
}); | ||
} | ||
exports.createAccount = createAccount; | ||
/** | ||
* Creates a new Custom Account backed by a Turnkey API key. | ||
* @deprecated use {@link createAccount} instead. | ||
*/ | ||
async function createApiKeyAccount(config) { | ||
const { apiPublicKey, apiPrivateKey, baseUrl, organizationId, privateKeyId } = config; | ||
(0, http_1.init)({ | ||
apiPublicKey, | ||
apiPrivateKey, | ||
baseUrl, | ||
const stamper = new api_key_stamper_1.ApiKeyStamper({ | ||
apiPublicKey: apiPublicKey, | ||
apiPrivateKey: apiPrivateKey, | ||
}); | ||
const data = await http_1.TurnkeyApi.getPrivateKey({ | ||
body: { | ||
privateKeyId: privateKeyId, | ||
organizationId: organizationId, | ||
}, | ||
const client = new http_1.TurnkeyClient({ | ||
baseUrl: baseUrl, | ||
}, stamper); | ||
const data = await client.getPrivateKey({ | ||
privateKeyId: privateKeyId, | ||
organizationId: organizationId, | ||
}); | ||
@@ -34,3 +69,3 @@ const ethereumAddress = data.privateKey.addresses.find((item) => item.format === "ADDRESS_FORMAT_ETHEREUM")?.address; | ||
signMessage: function ({ message, }) { | ||
return signMessage(message, organizationId, privateKeyId); | ||
return signMessage(client, message, organizationId, privateKeyId); | ||
}, | ||
@@ -41,6 +76,6 @@ signTransaction: function (transaction, args) { | ||
: args.serializer; | ||
return signTransaction(transaction, serializer, organizationId, privateKeyId); | ||
return signTransaction(client, transaction, serializer, organizationId, privateKeyId); | ||
}, | ||
signTypedData: function (typedData) { | ||
return signTypedData(typedData, organizationId, privateKeyId); | ||
return signTypedData(client, typedData, organizationId, privateKeyId); | ||
}, | ||
@@ -50,20 +85,20 @@ }); | ||
exports.createApiKeyAccount = createApiKeyAccount; | ||
async function signMessage(message, organizationId, privateKeyId) { | ||
async function signMessage(client, message, organizationId, privateKeyId) { | ||
const hashedMessage = (0, viem_2.keccak256)(message); | ||
const signedMessage = await signMessageWithErrorWrapping(hashedMessage, organizationId, privateKeyId); | ||
const signedMessage = await signMessageWithErrorWrapping(client, hashedMessage, organizationId, privateKeyId); | ||
return `${signedMessage}`; | ||
} | ||
async function signTransaction(transaction, serializer, organizationId, privateKeyId) { | ||
async function signTransaction(client, transaction, serializer, organizationId, privateKeyId) { | ||
const serializedTx = serializer(transaction); | ||
const nonHexPrefixedSerializedTx = serializedTx.replace(/^0x/, ""); | ||
return await signTransactionWithErrorWrapping(nonHexPrefixedSerializedTx, organizationId, privateKeyId); | ||
return await signTransactionWithErrorWrapping(client, nonHexPrefixedSerializedTx, organizationId, privateKeyId); | ||
} | ||
async function signTypedData(data, organizationId, privateKeyId) { | ||
async function signTypedData(client, data, organizationId, privateKeyId) { | ||
const hashToSign = (0, viem_1.hashTypedData)(data); | ||
return await signMessageWithErrorWrapping(hashToSign, organizationId, privateKeyId); | ||
return await signMessageWithErrorWrapping(client, hashToSign, organizationId, privateKeyId); | ||
} | ||
async function signTransactionWithErrorWrapping(unsignedTransaction, organizationId, privateKeyId) { | ||
async function signTransactionWithErrorWrapping(client, unsignedTransaction, organizationId, privateKeyId) { | ||
let signedTx; | ||
try { | ||
signedTx = await signTransactionImpl(unsignedTransaction, organizationId, privateKeyId); | ||
signedTx = await signTransactionImpl(client, unsignedTransaction, organizationId, privateKeyId); | ||
} | ||
@@ -81,14 +116,12 @@ catch (error) { | ||
} | ||
async function signTransactionImpl(unsignedTransaction, organizationId, privateKeyId) { | ||
const { activity } = await http_1.TurnkeyApi.signTransaction({ | ||
body: { | ||
type: "ACTIVITY_TYPE_SIGN_TRANSACTION", | ||
organizationId: organizationId, | ||
parameters: { | ||
privateKeyId: privateKeyId, | ||
type: "TRANSACTION_TYPE_ETHEREUM", | ||
unsignedTransaction: unsignedTransaction, | ||
}, | ||
timestampMs: String(Date.now()), // millisecond timestamp | ||
async function signTransactionImpl(client, unsignedTransaction, organizationId, privateKeyId) { | ||
const { activity } = await client.signTransaction({ | ||
type: "ACTIVITY_TYPE_SIGN_TRANSACTION", | ||
organizationId: organizationId, | ||
parameters: { | ||
privateKeyId: privateKeyId, | ||
type: "TRANSACTION_TYPE_ETHEREUM", | ||
unsignedTransaction: unsignedTransaction, | ||
}, | ||
timestampMs: String(Date.now()), // millisecond timestamp | ||
}); | ||
@@ -106,6 +139,6 @@ const { id, status, type } = activity; | ||
} | ||
async function signMessageWithErrorWrapping(message, organizationId, privateKeyId) { | ||
async function signMessageWithErrorWrapping(client, message, organizationId, privateKeyId) { | ||
let signedMessage; | ||
try { | ||
signedMessage = await signMessageImpl(message, organizationId, privateKeyId); | ||
signedMessage = await signMessageImpl(client, message, organizationId, privateKeyId); | ||
} | ||
@@ -123,15 +156,13 @@ catch (error) { | ||
} | ||
async function signMessageImpl(message, organizationId, privateKeyId) { | ||
const { activity } = await http_1.TurnkeyApi.signRawPayload({ | ||
body: { | ||
type: "ACTIVITY_TYPE_SIGN_RAW_PAYLOAD", | ||
organizationId: organizationId, | ||
parameters: { | ||
privateKeyId: privateKeyId, | ||
payload: message, | ||
encoding: "PAYLOAD_ENCODING_HEXADECIMAL", | ||
hashFunction: "HASH_FUNCTION_NO_OP", | ||
}, | ||
timestampMs: String(Date.now()), // millisecond timestamp | ||
async function signMessageImpl(client, message, organizationId, privateKeyId) { | ||
const { activity } = await client.signRawPayload({ | ||
type: "ACTIVITY_TYPE_SIGN_RAW_PAYLOAD", | ||
organizationId: organizationId, | ||
parameters: { | ||
privateKeyId: privateKeyId, | ||
payload: message, | ||
encoding: "PAYLOAD_ENCODING_HEXADECIMAL", | ||
hashFunction: "HASH_FUNCTION_NO_OP", | ||
}, | ||
timestampMs: String(Date.now()), // millisecond timestamp | ||
}); | ||
@@ -138,0 +169,0 @@ const { id, status, type } = activity; |
# @turnkey/viem | ||
## 0.2.0 | ||
### Minor Changes | ||
- Add new `createAccount` method and deprecates the existing `createApiAccount`. `createAccount` offers a superset of functionality and works with stampers (`@turnkey/api-key-stamper` / `@turnkey/webauthn-stamper`) to integrate with API keys or passkeys. | ||
### Patch Changes | ||
- Updated dependencies: @turnkey/http@1.1.0 | ||
- New dependency: @turnkey/api-key-stamper@0.1.0 | ||
## 0.1.1 | ||
@@ -4,0 +15,0 @@ |
import type { LocalAccount } from "viem"; | ||
import { TurnkeyClient } from "@turnkey/http"; | ||
export declare function createAccount(input: { | ||
client: TurnkeyClient; | ||
organizationId: string; | ||
privateKeyId: string; | ||
ethereumAddress?: string; | ||
}): Promise<LocalAccount>; | ||
/** | ||
* Type bundling configuration for an API Key Viem account creation | ||
* @deprecated this is used only with {@link createApiKeyAccount}, a deprecated API. See {@link createAccount}. | ||
*/ | ||
@@ -29,2 +37,3 @@ type TApiKeyAccountConfig = { | ||
* Creates a new Custom Account backed by a Turnkey API key. | ||
* @deprecated use {@link createAccount} instead. | ||
*/ | ||
@@ -31,0 +40,0 @@ export declare function createApiKeyAccount(config: TApiKeyAccountConfig): Promise<LocalAccount>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createApiKeyAccount = void 0; | ||
exports.createApiKeyAccount = exports.createAccount = void 0; | ||
const viem_1 = require("viem"); | ||
@@ -8,17 +8,52 @@ const accounts_1 = require("viem/accounts"); | ||
const http_1 = require("@turnkey/http"); | ||
const api_key_stamper_1 = require("@turnkey/api-key-stamper"); | ||
async function createAccount(input) { | ||
const { client, organizationId, privateKeyId } = input; | ||
let { ethereumAddress } = input; | ||
// Fetch the address if we don't have it | ||
if (ethereumAddress === undefined) { | ||
const data = await client.getPrivateKey({ | ||
privateKeyId: privateKeyId, | ||
organizationId: organizationId, | ||
}); | ||
ethereumAddress = data.privateKey.addresses.find((item) => item.format === "ADDRESS_FORMAT_ETHEREUM")?.address; | ||
if (typeof ethereumAddress !== "string" || !ethereumAddress) { | ||
throw new http_1.TurnkeyActivityError({ | ||
message: `Unable to find Ethereum address for key ${privateKeyId} under organization ${organizationId}`, | ||
}); | ||
} | ||
} | ||
return (0, accounts_1.toAccount)({ | ||
address: ethereumAddress, | ||
signMessage: function ({ message, }) { | ||
return signMessage(client, message, organizationId, privateKeyId); | ||
}, | ||
signTransaction: function (transaction, args) { | ||
const serializer = !args?.serializer | ||
? viem_1.serializeTransaction | ||
: args.serializer; | ||
return signTransaction(client, transaction, serializer, organizationId, privateKeyId); | ||
}, | ||
signTypedData: function (typedData) { | ||
return signTypedData(client, typedData, organizationId, privateKeyId); | ||
}, | ||
}); | ||
} | ||
exports.createAccount = createAccount; | ||
/** | ||
* Creates a new Custom Account backed by a Turnkey API key. | ||
* @deprecated use {@link createAccount} instead. | ||
*/ | ||
async function createApiKeyAccount(config) { | ||
const { apiPublicKey, apiPrivateKey, baseUrl, organizationId, privateKeyId } = config; | ||
(0, http_1.init)({ | ||
apiPublicKey, | ||
apiPrivateKey, | ||
baseUrl, | ||
const stamper = new api_key_stamper_1.ApiKeyStamper({ | ||
apiPublicKey: apiPublicKey, | ||
apiPrivateKey: apiPrivateKey, | ||
}); | ||
const data = await http_1.TurnkeyApi.getPrivateKey({ | ||
body: { | ||
privateKeyId: privateKeyId, | ||
organizationId: organizationId, | ||
}, | ||
const client = new http_1.TurnkeyClient({ | ||
baseUrl: baseUrl, | ||
}, stamper); | ||
const data = await client.getPrivateKey({ | ||
privateKeyId: privateKeyId, | ||
organizationId: organizationId, | ||
}); | ||
@@ -34,3 +69,3 @@ const ethereumAddress = data.privateKey.addresses.find((item) => item.format === "ADDRESS_FORMAT_ETHEREUM")?.address; | ||
signMessage: function ({ message, }) { | ||
return signMessage(message, organizationId, privateKeyId); | ||
return signMessage(client, message, organizationId, privateKeyId); | ||
}, | ||
@@ -41,6 +76,6 @@ signTransaction: function (transaction, args) { | ||
: args.serializer; | ||
return signTransaction(transaction, serializer, organizationId, privateKeyId); | ||
return signTransaction(client, transaction, serializer, organizationId, privateKeyId); | ||
}, | ||
signTypedData: function (typedData) { | ||
return signTypedData(typedData, organizationId, privateKeyId); | ||
return signTypedData(client, typedData, organizationId, privateKeyId); | ||
}, | ||
@@ -50,20 +85,20 @@ }); | ||
exports.createApiKeyAccount = createApiKeyAccount; | ||
async function signMessage(message, organizationId, privateKeyId) { | ||
async function signMessage(client, message, organizationId, privateKeyId) { | ||
const hashedMessage = (0, viem_2.keccak256)(message); | ||
const signedMessage = await signMessageWithErrorWrapping(hashedMessage, organizationId, privateKeyId); | ||
const signedMessage = await signMessageWithErrorWrapping(client, hashedMessage, organizationId, privateKeyId); | ||
return `${signedMessage}`; | ||
} | ||
async function signTransaction(transaction, serializer, organizationId, privateKeyId) { | ||
async function signTransaction(client, transaction, serializer, organizationId, privateKeyId) { | ||
const serializedTx = serializer(transaction); | ||
const nonHexPrefixedSerializedTx = serializedTx.replace(/^0x/, ""); | ||
return await signTransactionWithErrorWrapping(nonHexPrefixedSerializedTx, organizationId, privateKeyId); | ||
return await signTransactionWithErrorWrapping(client, nonHexPrefixedSerializedTx, organizationId, privateKeyId); | ||
} | ||
async function signTypedData(data, organizationId, privateKeyId) { | ||
async function signTypedData(client, data, organizationId, privateKeyId) { | ||
const hashToSign = (0, viem_1.hashTypedData)(data); | ||
return await signMessageWithErrorWrapping(hashToSign, organizationId, privateKeyId); | ||
return await signMessageWithErrorWrapping(client, hashToSign, organizationId, privateKeyId); | ||
} | ||
async function signTransactionWithErrorWrapping(unsignedTransaction, organizationId, privateKeyId) { | ||
async function signTransactionWithErrorWrapping(client, unsignedTransaction, organizationId, privateKeyId) { | ||
let signedTx; | ||
try { | ||
signedTx = await signTransactionImpl(unsignedTransaction, organizationId, privateKeyId); | ||
signedTx = await signTransactionImpl(client, unsignedTransaction, organizationId, privateKeyId); | ||
} | ||
@@ -81,14 +116,12 @@ catch (error) { | ||
} | ||
async function signTransactionImpl(unsignedTransaction, organizationId, privateKeyId) { | ||
const { activity } = await http_1.TurnkeyApi.signTransaction({ | ||
body: { | ||
type: "ACTIVITY_TYPE_SIGN_TRANSACTION", | ||
organizationId: organizationId, | ||
parameters: { | ||
privateKeyId: privateKeyId, | ||
type: "TRANSACTION_TYPE_ETHEREUM", | ||
unsignedTransaction: unsignedTransaction, | ||
}, | ||
timestampMs: String(Date.now()), // millisecond timestamp | ||
async function signTransactionImpl(client, unsignedTransaction, organizationId, privateKeyId) { | ||
const { activity } = await client.signTransaction({ | ||
type: "ACTIVITY_TYPE_SIGN_TRANSACTION", | ||
organizationId: organizationId, | ||
parameters: { | ||
privateKeyId: privateKeyId, | ||
type: "TRANSACTION_TYPE_ETHEREUM", | ||
unsignedTransaction: unsignedTransaction, | ||
}, | ||
timestampMs: String(Date.now()), // millisecond timestamp | ||
}); | ||
@@ -106,6 +139,6 @@ const { id, status, type } = activity; | ||
} | ||
async function signMessageWithErrorWrapping(message, organizationId, privateKeyId) { | ||
async function signMessageWithErrorWrapping(client, message, organizationId, privateKeyId) { | ||
let signedMessage; | ||
try { | ||
signedMessage = await signMessageImpl(message, organizationId, privateKeyId); | ||
signedMessage = await signMessageImpl(client, message, organizationId, privateKeyId); | ||
} | ||
@@ -123,15 +156,13 @@ catch (error) { | ||
} | ||
async function signMessageImpl(message, organizationId, privateKeyId) { | ||
const { activity } = await http_1.TurnkeyApi.signRawPayload({ | ||
body: { | ||
type: "ACTIVITY_TYPE_SIGN_RAW_PAYLOAD", | ||
organizationId: organizationId, | ||
parameters: { | ||
privateKeyId: privateKeyId, | ||
payload: message, | ||
encoding: "PAYLOAD_ENCODING_HEXADECIMAL", | ||
hashFunction: "HASH_FUNCTION_NO_OP", | ||
}, | ||
timestampMs: String(Date.now()), // millisecond timestamp | ||
async function signMessageImpl(client, message, organizationId, privateKeyId) { | ||
const { activity } = await client.signRawPayload({ | ||
type: "ACTIVITY_TYPE_SIGN_RAW_PAYLOAD", | ||
organizationId: organizationId, | ||
parameters: { | ||
privateKeyId: privateKeyId, | ||
payload: message, | ||
encoding: "PAYLOAD_ENCODING_HEXADECIMAL", | ||
hashFunction: "HASH_FUNCTION_NO_OP", | ||
}, | ||
timestampMs: String(Date.now()), // millisecond timestamp | ||
}); | ||
@@ -138,0 +169,0 @@ const { id, status, type } = activity; |
{ | ||
"name": "@turnkey/viem", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"main": "./dist/index.js", | ||
@@ -41,3 +41,4 @@ "types": "./dist/index.d.ts", | ||
"typescript": "5.1", | ||
"@turnkey/http": "1.0.1" | ||
"@turnkey/http": "1.1.0", | ||
"@turnkey/api-key-stamper": "0.1.0" | ||
}, | ||
@@ -44,0 +45,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37634
410
4
+ Added@turnkey/api-key-stamper@0.1.0(transitive)
+ Added@turnkey/http@1.1.0(transitive)
+ Added@turnkey/webauthn-stamper@0.1.0(transitive)
- Removed@turnkey/http@1.0.1(transitive)
Updated@turnkey/http@1.1.0