@phala/dstack-sdk
Version:
26 lines (23 loc) • 1.18 kB
JavaScript
import { sha256 } from '@noble/hashes/sha256';
import { bytesToHex } from '@noble/hashes/utils';
import { privateKeyToAccount } from 'viem/accounts';
// src/viem.ts
function toViemAccount(keyResponse) {
if (keyResponse.__name__ === "GetTlsKeyResponse") {
console.warn("toViemAccount: Please don't use `deriveKey` method to get key, use `getKey` instead.");
const hex2 = Array.from(keyResponse.asUint8Array(32)).map((b) => b.toString(16).padStart(2, "0")).join("");
return privateKeyToAccount(`0x${hex2}`);
}
const hex = Array.from(keyResponse.key).map((b) => b.toString(16).padStart(2, "0")).join("");
return privateKeyToAccount(`0x${hex}`);
}
function toViemAccountSecure(keyResponse) {
if (keyResponse.__name__ === "GetTlsKeyResponse") {
console.warn("toViemAccountSecure: Please don't use `deriveKey` method to get key, use `getKey` instead.");
const hex2 = bytesToHex(sha256(keyResponse.asUint8Array()));
return privateKeyToAccount(`0x${hex2}`);
}
const hex = Array.from(keyResponse.key).map((b) => b.toString(16).padStart(2, "0")).join("");
return privateKeyToAccount(`0x${hex}`);
}
export { toViemAccount, toViemAccountSecure };