Skip to content

Commit

Permalink
Merge pull request #17 from argentlabs/fix/build-and-rename-argent-se…
Browse files Browse the repository at this point in the history
…rvice

fix: build error and rename ArgentSessionService
  • Loading branch information
bluecco authored May 31, 2024
2 parents aecc281 + 5fa2fe4 commit bcb88c9
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 84 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ Executing transactions “from outside” allows an account to submit transactio
This package expose a method in order to get the Call required to perform an execution from outside.

```typescript
// instantiate argent backend session service
const beService = new ArgentBackendSessionService(
// instantiate argent session service
const beService = new ArgentSessionService(
dappKey.publicKey,
accountSessionSignature
)
Expand Down
58 changes: 30 additions & 28 deletions src/__tests__/sessionBackendService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
OutsideExecution,
getOutsideExecutionTypedData,
} from "../outsideExecution"
import { ArgentBackendSessionService } from "../sessionBackendService"
import { BackendSignatureResponse } from "../sessionTypes"
import { ArgentSessionService } from "../argentSessionService"
import { ArgentServiceSignatureResponse } from "../sessionTypes"
import { getSessionTypedData } from "../utils"

export const restHandlers = [
Expand All @@ -34,10 +34,10 @@ export const restHandlers = [

const server = setupServer(...restHandlers)

describe("ArgentBackendSessionService", () => {
describe("ArgentSessionService", () => {
const pubkey = "0x1234567890abcdef"
const accountSessionSignature = ["123", "456"]
const service = new ArgentBackendSessionService(
const service = new ArgentSessionService(
pubkey,
accountSessionSignature,
"http://localhost:3000",
Expand All @@ -53,7 +53,7 @@ describe("ArgentBackendSessionService", () => {
afterEach(() => server.resetHandlers())

describe("signTxAndSession", () => {
it("should sign the transactions and session and return a backend signature response", async () => {
it("should sign the transactions and session and return a argent service signature response", async () => {
const cacheAuthorisation = false

const sessionRequest = {
Expand All @@ -79,20 +79,21 @@ describe("ArgentBackendSessionService", () => {
StarknetChainId.SN_SEPOLIA,
)

const response: BackendSignatureResponse = await service.signTxAndSession(
[],
{
cairoVersion: "1",
chainId: StarknetChainId.SN_SEPOLIA,
maxFee: 1000n,
nonce: 1,
version: "0x2",
walletAddress: stark.randomAddress(),
},
sessionTypedData,
[10n, 20n],
cacheAuthorisation,
)
const response: ArgentServiceSignatureResponse =
await service.signTxAndSession(
[],
{
cairoVersion: "1",
chainId: StarknetChainId.SN_SEPOLIA,
maxFee: 1000n,
nonce: 1,
version: "0x2",
walletAddress: stark.randomAddress(),
},
sessionTypedData,
[10n, 20n],
cacheAuthorisation,
)

expect(response).toEqual({
publicKey: "0x123",
Expand All @@ -105,7 +106,7 @@ describe("ArgentBackendSessionService", () => {
})

describe("signOutsideTxAndSession", () => {
it("should sign the outside session and return a backend signature response", async () => {
it("should sign the outside session and return an argent session signature response", async () => {
const sessionTokenToSign = {
expires_at: 1234567890,
allowed_methods: [
Expand Down Expand Up @@ -155,14 +156,15 @@ describe("ArgentBackendSessionService", () => {

const chainId: StarknetChainId = constants.StarknetChainId.SN_SEPOLIA

const response: BackendSignatureResponse = await service.signSessionEFO(
sessionTokenToSign,
accountAddress,
getOutsideExecutionTypedData(outsideExecution, chainId),
[123n, 456n],
false,
chainId,
)
const response: ArgentServiceSignatureResponse =
await service.signSessionEFO(
sessionTokenToSign,
accountAddress,
getOutsideExecutionTypedData(outsideExecution, chainId),
[123n, 456n],
false,
chainId,
)

expect(response).toStrictEqual({
publicKey: "0x123",
Expand Down
40 changes: 22 additions & 18 deletions src/__tests__/sessionDappService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "starknet"
import { StarknetChainId } from "starknet-types"
import { beforeAll, describe, expect, it, vi } from "vitest"
import { ArgentBackendSessionService } from "../sessionBackendService"
import { ArgentSessionService } from "../argentSessionService"
import { SessionDappService } from "../sessionDappService"
import { outsideExecutionTypedDataFixture } from "./fixture"

Expand All @@ -35,7 +35,7 @@ const sessionRequest = {
const sessionAuthorizationSignature = ["signature1", "signature2"]

describe("SessionDappService", () => {
let argentBackend: ArgentBackendSessionService
let argentSessionService: ArgentSessionService
let chainId: StarknetChainId
let privateDappKey: Uint8Array
let publicDappKey: string
Expand All @@ -45,20 +45,20 @@ describe("SessionDappService", () => {
chainId = StarknetChainId.SN_GOERLI
privateDappKey = ec.starkCurve.utils.randomPrivateKey()
publicDappKey = ec.starkCurve.getStarkKey(privateDappKey)
argentBackend = new ArgentBackendSessionService(
argentSessionService = new ArgentSessionService(
publicDappKey,
sessionAuthorizationSignature,
)

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

sessionDappService = new SessionDappService(argentBackend, chainId, {
sessionDappService = new SessionDappService(argentSessionService, chainId, {
privateKey: privateDappKey,
publicKey: publicDappKey,
})
Expand All @@ -79,7 +79,7 @@ describe("SessionDappService", () => {
expect(account).toBeInstanceOf(Account)
})

it("should signTransaction calling argent backend", async () => {
it("should signTransaction calling argent session service", async () => {
const invokationDetails: V2InvocationsSignerDetails = {
cairoVersion: "1",
chainId,
Expand All @@ -100,8 +100,8 @@ describe("SessionDappService", () => {
)

expect(signatureResult).toBeInstanceOf(Array)
expect(argentBackend.signTxAndSession).toHaveBeenCalled()
expect(argentBackend.signTxAndSession).toReturnWith({
expect(argentSessionService.signTxAndSession).toHaveBeenCalled()
expect(argentSessionService.signTxAndSession).toReturnWith({
publicKey: "0x123",
r: 10n,
s: 10n,
Expand All @@ -125,11 +125,13 @@ describe("SessionDappService", () => {
},
]

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

const outsideExecutionCall =
await sessionDappService.getOutsideExecutionCall(
Expand Down Expand Up @@ -167,11 +169,13 @@ describe("SessionDappService", () => {
},
]

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

const { signature, outsideExecutionTypedData } =
await sessionDappService.getOutsideExecutionTypedData(
Expand Down
26 changes: 13 additions & 13 deletions src/sessionBackendService.ts → src/argentSessionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ import {
} from "starknet"

import { StarknetChainId, TypedData } from "starknet-types"
import { ARGENT_BACKEND_BASE_URL } from "./constants"
import { ARGENT_SESSION_SERVICE_BASE_URL } from "./constants"
import { SignSessionError } from "./errors"
import {
BackendSessionBody,
BackendSignSessionBody,
BackendSignatureResponse,
ArgentServiceSessionBody,
ArgentServiceSignSessionBody,
ArgentServiceSignatureResponse,
OffChainSession,
} from "./sessionTypes"
import { getSessionTypedData } from "./utils"

export class ArgentBackendSessionService {
export class ArgentSessionService {
constructor(
public pubkey: string,
private accountSessionSignature: Signature,
private argentBackendBaseUrl = ARGENT_BACKEND_BASE_URL,
private argentSessionServiceBaseUrl = ARGENT_SESSION_SERVICE_BASE_URL,
) {}

public async signTxAndSession(
Expand All @@ -35,7 +35,7 @@ export class ArgentBackendSessionService {
sessionTypedData: TypedData,
sessionSignature: bigint[],
cacheAuthorisation: boolean,
): Promise<BackendSignatureResponse> {
): Promise<ArgentServiceSignatureResponse> {
const compiledCalldata = transaction.getExecuteCalldata(
calls,
transactionsDetail.cairoVersion,
Expand All @@ -50,7 +50,7 @@ export class ArgentBackendSessionService {
this.accountSessionSignature,
)

const session: BackendSessionBody = {
const session: ArgentServiceSessionBody = {
sessionHash,
sessionAuthorisation,
cacheAuthorisation,
Expand All @@ -64,7 +64,7 @@ export class ArgentBackendSessionService {
},
}

const body: BackendSignSessionBody = {
const body: ArgentServiceSignSessionBody = {
session,
}

Expand Down Expand Up @@ -119,7 +119,7 @@ export class ArgentBackendSessionService {
}

const response = await fetch(
`${this.argentBackendBaseUrl}/cosigner/signSession`,
`${this.argentSessionServiceBaseUrl}/cosigner/signSession`,
{
method: "POST",
headers: {
Expand All @@ -145,7 +145,7 @@ export class ArgentBackendSessionService {
sessionSignature: bigint[],
cacheAuthorisation: boolean,
chainId: StarknetChainId,
): Promise<BackendSignatureResponse> {
): Promise<ArgentServiceSignatureResponse> {
const sessionMessageHash = typedData.getMessageHash(
getSessionTypedData(sessionTokenToSign, chainId),
accountAddress,
Expand All @@ -155,7 +155,7 @@ export class ArgentBackendSessionService {
this.accountSessionSignature,
)

const session: BackendSessionBody = {
const session: ArgentServiceSessionBody = {
sessionHash: sessionMessageHash,
sessionAuthorisation,
cacheAuthorisation,
Expand Down Expand Up @@ -187,7 +187,7 @@ export class ArgentBackendSessionService {
)

const response = await fetch(
`${this.argentBackendBaseUrl}/cosigner/signSessionEFO`,
`${this.argentSessionServiceBaseUrl}/cosigner/signSessionEFO`,
{
method: "POST",
headers: {
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const ARGENT_BACKEND_BASE_URL = "https://cloud.argent-api.com/v1"
export const ARGENT_SESSION_SERVICE_BASE_URL = "https://cloud.argent-api.com/v1"
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export * from "./utils"
export type * from "./sessionTypes"
export * from "./outsideExecution"

export { ArgentBackendSessionService } from "./sessionBackendService"
export { ArgentSessionService } from "./argentSessionService"
export { SessionDappService } from "./sessionDappService"

export { type SignSessionError } from "./errors"
14 changes: 6 additions & 8 deletions src/sessionDappService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ import {
getOutsideCall,
getOutsideExecutionTypedData,
} from "./outsideExecution"
import { ArgentBackendSessionService } from "./sessionBackendService"
import { ArgentSessionService } from "./argentSessionService"
import {
BackendSignatureResponse,
ArgentServiceSignatureResponse,
DappKey,
OffChainSession,
OnChainSession,
Expand Down Expand Up @@ -68,7 +68,7 @@ class SessionSigner extends Signer {

export class SessionDappService {
constructor(
private argentBackend: ArgentBackendSessionService,
private argentSessionService: ArgentSessionService,
public chainId: StarknetChainId,
private dappKey: DappKey,
) {}
Expand Down Expand Up @@ -173,7 +173,7 @@ export class SessionDappService {
cacheAuthorisation,
)

const guardianSignature = await this.argentBackend.signTxAndSession(
const guardianSignature = await this.argentSessionService.signTxAndSession(
calls,
invocationSignerDetails,
sessionTypedData,
Expand Down Expand Up @@ -279,7 +279,7 @@ export class SessionDappService {
calls: Call[],
sessionSignature: bigint[],
session_authorization: string[],
guardianSignature: BackendSignatureResponse,
guardianSignature: ArgentServiceSignatureResponse,
cache_authorization: boolean,
) {
return {
Expand Down Expand Up @@ -380,7 +380,6 @@ export class SessionDappService {
await this.getSessionSignatureForOutsideExecutionTypedData(
sessionAuthorizationSignature,
sessionRequest,
messageHash,
calls,
accountAddress,
outsideExecutionTypedData,
Expand Down Expand Up @@ -417,7 +416,7 @@ export class SessionDappService {
cacheAuthorisation,
)

const guardianSignature = await this.argentBackend.signSessionEFO(
const guardianSignature = await this.argentSessionService.signSessionEFO(
sessionRequest,
accountAddress,
outsideExecutionTypedData,
Expand Down Expand Up @@ -463,7 +462,6 @@ export class SessionDappService {
await this.getSessionSignatureForOutsideExecutionTypedData(
sessionAuthorizationSignature,
sessionRequest,
messageHash,
calls,
accountAddress,
currentTypedData,
Expand Down
Loading

0 comments on commit bcb88c9

Please sign in to comment.