diff --git a/lib/accounts.ts b/lib/accounts.ts index ae14e88..417183a 100644 --- a/lib/accounts.ts +++ b/lib/accounts.ts @@ -1,67 +1,7 @@ -import { - Abi, - Account, - AllowArray, - Call, - CallData, - Contract, - DeployAccountContractPayload, - DeployContractResponse, - InvokeFunctionResponse, - RPC, - UniversalDetails, - num, - uint256, -} from "starknet"; +import { Account, Call, CallData, RPC, uint256 } from "starknet"; import { manager } from "./manager"; -import { KeyPair } from "./signers/signers"; import { ethAddress, strkAddress } from "./tokens"; -export class ArgentAccount extends Account { - // Increase the gas limit by 30% to avoid failures due to gas estimation being too low with tx v3 and transactions the use escaping - override async deployAccount( - payload: DeployAccountContractPayload, - details?: UniversalDetails, - ): Promise { - details ||= {}; - if (!details.skipValidate) { - details.skipValidate = false; - } - return super.deployAccount(payload, details); - } - - override async execute( - calls: AllowArray, - abis?: Abi[], - details: UniversalDetails = {}, - ): Promise { - details ||= {}; - if (!details.skipValidate) { - details.skipValidate = false; - } - if (details.resourceBounds) { - return super.execute(calls, abis, details); - } - const estimate = await this.estimateFee(calls, details); - return super.execute(calls, abis, { - ...details, - resourceBounds: { - ...estimate.resourceBounds, - l1_gas: { - ...estimate.resourceBounds.l1_gas, - max_amount: num.toHexString(num.addPercent(estimate.resourceBounds.l1_gas.max_amount, 30)), - }, - }, - }); - } -} - -export interface ArgentWallet { - account: ArgentAccount; - accountContract: Contract; - owner: KeyPair; -} - export const deployer = (() => { if (manager.isDevnet) { const devnetAddress = "0x64b48806902a367c8598f4f95c305e8c1a1acba5f082d294a43793113115691"; @@ -87,15 +27,15 @@ export const genericAccount = (() => { export const deployerV3 = setDefaultTransactionVersionV3(deployer); -export function setDefaultTransactionVersion(account: ArgentAccount, newVersion: boolean): Account { +export function setDefaultTransactionVersion(account: Account, newVersion: boolean): Account { const newDefaultVersion = newVersion ? RPC.ETransactionVersion.V3 : RPC.ETransactionVersion.V2; if (account.transactionVersion === newDefaultVersion) { return account; } - return new ArgentAccount(account, account.address, account.signer, account.cairoVersion, newDefaultVersion); + return new Account(account, account.address, account.signer, account.cairoVersion, newDefaultVersion); } -export function setDefaultTransactionVersionV3(account: ArgentAccount): ArgentAccount { +export function setDefaultTransactionVersionV3(account: Account): Account { return setDefaultTransactionVersion(account, true); } diff --git a/lib/index.ts b/lib/index.ts index 5ba5b06..07452b0 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -10,7 +10,6 @@ export * from "./contracts"; export * from "./devnet"; export * from "./expectations"; export * from "./manager"; -export * from "./openZeppelinAccount"; export * from "./receipts"; export * from "./signers/legacy"; export * from "./signers/signers"; diff --git a/lib/openZeppelinAccount.ts b/lib/openZeppelinAccount.ts deleted file mode 100644 index b47df9c..0000000 --- a/lib/openZeppelinAccount.ts +++ /dev/null @@ -1,60 +0,0 @@ -import { Account, CallData, RPC, hash, num } from "starknet"; -import { deployer, fundAccountCall } from "./accounts"; -import { ContractWithClass } from "./contracts"; -import { manager } from "./manager"; -import { LegacyMultisigSigner, LegacyStarknetKeyPair } from "./signers/legacy"; -import { randomStarknetKeyPair } from "./signers/signers"; - -export type DeployOzAccountParams = { - useTxV3?: boolean; - owner?: LegacyStarknetKeyPair; - salt?: string; - fundingAmount?: number | bigint; -}; - -export type DeployOzAccountResult = { - account: Account; - accountContract: ContractWithClass; - deployTxHash: string; - useTxV3: boolean; - owner: LegacyStarknetKeyPair; - salt: string; -}; - -export async function deployOpenZeppelinAccount(params: DeployOzAccountParams): Promise { - const classHash = await manager.declareLocalContract("AccountUpgradeable"); - const finalParams = { - ...params, - salt: params.salt ?? num.toHex(randomStarknetKeyPair().privateKey), - owner: params.owner ?? new LegacyStarknetKeyPair(), - useTxV3: params.useTxV3 ?? false, - }; - - const constructorCalldata = CallData.compile({ - owner: finalParams.owner.publicKey, - }); - - const contractAddress = hash.calculateContractAddressFromHash(finalParams.salt, classHash, constructorCalldata, 0); - - const fundingCall = finalParams.useTxV3 - ? await fundAccountCall(contractAddress, finalParams.fundingAmount ?? 1e18, "STRK") - : await fundAccountCall(contractAddress, finalParams.fundingAmount ?? 1e16, "ETH"); - const response = await deployer.execute([fundingCall!]); - await manager.waitForTransaction(response.transaction_hash); - - const defaultTxVersion = finalParams.useTxV3 ? RPC.ETransactionVersion.V3 : RPC.ETransactionVersion.V2; - const signer = new LegacyMultisigSigner([finalParams.owner]); - const account = new Account(manager, contractAddress, signer, "1", defaultTxVersion); - - const { transaction_hash: deployTxHash } = await account.deploySelf({ - classHash, - constructorCalldata, - addressSalt: finalParams.salt, - }); - - await manager.waitForTransaction(deployTxHash); - const accountContract = await manager.loadContract(account.address, classHash); - accountContract.connect(account); - - return { ...finalParams, account, accountContract, deployTxHash }; -} diff --git a/lib/signers/legacy.ts b/lib/signers/legacy.ts index 37a39b1..1132ce7 100644 --- a/lib/signers/legacy.ts +++ b/lib/signers/legacy.ts @@ -20,20 +20,6 @@ export class LegacyArgentSigner extends RawSigner { } } -export class LegacyMultisigSigner extends RawSigner { - constructor(public keys: RawSigner[]) { - super(); - } - - async signRaw(messageHash: string): Promise { - const keys = []; - for (const key of this.keys) { - keys.push(await key.signRaw(messageHash)); - } - return keys.flat(); - } -} - export abstract class LegacyKeyPair extends RawSigner { abstract get privateKey(): string; abstract get publicKey(): bigint; @@ -60,28 +46,3 @@ export class LegacyStarknetKeyPair extends LegacyKeyPair { return [r.toString(), s.toString()]; } } - -export class LegacyMultisigKeyPair extends LegacyKeyPair { - pk: string; - - constructor(pk?: string | bigint) { - super(); - this.pk = pk ? `${pk}` : `0x${encode.buf2hex(ec.starkCurve.utils.randomPrivateKey())}`; - } - - public get publicKey() { - return BigInt(ec.starkCurve.getStarkKey(this.pk)); - } - - public get privateKey(): string { - return this.pk; - } - - public async signRaw(messageHash: string): Promise { - const { r, s } = ec.starkCurve.sign(messageHash, this.pk); - return [this.publicKey.toString(), r.toString(), s.toString()]; - } -} - -export const randomLegacyMultisigKeyPairs = (length: number) => - Array.from({ length }, () => new LegacyMultisigKeyPair()).sort((n1, n2) => (n1.publicKey < n2.publicKey ? -1 : 1)); diff --git a/lib/signers/signers.ts b/lib/signers/signers.ts index 0f3c956..a6f4cd4 100644 --- a/lib/signers/signers.ts +++ b/lib/signers/signers.ts @@ -1,10 +1,6 @@ import { - CairoCustomEnum, - CairoOption, - CairoOptionVariant, Call, CallData, - Calldata, DeclareSignerDetails, DeployAccountSignerDetails, InvocationsSignerDetails, @@ -17,11 +13,7 @@ import { V3DeclareSignerDetails, V3DeployAccountSignerDetails, V3InvocationsSignerDetails, - ec, - encode, hash, - num, - shortString, stark, transaction, typedData, @@ -129,174 +121,3 @@ export abstract class RawSigner implements SignerInterface { return await this.signRaw(msgHash); } } - -export class MultisigSigner extends RawSigner { - constructor(public keys: KeyPair[]) { - super(); - } - - async signRaw(messageHash: string): Promise { - const keys = []; - for (const key of this.keys) { - keys.push(await key.signRaw(messageHash)); - } - return [keys.length.toString(), keys.flat()].flat(); - } -} - -export class ArgentSigner extends MultisigSigner { - constructor( - public owner: KeyPair = randomStarknetKeyPair(), - public guardian?: KeyPair, - ) { - const signers = [owner]; - if (guardian) { - signers.push(guardian); - } - super(signers); - } -} - -export abstract class KeyPair extends RawSigner { - abstract get signer(): CairoCustomEnum; - abstract get guid(): bigint; - abstract get storedValue(): bigint; - - public get compiledSigner(): Calldata { - return CallData.compile([this.signer]); - } - - public get signerAsOption() { - return new CairoOption(CairoOptionVariant.Some, { - signer: this.signer, - }); - } - public get compiledSignerAsOption() { - return CallData.compile([this.signerAsOption]); - } -} - -export class StarknetKeyPair extends KeyPair { - pk: string; - - constructor(pk?: string | bigint) { - super(); - this.pk = pk ? num.toHex(pk) : `0x${encode.buf2hex(ec.starkCurve.utils.randomPrivateKey())}`; - } - - public get privateKey(): string { - return this.pk; - } - - public get publicKey() { - return BigInt(ec.starkCurve.getStarkKey(this.pk)); - } - - public get guid() { - return BigInt(hash.computePoseidonHash(shortString.encodeShortString("Starknet Signer"), this.publicKey)); - } - - public get storedValue() { - return this.publicKey; - } - - public get signer(): CairoCustomEnum { - return signerTypeToCustomEnum(SignerType.Starknet, { signer: this.publicKey }); - } - - public async signRaw(messageHash: string): Promise { - const { r, s } = ec.starkCurve.sign(messageHash, this.pk); - return starknetSignatureType(this.publicKey, r, s); - } -} - -export class EstimateStarknetKeyPair extends KeyPair { - readonly pubKey: bigint; - - constructor(pubKey: bigint) { - super(); - this.pubKey = pubKey; - } - - public get privateKey(): string { - throw new Error("EstimateStarknetKeyPair does not have a private key"); - } - - public get publicKey() { - return this.pubKey; - } - - public get guid() { - return BigInt(hash.computePoseidonHash(shortString.encodeShortString("Starknet Signer"), this.publicKey)); - } - - public get storedValue() { - return this.publicKey; - } - - public get signer(): CairoCustomEnum { - return signerTypeToCustomEnum(SignerType.Starknet, { signer: this.publicKey }); - } - - public async signRaw(messageHash: string): Promise { - const fakeR = "0x6cefb49a1f4eb406e8112db9b8cdf247965852ddc5ca4d74b09e42471689495"; - const fakeS = "0x25760910405a052b7f08ec533939c54948bc530c662c5d79e8ff416579087f7"; - return starknetSignatureType(this.publicKey, fakeR, fakeS); - } -} - -export function starknetSignatureType( - signer: bigint | number | string, - r: bigint | number | string, - s: bigint | number | string, -) { - return CallData.compile([signerTypeToCustomEnum(SignerType.Starknet, { signer, r, s })]); -} - -export function zeroStarknetSignatureType() { - return signerTypeToCustomEnum(SignerType.Starknet, { signer: 0 }); -} - -// reflects the signer type in signer_signature.cairo -// needs to be updated for the signer types -// used to convert signertype to guid -export enum SignerType { - Starknet, - Secp256k1, - Secp256r1, - Eip191, - Webauthn, -} - -export function signerTypeToCustomEnum(signerType: SignerType, value: any): CairoCustomEnum { - const contents = { - Starknet: undefined, - Secp256k1: undefined, - Secp256r1: undefined, - Eip191: undefined, - Webauthn: undefined, - }; - - if (signerType === SignerType.Starknet) { - contents.Starknet = value; - } else if (signerType === SignerType.Secp256k1) { - contents.Secp256k1 = value; - } else if (signerType === SignerType.Secp256r1) { - contents.Secp256r1 = value; - } else if (signerType === SignerType.Eip191) { - contents.Eip191 = value; - } else if (signerType === SignerType.Webauthn) { - contents.Webauthn = value; - } else { - throw new Error(`Unknown SignerType`); - } - - return new CairoCustomEnum(contents); -} - -export function sortByGuid(keys: KeyPair[]) { - return keys.sort((n1, n2) => (n1.guid < n2.guid ? -1 : 1)); -} - -export const randomStarknetKeyPair = () => new StarknetKeyPair(); -export const randomStarknetKeyPairs = (length: number) => Array.from({ length }, randomStarknetKeyPair);