|
| 1 | +import { secp256k1 } from '@noble/curves/secp256k1'; |
| 2 | + |
| 3 | +import { |
| 4 | + Call, |
| 5 | + DeclareSignerDetails, |
| 6 | + DeployAccountSignerDetails, |
| 7 | + InvocationsSignerDetails, |
| 8 | + Signature, |
| 9 | + TypedData, |
| 10 | + V2DeclareSignerDetails, |
| 11 | + V2DeployAccountSignerDetails, |
| 12 | + V2InvocationsSignerDetails, |
| 13 | + V3DeclareSignerDetails, |
| 14 | + V3DeployAccountSignerDetails, |
| 15 | + V3InvocationsSignerDetails, |
| 16 | +} from '../types'; |
| 17 | +import { ETransactionVersion2, ETransactionVersion3 } from '../types/api'; |
| 18 | +import { CallData } from '../utils/calldata'; |
| 19 | +import { addHexPrefix, buf2hex, removeHexPrefix, sanitizeHex } from '../utils/encode'; |
| 20 | +import { ethRandomPrivateKey } from '../utils/eth'; |
| 21 | +import { |
| 22 | + calculateDeclareTransactionHash, |
| 23 | + calculateDeployAccountTransactionHash, |
| 24 | + calculateInvokeTransactionHash, |
| 25 | +} from '../utils/hash'; |
| 26 | +import { toHex } from '../utils/num'; |
| 27 | +import { intDAM } from '../utils/stark'; |
| 28 | +import { getExecuteCalldata } from '../utils/transaction'; |
| 29 | +import { getMessageHash } from '../utils/typedData'; |
| 30 | +import { SignerInterface } from './interface'; |
| 31 | + |
| 32 | +/** |
| 33 | + * Signer for accounts using Ethereum signature |
| 34 | + */ |
| 35 | +export class EthSigner implements SignerInterface { |
| 36 | + protected pk: string; // hex string without 0x and odd number of characters |
| 37 | + |
| 38 | + constructor(pk: Uint8Array | string = ethRandomPrivateKey()) { |
| 39 | + this.pk = |
| 40 | + pk instanceof Uint8Array |
| 41 | + ? removeHexPrefix(sanitizeHex(buf2hex(pk))) |
| 42 | + : removeHexPrefix(sanitizeHex(toHex(pk))); |
| 43 | + } |
| 44 | + |
| 45 | + public async getPubKey(): Promise<string> { |
| 46 | + return addHexPrefix(buf2hex(secp256k1.getPublicKey(this.pk))); |
| 47 | + } |
| 48 | + |
| 49 | + public async signMessage(typedData: TypedData, accountAddress: string): Promise<Signature> { |
| 50 | + const msgHash = getMessageHash(typedData, accountAddress); |
| 51 | + return secp256k1.sign(removeHexPrefix(sanitizeHex(msgHash)), this.pk); |
| 52 | + } |
| 53 | + |
| 54 | + public async signTransaction( |
| 55 | + transactions: Call[], |
| 56 | + details: InvocationsSignerDetails |
| 57 | + ): Promise<Signature> { |
| 58 | + const compiledCalldata = getExecuteCalldata(transactions, details.cairoVersion); |
| 59 | + let msgHash; |
| 60 | + |
| 61 | + // TODO: How to do generic union discriminator for all like this |
| 62 | + if (Object.values(ETransactionVersion2).includes(details.version as any)) { |
| 63 | + const det = details as V2InvocationsSignerDetails; |
| 64 | + msgHash = calculateInvokeTransactionHash({ |
| 65 | + ...det, |
| 66 | + senderAddress: det.walletAddress, |
| 67 | + compiledCalldata, |
| 68 | + version: det.version, |
| 69 | + }); |
| 70 | + } else if (Object.values(ETransactionVersion3).includes(details.version as any)) { |
| 71 | + const det = details as V3InvocationsSignerDetails; |
| 72 | + msgHash = calculateInvokeTransactionHash({ |
| 73 | + ...det, |
| 74 | + senderAddress: det.walletAddress, |
| 75 | + compiledCalldata, |
| 76 | + version: det.version, |
| 77 | + nonceDataAvailabilityMode: intDAM(det.nonceDataAvailabilityMode), |
| 78 | + feeDataAvailabilityMode: intDAM(det.feeDataAvailabilityMode), |
| 79 | + }); |
| 80 | + } else { |
| 81 | + throw Error('unsupported signTransaction version'); |
| 82 | + } |
| 83 | + |
| 84 | + return secp256k1.sign(removeHexPrefix(sanitizeHex(msgHash)), this.pk); |
| 85 | + } |
| 86 | + |
| 87 | + public async signDeployAccountTransaction( |
| 88 | + details: DeployAccountSignerDetails |
| 89 | + ): Promise<Signature> { |
| 90 | + const compiledConstructorCalldata = CallData.compile(details.constructorCalldata); |
| 91 | + /* const version = BigInt(details.version).toString(); */ |
| 92 | + let msgHash; |
| 93 | + |
| 94 | + if (Object.values(ETransactionVersion2).includes(details.version as any)) { |
| 95 | + const det = details as V2DeployAccountSignerDetails; |
| 96 | + msgHash = calculateDeployAccountTransactionHash({ |
| 97 | + ...det, |
| 98 | + salt: det.addressSalt, |
| 99 | + constructorCalldata: compiledConstructorCalldata, |
| 100 | + version: det.version, |
| 101 | + }); |
| 102 | + } else if (Object.values(ETransactionVersion3).includes(details.version as any)) { |
| 103 | + const det = details as V3DeployAccountSignerDetails; |
| 104 | + msgHash = calculateDeployAccountTransactionHash({ |
| 105 | + ...det, |
| 106 | + salt: det.addressSalt, |
| 107 | + compiledConstructorCalldata, |
| 108 | + version: det.version, |
| 109 | + nonceDataAvailabilityMode: intDAM(det.nonceDataAvailabilityMode), |
| 110 | + feeDataAvailabilityMode: intDAM(det.feeDataAvailabilityMode), |
| 111 | + }); |
| 112 | + } else { |
| 113 | + throw Error('unsupported signDeployAccountTransaction version'); |
| 114 | + } |
| 115 | + |
| 116 | + return secp256k1.sign(removeHexPrefix(sanitizeHex(msgHash)), this.pk); |
| 117 | + } |
| 118 | + |
| 119 | + public async signDeclareTransaction( |
| 120 | + // contractClass: ContractClass, // Should be used once class hash is present in ContractClass |
| 121 | + details: DeclareSignerDetails |
| 122 | + ): Promise<Signature> { |
| 123 | + let msgHash; |
| 124 | + |
| 125 | + if (Object.values(ETransactionVersion2).includes(details.version as any)) { |
| 126 | + const det = details as V2DeclareSignerDetails; |
| 127 | + msgHash = calculateDeclareTransactionHash({ |
| 128 | + ...det, |
| 129 | + version: det.version, |
| 130 | + }); |
| 131 | + } else if (Object.values(ETransactionVersion3).includes(details.version as any)) { |
| 132 | + const det = details as V3DeclareSignerDetails; |
| 133 | + msgHash = calculateDeclareTransactionHash({ |
| 134 | + ...det, |
| 135 | + version: det.version, |
| 136 | + nonceDataAvailabilityMode: intDAM(det.nonceDataAvailabilityMode), |
| 137 | + feeDataAvailabilityMode: intDAM(det.feeDataAvailabilityMode), |
| 138 | + }); |
| 139 | + } else { |
| 140 | + throw Error('unsupported signDeclareTransaction version'); |
| 141 | + } |
| 142 | + |
| 143 | + return secp256k1.sign(removeHexPrefix(sanitizeHex(msgHash)), this.pk); |
| 144 | + } |
| 145 | +} |
0 commit comments