Skip to content

Commit

Permalink
fix: signature tight flexible resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
tabaktoni committed Mar 30, 2023
1 parent 8146e45 commit 4990cd8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
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
8 changes: 4 additions & 4 deletions src/types/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ 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 AnySignatureType = string | number | ArraySignatureType | WeierstrassSignatureType;
export type Signature = ArraySignatureType | WeierstrassSignatureType;

export type RawCalldata = BigNumberish[];
export type AllowArray<T> = T | T[];
Expand Down
24 changes: 16 additions & 8 deletions src/utils/stark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { Signature, getStarkKey, utils } from 'micro-starknet';
import { gzip } from 'pako';

import {
AnySignatureType,
ArraySignatureType,
Calldata,
CompressedProgram,
Program,
RawArgs,
Signature as SignatureType,
WeierstrassSignatureType,
} from '../types';
import { addHexPrefix, btoaUniversal } from './encode';
import { stringify } from './json';
Expand Down Expand Up @@ -40,26 +42,32 @@ export function makeAddress(input: string): string {
return addHexPrefix(input).toLowerCase();
}

export function formatSignature(sig?: SignatureType): string[] {
if (!sig) return [];
export function formatSignature(sig?: AnySignatureType): ArraySignatureType {
if (!sig) throw Error('formatSignature: provided signature is undefined');
if (typeof sig === 'string' || typeof sig === 'number') {
return [toHex(sig)];
}
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 WeierstrassSignatureType or an array');
}
}

export function signatureToDecimalArray(sig?: SignatureType): string[] {
export function signatureToDecimalArray(sig?: AnySignatureType): ArraySignatureType {
return bigNumberishArrayToDecimalStringArray(formatSignature(sig));
}

export function signatureToHexArray(sig?: SignatureType): string[] {
export function signatureToHexArray(sig?: AnySignatureType): ArraySignatureType {
return bigNumberishArrayToHexadecimalStringArray(formatSignature(sig));
}

export function parseSignature(sig?: string[]) {
if (!sig) return undefined;
export function parseSignature(sig?: ArraySignatureType): WeierstrassSignatureType {
if (!sig) throw Error('parseSignature: provided signature is undefined');

const [r, s] = sig;
return new Signature(toBigInt(r), toBigInt(s));
Expand Down

0 comments on commit 4990cd8

Please sign in to comment.