@axiom-crypto/keystore-sdk
Version:
Keystore Rollup SDK
88 lines • 2.81 kB
JavaScript
import { bytesToHex, concat, hexToBytes, keccak256, pad } from "viem";
import { RLP } from "@ethereumjs/rlp";
export function initAccountCounterfactual({ salt, dataHash, vkey, nodeClient, }) {
const paddedSalt = pad(salt, { size: 32 });
const paddedDataHash = pad(dataHash, { size: 32 });
const vkeyHash = keccak256(vkey);
const keystoreAddress = keccak256(concat([paddedSalt, paddedDataHash, vkeyHash]));
return initAccount({
address: keystoreAddress,
salt: paddedSalt,
dataHash: paddedDataHash,
vkey,
nodeClient,
});
}
export function initAccountFromAddress({ address, dataHash, vkey, nodeClient, }) {
const paddedSalt = pad("0x00", { size: 32 });
return initAccount({
address,
salt: paddedSalt,
dataHash,
vkey,
nodeClient,
});
}
export function initAccount({ address, salt, dataHash, vkey, nodeClient, }) {
const accountData = {
address,
salt,
dataHash,
vkey,
};
const accountActions = keystoreAccountActions({
address,
salt,
dataHash,
vkey,
nodeClient,
});
return {
...accountData,
...accountActions,
};
}
export function initFromRlpEncoded({ rlpEncoded, nodeClient, }) {
const rlpDecoded = RLP.decode(hexToBytes(rlpEncoded));
const address = bytesToHex(rlpDecoded[0]);
const salt = bytesToHex(rlpDecoded[1]);
const dataHash = bytesToHex(rlpDecoded[2]);
const vkey = bytesToHex(rlpDecoded[3]);
const accountData = {
address,
salt,
dataHash,
vkey,
};
const accountActions = keystoreAccountActions({ address, salt, dataHash, vkey, nodeClient });
return {
...accountData,
...accountActions,
};
}
function keystoreAccountActions({ address, salt, dataHash, vkey, nodeClient, }) {
return {
rlpEncode: () => {
return bytesToHex(RLP.encode([address, salt, dataHash, vkey]));
},
getNonce: async ({ block } = {}) => {
if (!nodeClient) {
throw new Error("A NodeClient is required for `getNonce`");
}
return await nodeClient.getTransactionCount({ address, block });
},
getBalance: async ({ block } = {}) => {
if (!nodeClient) {
throw new Error("A NodeClient is required for `getBalance`");
}
return await nodeClient.getBalance({ address, block });
},
getState: async ({ block } = {}) => {
if (!nodeClient) {
throw new Error("A NodeClient is required for `getState`");
}
return await nodeClient.getStateAt({ address, block });
},
};
}
//# sourceMappingURL=init.js.map