Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix call response type & Signature type #570

Merged
merged 4 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion __tests__/account.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;

Expand Down
11 changes: 8 additions & 3 deletions src/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,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 { fromCallsToExecuteCalldata } from '../utils/transaction';
import { TypedData, getMessageHash } from '../utils/typedData';
import { AccountInterface } from './interface';
Expand Down Expand Up @@ -470,7 +475,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;
Expand Down
3 changes: 2 additions & 1 deletion src/contract/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
FunctionAbi,
InvokeFunctionResponse,
Overrides,
Result,
StructAbi,
} from '../types';
import assert from '../utils/assert';
Expand Down Expand Up @@ -190,7 +191,7 @@ export class Contract implements ContractInterface {
method: string,
args: Array<any> = [],
options: CallOptions = { parseRequest: true, parseResponse: true, formatResponse: undefined }
): Promise<object> {
): Promise<Result> {
assert(this.address !== null, 'contract is not connected to an address');
const blockIdentifier = options?.blockIdentifier || undefined;
let calldata = args[0];
Expand Down
3 changes: 3 additions & 0 deletions src/types/contract.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export type AsyncContractFunction<T = any> = (...args: Array<any>) => Promise<T>;
export type ContractFunction = (...args: Array<any>) => any;
export type Result = {
[key: string]: any;
};
7 changes: 3 additions & 4 deletions src/types/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 | T[];
Expand Down
4 changes: 1 addition & 3 deletions src/utils/responseParser/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
TransactionSimulationResponse,
} from '../../types';
import { toBigInt } from '../num';
import { parseSignature } from '../stark';
import { ResponseParser } from '.';

export class SequencerAPIResponseParser extends ResponseParser {
Expand Down Expand Up @@ -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,
Expand Down
25 changes: 11 additions & 14 deletions src/utils/stark.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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
*/
Expand Down