@lit-protocol/vincent-scaffold-sdk
Version:
Shared build configuration and utilities for Vincent abilities and policies
95 lines (94 loc) • 3.66 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LitActionsSmartSigner = void 0;
class LitActionsSmartSigner {
constructor(config) {
this.signerType = "lit-actions";
if (config.pkpPublicKey.startsWith("0x")) {
config.pkpPublicKey = config.pkpPublicKey.slice(2);
}
this.pkpPublicKey = config.pkpPublicKey;
this.signerAddress = ethers.utils.computeAddress("0x" + config.pkpPublicKey);
this.inner = {
pkpPublicKey: config.pkpPublicKey,
chainId: config.chainId,
}; // Inner client reference
}
async getAddress() {
return this.signerAddress;
}
async signMessage(message) {
let messageToSign;
if (typeof message === "string") {
messageToSign = message;
}
else {
messageToSign =
typeof message.raw === "string"
? ethers.utils.arrayify(message.raw)
: message.raw;
}
const messageHash = ethers.utils.hashMessage(messageToSign);
const sig = await Lit.Actions.signAndCombineEcdsa({
toSign: ethers.utils.arrayify(messageHash),
publicKey: this.pkpPublicKey,
sigName: `alchemyMessage`,
});
const parsedSig = JSON.parse(sig);
return ethers.utils.joinSignature({
r: "0x" + parsedSig.r.substring(2),
s: "0x" + parsedSig.s,
v: parsedSig.v,
});
}
async signTypedData(params) {
// console.log("signTypedData called with params", params);
// Create the EIP-712 hash
const hash = ethers.utils._TypedDataEncoder.hash(params.domain || {}, params.types || {}, params.message || {});
const sig = await Lit.Actions.signAndCombineEcdsa({
toSign: ethers.utils.arrayify(hash),
publicKey: this.pkpPublicKey,
sigName: `alchemyTypedData`,
});
const parsedSig = JSON.parse(sig);
return ethers.utils.joinSignature({
r: "0x" + parsedSig.r.substring(2),
s: "0x" + parsedSig.s,
v: parsedSig.v,
});
}
// reference implementation is from Viem SmartAccountSigner
async signAuthorization(unsignedAuthorization) {
// console.log("signAuthorization called with params", unsignedAuthorization);
const { contractAddress, chainId, nonce } = unsignedAuthorization;
if (!contractAddress || !chainId) {
throw new Error("Invalid authorization: contractAddress and chainId are required");
}
const hash = ethers.utils.keccak256(ethers.utils.hexConcat([
"0x05",
ethers.utils.RLP.encode([
ethers.utils.hexlify(chainId),
contractAddress,
nonce ? ethers.utils.hexlify(nonce) : "0x",
]),
]));
// console.log("hash in signAuthorization", hash);
const sig = await Lit.Actions.signAndCombineEcdsa({
toSign: ethers.utils.arrayify(hash),
publicKey: this.pkpPublicKey,
sigName: `alchemyAuth7702`,
});
const sigObj = JSON.parse(sig);
// console.log("sigObj in signAuthorization", sigObj);
return {
address: (unsignedAuthorization.address || contractAddress),
chainId: chainId,
nonce: nonce,
r: ("0x" + sigObj.r.substring(2)),
s: ("0x" + sigObj.s),
v: BigInt(sigObj.v),
yParity: sigObj.v,
};
}
}
exports.LitActionsSmartSigner = LitActionsSmartSigner;