diff --git a/__tests__/account.test.ts b/__tests__/account.test.ts index f8d37aed1..f7d87a8b6 100644 --- a/__tests__/account.test.ts +++ b/__tests__/account.test.ts @@ -1,3 +1,5 @@ +import { Signature } from 'micro-starknet'; + import typedDataExample from '../__mocks__/typedDataExample.json'; import { Account, Contract, Provider, TransactionStatus, ec, stark } from '../src'; import { uint256 } from '../src/utils/calldata/cairo'; @@ -184,7 +186,7 @@ describe('deploy and test Wallet', () => { // change the signature to make it invalid const r2 = toBigInt(r) + 123n; - const signature2 = stark.parseSignature([r2.toString(), s]); + const signature2 = new Signature(toBigInt(r2.toString()), toBigInt(s)); if (!signature2) return; diff --git a/src/account/default.ts b/src/account/default.ts index 9aa219830..73a38788c 100644 --- a/src/account/default.ts +++ b/src/account/default.ts @@ -42,9 +42,14 @@ import { transactionVersion, transactionVersion_2, } from '../utils/hash'; -import { BigNumberish, toBigInt, toCairoBool, toHex } from '../utils/num'; +import { BigNumberish, toBigInt, toCairoBool } from '../utils/num'; import { parseContract } from '../utils/provider'; -import { compileCalldata, estimatedFeeToMaxFee, randomAddress } from '../utils/stark'; +import { + compileCalldata, + estimatedFeeToMaxFee, + formatSignature, + randomAddress, +} from '../utils/stark'; import { getExecuteCalldata } from '../utils/transaction'; import { TypedData, getMessageHash } from '../utils/typedData'; import { AccountInterface } from './interface'; @@ -491,7 +496,7 @@ export class Account extends Provider implements AccountInterface { entrypoint: 'isValidSignature', calldata: compileCalldata({ hash: toBigInt(hash).toString(), - signature: [toHex(signature.r), toHex(signature.s)], + signature: formatSignature(signature), }), }); return true; diff --git a/src/contract/default.ts b/src/contract/default.ts index b04b333a2..4df043b63 100644 --- a/src/contract/default.ts +++ b/src/contract/default.ts @@ -8,6 +8,7 @@ import { FunctionAbi, InvokeFunctionResponse, Overrides, + Result, StructAbi, } from '../types'; import assert from '../utils/assert'; @@ -190,7 +191,7 @@ export class Contract implements ContractInterface { method: string, args: Array = [], options: CallOptions = { parseRequest: true, parseResponse: true, formatResponse: undefined } - ): Promise { + ): Promise { assert(this.address !== null, 'contract is not connected to an address'); const blockIdentifier = options?.blockIdentifier || undefined; let calldata = args[0]; diff --git a/src/types/contract.ts b/src/types/contract.ts index 84bbd8945..8513a6161 100644 --- a/src/types/contract.ts +++ b/src/types/contract.ts @@ -1,2 +1,5 @@ export type AsyncContractFunction = (...args: Array) => Promise; export type ContractFunction = (...args: Array) => any; +export type Result = { + [key: string]: any; +}; diff --git a/src/types/lib/index.ts b/src/types/lib/index.ts index 04431570d..685ccddbc 100644 --- a/src/types/lib/index.ts +++ b/src/types/lib/index.ts @@ -3,10 +3,9 @@ import { weierstrass } from '../../utils/ec'; import type { BigNumberish } from '../../utils/num'; import { CompiledContract, CompiledSierraCasm, ContractClass } from './contract'; -// Common Signature Type which needs to be imported from weierstrass -// and imported at many places -// This is because stark.ts doesn't export SignatureType -export type Signature = weierstrass.SignatureType; +export type WeierstrassSignatureType = weierstrass.SignatureType; +export type ArraySignatureType = string[]; +export type Signature = ArraySignatureType | WeierstrassSignatureType; export type RawCalldata = BigNumberish[]; export type AllowArray = T | T[]; diff --git a/src/utils/responseParser/sequencer.ts b/src/utils/responseParser/sequencer.ts index d3644d161..70b17f11a 100644 --- a/src/utils/responseParser/sequencer.ts +++ b/src/utils/responseParser/sequencer.ts @@ -17,7 +17,6 @@ import { TransactionSimulationResponse, } from '../../types'; import { toBigInt } from '../num'; -import { parseSignature } from '../stark'; import { ResponseParser } from '.'; export class SequencerAPIResponseParser extends ResponseParser { @@ -50,8 +49,7 @@ export class SequencerAPIResponseParser extends ResponseParser { 'sender_address' in res.transaction ? (res.transaction.sender_address as string) : undefined, - signature: - 'signature' in res.transaction ? parseSignature(res.transaction.signature) : undefined, + signature: 'signature' in res.transaction ? res.transaction.signature : undefined, transaction_hash: 'transaction_hash' in res.transaction ? res.transaction.transaction_hash : undefined, version: 'version' in res.transaction ? (res.transaction.version as string) : undefined, diff --git a/src/utils/stark.ts b/src/utils/stark.ts index 5e07ad008..87860d84a 100644 --- a/src/utils/stark.ts +++ b/src/utils/stark.ts @@ -1,12 +1,13 @@ -import { Signature, getStarkKey, utils } from 'micro-starknet'; +import { getStarkKey, utils } from 'micro-starknet'; import { gzip } from 'pako'; import { + ArraySignatureType, Calldata, CompressedProgram, Program, RawArgs, - Signature as SignatureType, + Signature, } from '../types'; import { addHexPrefix, btoaUniversal } from './encode'; import { stringify } from './json'; @@ -40,31 +41,27 @@ export function makeAddress(input: string): string { return addHexPrefix(input).toLowerCase(); } -export function formatSignature(sig?: SignatureType): string[] { - if (!sig) return []; +export function formatSignature(sig?: Signature): ArraySignatureType { + if (!sig) throw Error('formatSignature: provided signature is undefined'); + if (Array.isArray(sig)) { + return sig.map((it) => toHex(it)); + } try { const { r, s } = sig; return [toHex(r), toHex(s)]; } catch (e) { - return []; + throw new Error('Signature need to be weierstrass.SignatureType or an array for custom'); } } -export function signatureToDecimalArray(sig?: SignatureType): string[] { +export function signatureToDecimalArray(sig?: Signature): ArraySignatureType { return bigNumberishArrayToDecimalStringArray(formatSignature(sig)); } -export function signatureToHexArray(sig?: SignatureType): string[] { +export function signatureToHexArray(sig?: Signature): ArraySignatureType { return bigNumberishArrayToHexadecimalStringArray(formatSignature(sig)); } -export function parseSignature(sig?: string[]) { - if (!sig) return undefined; - - const [r, s] = sig; - return new Signature(toBigInt(r), toBigInt(s)); -} - /** * @deprecated this function is deprecated use callData instead from calldata.ts */