Skip to content

Commit

Permalink
feat: declare v2 intermidiate
Browse files Browse the repository at this point in the history
  • Loading branch information
tabaktoni committed Mar 13, 2023
1 parent 7cec344 commit a8fdfe3
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ export class Account extends Provider implements AccountInterface {
}

public async declare(
{ contract, classHash: providedClassHash }: DeclareContractPayload,
{ contract, classHash: providedClassHash }: DeclareContractPayload, // TODO: compiledClassHash i casm
transactionsDetail: InvocationsDetails = {}
): Promise<DeclareContractResponse> {
const nonce = toBigInt(transactionsDetail.nonce ?? (await this.getNonce()));
Expand Down
17 changes: 15 additions & 2 deletions src/provider/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,14 +388,27 @@ export class SequencerProvider implements ProviderInterface {
{ senderAddress, contractDefinition, signature }: DeclareContractTransaction,
details: InvocationsDetailsWithNonce
): Promise<DeclareContractResponse> {
if ('program' in contractDefinition) {
return this.fetchEndpoint('add_transaction', undefined, {
type: TransactionType.DECLARE,
contract_class: contractDefinition,
nonce: toHex(details.nonce),
signature: signatureToDecimalArray(signature),
sender_address: senderAddress,
max_fee: toHex(details.maxFee || 0),
version: '0x1',
}).then(this.responseParser.parseDeclareContractResponse);
}
// Cairo 1
return this.fetchEndpoint('add_transaction', undefined, {
type: TransactionType.DECLARE,
sender_address: senderAddress,
compiled_class_hash: details.compiledClassHash,
contract_class: contractDefinition,
nonce: toHex(details.nonce),
signature: signatureToDecimalArray(signature),
sender_address: senderAddress,
max_fee: toHex(details.maxFee || 0),
version: toHex(details.version || 1),
version: '0x2',
}).then(this.responseParser.parseDeclareContractResponse);
}

Expand Down
13 changes: 11 additions & 2 deletions src/signer/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,24 @@ export class Signer implements SignerInterface {

public async signDeclareTransaction(
// contractClass: ContractClass, // Should be used once class hash is present in ContractClass
{ classHash, senderAddress, chainId, maxFee, version, nonce }: DeclareSignerDetails
{
classHash,
senderAddress,
chainId,
maxFee,
version,
nonce,
compiledClassHash,
}: DeclareSignerDetails
) {
const msgHash = calculateDeclareTransactionHash(
classHash,
senderAddress,
version,
maxFee,
chainId,
nonce
nonce,
compiledClassHash
);

return starkCurve.sign(msgHash, this.pk);
Expand Down
1 change: 1 addition & 0 deletions src/types/api/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export namespace Sequencer {
nonce: BigNumberish;
max_fee?: BigNumberish;
version?: BigNumberish;
compiled_class_hash?: string; // v2 declare
};

export type DeployTransaction = {
Expand Down
5 changes: 4 additions & 1 deletion src/types/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ export type InvocationsDetails = {
version?: BigNumberish;
};

export type InvocationsDetailsWithNonce = InvocationsDetails & { nonce: BigNumberish };
export type InvocationsDetailsWithNonce = InvocationsDetails & {
nonce: BigNumberish;
compiledClassHash?: string;
};

export enum TransactionStatus {
NOT_RECEIVED = 'NOT_RECEIVED',
Expand Down
1 change: 1 addition & 0 deletions src/types/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface DeclareSignerDetails {
maxFee: BigNumberish;
version: BigNumberish;
nonce: BigNumberish;
compiledClassHash?: string;
}

export type DeployAccountSignerDetails = Required<DeployAccountContractPayload> &
Expand Down
5 changes: 3 additions & 2 deletions src/utils/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ export function calculateDeclareTransactionHash(
version: BigNumberish,
maxFee: BigNumberish,
chainId: StarknetChainId,
nonce: BigNumberish
nonce: BigNumberish,
compiledClassHash?: string
): string {
return calculateTransactionHashCommon(
TransactionHashPrefix.DECLARE,
Expand All @@ -136,7 +137,7 @@ export function calculateDeclareTransactionHash(
[classHash],
maxFee,
chainId,
[nonce]
[nonce, ...(compiledClassHash ? [compiledClassHash] : [])]
);
}

Expand Down

0 comments on commit a8fdfe3

Please sign in to comment.