@osiris-ai/web3-evm-sdk
Version:
Osiris Web3 EVM SDK
132 lines (129 loc) • 3.88 kB
JavaScript
import { hashMessage, serializeSignature } from 'viem';
import { toAccount } from 'viem/accounts';
import axios from 'axios';
// @osiris-ai/web3-evm-sdk - Web3 EVM SDK for Authentication and API Clients
var EVMWalletClient = class {
hubBaseUrl;
accessToken;
deploymentId;
constructor(hubBaseUrl, accessToken, deploymentId) {
this.hubBaseUrl = hubBaseUrl;
this.accessToken = accessToken;
this.deploymentId = deploymentId;
}
async getWalletRecords() {
try {
const response = await axios.get(`${this.hubBaseUrl}/packages/packages/user/deployments/${this.deploymentId}/auth`, {
method: "GET",
headers: {
Authorization: `Bearer ${this.accessToken}`
}
});
const data = await response.data;
if (data.status.toLowerCase() === "success") {
const walletRecords = data.data.filter(
(record) => record.serviceClient.serviceClientType === "embedded_wallet"
);
if (walletRecords.length === 0) {
throw new Error("No wallet record found");
}
return walletRecords.map((record) => record.connection.metadata);
} else {
throw new Error(data.error);
}
} catch (error) {
throw new Error(error.message || "Failed to get wallet records");
}
}
async signMessage(message, chainId, walletAddress) {
const hashedMessage = hashMessage(message);
const response = await axios.post(`${this.hubBaseUrl}/hub/action/${this.deploymentId}/turnkey`, {
method: "signMessage",
chain: chainId,
walletAddress,
payload: [hashedMessage]
}, {
headers: {
Authorization: `Bearer ${this.accessToken}`
}
});
const data = await response.data;
return data.signatures[0];
}
async signTransaction(abi, transaction, chainId, walletAddress) {
const response = await axios.post(
`${this.hubBaseUrl}/hub/action/${this.deploymentId}/turnkey`,
{
method: "signTransaction",
chain: chainId,
walletAddress,
abi: {
abi
},
payload: {
serializedTransaction: transaction
}
},
{
headers: {
Authorization: `Bearer ${this.accessToken}`
}
}
);
const data = await response.data;
if (response.status !== 200 || data.error) {
throw new Error(data.error || "Failed to sign transaction");
}
return `0x${data.signedTransaction}`;
}
async signTypedData(typedData, chainId, walletAddress, metadata) {
const response = await axios.post(`${this.hubBaseUrl}/hub/action/${this.deploymentId}/turnkey`, {
method: "signTypedData",
chain: chainId,
walletAddress,
payload: [typedData],
metadata
}, {
headers: {
Authorization: `Bearer ${this.accessToken}`
}
});
const data = await response.data;
if (response.status !== 200 || data.error) {
throw new Error(data.error || "Failed to sign transaction");
}
const { r, s, v } = data.signatures[0];
return serializeSignature({
r: `0x${r}`,
s: `0x${s}`,
yParity: Number(v)
});
}
async getViemAccount(address, chainId) {
return toAccount({
address,
signMessage: async ({ message }) => {
const signature = await this.signMessage(
message,
chainId,
address
);
return signature;
},
signTransaction: async (transaction, options = {}) => {
throw new Error("Not implemented");
},
signTypedData: async (typedData) => {
const signature = await this.signTypedData(
typedData,
chainId,
address
);
return signature;
}
});
}
};
export { EVMWalletClient };
//# sourceMappingURL=client.js.map
//# sourceMappingURL=client.js.map