Skip to content

Commit

Permalink
Merge pull request #22 from argentlabs/fix/efo-version
Browse files Browse the repository at this point in the history
fix: efo version not hardcoded and defaulted to 2
  • Loading branch information
bluecco authored Jul 30, 2024
2 parents 3bbde81 + 6a218f9 commit 7d178e6
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 7 deletions.
44 changes: 44 additions & 0 deletions src/__tests__/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,47 @@ export const outsideExecutionTypedDataFixture = (to: string) => ({
],
},
})

export const outsideExecutionTypedDataFixture_v2 = (to: string) => ({
types: {
StarknetDomain: [
{ name: "name", type: "shortstring" },
{ name: "version", type: "shortstring" },
{ name: "chainId", type: "shortstring" },
{ name: "revision", type: "shortstring" },
],
OutsideExecution: [
{ name: "Caller", type: "ContractAddress" },
{ name: "Nonce", type: "felt" },
{ name: "Execute After", type: "u128" },
{ name: "Execute Before", type: "u128" },
{ name: "Calls", type: "Call*" },
],
Call: [
{ name: "To", type: "ContractAddress" },
{ name: "Selector", type: "selector" },
{ name: "Calldata", type: "felt*" },
],
},
primaryType: "OutsideExecution",
domain: {
name: "Account.execute_from_outside",
version: "2",
chainId: "0x534e5f5345504f4c4941",
revision: "1",
},
message: {
Caller: "0x414e595f43414c4c4552",
"Execute After": 1,
"Execute Before": 999999999999999,
Nonce: "0x1",
Calls: [
{
Selector:
"0x2f043e3dd744a94c4afd9d35321851d44c14a01fa5b2b8a05e054b12e5cc838",
To: to,
Calldata: ["0x123"],
},
],
},
})
50 changes: 49 additions & 1 deletion src/__tests__/sessionDappService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {
import { beforeAll, describe, expect, it, vi } from "vitest"
import { ArgentSessionService } from "../argentSessionService"
import { SessionDappService } from "../sessionDappService"
import { outsideExecutionTypedDataFixture } from "./fixture"
import {
outsideExecutionTypedDataFixture,
outsideExecutionTypedDataFixture_v2,
} from "./fixture"

const allowedMethodContractAddress = stark.randomAddress()

Expand Down Expand Up @@ -195,4 +198,49 @@ describe("SessionDappService", () => {
outsideExecutionTypedDataFixture(allowedMethodContractAddress),
)
})

it("should get an outside execution call with getOutsideExecutionTypedData", async () => {
const provider = new RpcProvider()
vi.spyOn(provider, "getChainId").mockImplementation(
async () => constants.StarknetChainId.SN_SEPOLIA,
)
const address = stark.randomAddress()
const execute_after = 1
const execute_before = 999999999999999
const nonce = "0x1"
const calls: Call[] = [
{
contractAddress: allowedMethodContractAddress,
entrypoint: "some_method",
calldata: ["0x123"],
},
]

vi.spyOn(argentSessionService, "signSessionEFO").mockImplementation(
async () => ({
publicKey: "0x123",
r: 10n,
s: 10n,
}),
)

const { signature, outsideExecutionTypedData } =
await sessionDappService.getOutsideExecutionTypedData(
sessionRequest,
sessionAuthorizationSignature,
false,
calls,
address,
"",
execute_after,
execute_before,
nonce,
"2",
)

expect(signature).toBeInstanceOf(Array)
expect(outsideExecutionTypedData).toStrictEqual(
outsideExecutionTypedDataFixture_v2(allowedMethodContractAddress),
)
})
})
14 changes: 9 additions & 5 deletions src/outsideExecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ export const typesRev1 = {
],
}

function getDomain(chainId: string) {
function getDomain(chainId: string, version: string = "1") {
// WARNING! Version and revision are encoded as numbers in the StarkNetDomain type and not as shortstring
// This is due to a bug in the Braavos implementation, and has been kept for compatibility
return {
name: "Account.execute_from_outside",
version: "1",
chainId: chainId,
version,
chainId,
revision: "1",
}
}
Expand Down Expand Up @@ -71,21 +71,23 @@ export function getTypedDataHash(
outsideExecution: OutsideExecution,
accountAddress: num.BigNumberish,
chainId: string,
version: string = "1",
): string {
return typedData.getMessageHash(
getOutsideExecutionTypedData(outsideExecution, chainId),
getOutsideExecutionTypedData(outsideExecution, chainId, version),
accountAddress,
)
}

export function getOutsideExecutionTypedData(
outsideExecution: OutsideExecution,
chainId: string,
version: string = "1",
) {
return {
types: typesRev1,
primaryType: "OutsideExecution",
domain: getDomain(chainId),
domain: getDomain(chainId, version),
message: {
Caller: outsideExecution.caller,
Nonce: outsideExecution.nonce,
Expand All @@ -108,11 +110,13 @@ export async function getOutsideExecutionCall(
signer: SignerInterface,
provider: ProviderInterface | Provider,
chainId?: constants.StarknetChainId,
version: string = "1",
): Promise<Call> {
chainId = chainId ?? (await provider.getChainId())
const currentTypedData = getOutsideExecutionTypedData(
outsideExecution,
chainId,
version,
)
const signature = await signer.signMessage(currentTypedData, accountAddress)
return {
Expand Down
7 changes: 6 additions & 1 deletion src/sessionDappService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ export class SessionDappService {
execute_after?: BigNumberish,
execute_before?: BigNumberish,
nonce?: BigNumberish,
version: string = "1",
): OutsideExecutionTypedData {
const outsideExecution = this.buildOutsideExecution(
calls,
Expand All @@ -351,7 +352,7 @@ export class SessionDappService {
nonce,
)

return getOutsideExecutionTypedData(outsideExecution, chainId)
return getOutsideExecutionTypedData(outsideExecution, chainId, version)
}

public async getOutsideExecutionCall(
Expand All @@ -364,6 +365,7 @@ export class SessionDappService {
execute_after?: BigNumberish,
execute_before?: BigNumberish,
nonce?: BigNumberish,
version: string = "1",
): Promise<Call> {
const outsideExecution = this.buildOutsideExecution(
calls,
Expand All @@ -376,6 +378,7 @@ export class SessionDappService {
const outsideExecutionTypedData = getOutsideExecutionTypedData(
outsideExecution,
this.chainId,
version,
)

const signature =
Expand Down Expand Up @@ -450,6 +453,7 @@ export class SessionDappService {
execute_after?: BigNumberish,
execute_before?: BigNumberish,
nonce?: BigNumberish,
version: string = "1",
): Promise<OutsideExecutionTypedDataResponse> {
const currentTypedData = this.buildOutsideExecutionTypedData(
this.chainId,
Expand All @@ -458,6 +462,7 @@ export class SessionDappService {
execute_after,
execute_before,
nonce,
version,
)

const signature =
Expand Down

0 comments on commit 7d178e6

Please sign in to comment.