diff --git a/sdk/apps/polkadot/src/e2e.test.ts b/sdk/apps/polkadot/src/e2e.test.ts index ecda7222..b958eaa8 100644 --- a/sdk/apps/polkadot/src/e2e.test.ts +++ b/sdk/apps/polkadot/src/e2e.test.ts @@ -60,7 +60,7 @@ describe('Base Client tests', () => { await client.connect(msg) }) - test.skip('#on("signTransactions")', async () => { + test('#on("signTransactions")', async () => { const payload = polkadotApi.tx.balances.transfer(RECEIVER, 50000000) let payloadToSign = '' @@ -93,6 +93,7 @@ describe('Base Client tests', () => { client.removeListener('signTransactions') }) test('#getPendingRequests()', async () => { + client.removeListener('signTransactions') const payload = polkadotApi.tx.balances.transfer(RECEIVER, 50000000) payload.signAsync(RECEIVER, { signer: app.signer }) payload.signAsync(RECEIVER, { signer: app.signer }) diff --git a/sdk/apps/sui/src/app.ts b/sdk/apps/sui/src/app.ts index 21a99f4a..619bf8ab 100644 --- a/sdk/apps/sui/src/app.ts +++ b/sdk/apps/sui/src/app.ts @@ -7,7 +7,6 @@ import { SuiSignTransactionBlockInput } from '@mysten/wallet-standard' import { - AppBaseInitialize, BaseApp, DeeplinkConnect, getWalletsMetadata, @@ -15,10 +14,10 @@ import { TransactionToSign } from '@nightlylabs/nightly-connect-base' import { EventEmitter } from 'eventemitter3' -import { SUI_NETWORK } from './utils' +import { AppSuiInitialize, SUI_NETWORK } from './utils' import { UserDisconnectedEvent } from '../../../bindings/UserDisconnectedEvent' import { WalletMetadata } from '../../../bindings/WalletMetadata' -export type AppSuiInitialize = Omit + interface SuiAppEvents { userConnected: (e: UserConnectedEvent) => void userDisconnected: (e: UserDisconnectedEvent) => void diff --git a/sdk/apps/sui/src/client.ts b/sdk/apps/sui/src/client.ts index 2ceed2e2..9f624506 100644 --- a/sdk/apps/sui/src/client.ts +++ b/sdk/apps/sui/src/client.ts @@ -8,8 +8,9 @@ import { TransactionToSign } from '@nightlylabs/nightly-connect-base' import { EventEmitter } from 'eventemitter3' -import { GetPendingRequestsResponse } from '../../../bindings/GetPendingRequestsResponse' import { GetInfoResponse } from '../../../bindings/GetInfoResponse' +import { SuiRequest } from './requestTypes' +import { parseRequest } from './utils' export interface SignSuiTransactionEvent { sessionId: string requestId: string @@ -69,13 +70,14 @@ export class ClientSui extends EventEmitter { await this.baseClient.connect(connect) this.sessionId = connect.sessionId } - public getPendingRequests = async (sessionId?: string) => { + public getPendingRequests = async (sessionId?: string): Promise => { const sessionIdToUse = sessionId || this.sessionId if (sessionIdToUse === undefined) { throw new Error('Session id is undefined') } - return await this.baseClient.getPendingRequests(sessionIdToUse) + const requests = await this.baseClient.getPendingRequests(sessionIdToUse) + return requests.map((request) => parseRequest(request, sessionIdToUse)) } public resolveSignTransaction = async ({ diff --git a/sdk/apps/sui/src/e2e.test.ts b/sdk/apps/sui/src/e2e.test.ts index 7a3336f1..efb781b5 100644 --- a/sdk/apps/sui/src/e2e.test.ts +++ b/sdk/apps/sui/src/e2e.test.ts @@ -1,4 +1,4 @@ -import { Connect, RELAY_ENDPOINT, smartDelay } from '@nightlylabs/nightly-connect-base' +import { Connect, ContentType, RELAY_ENDPOINT, smartDelay } from '@nightlylabs/nightly-connect-base' import { assert, beforeAll, beforeEach, describe, expect, test, vi } from 'vitest' import { AppSui } from './app' import { ClientSui } from './client' @@ -18,6 +18,7 @@ import { blake2b } from '@noble/hashes/blake2b' import { fetch } from 'cross-fetch' import { WalletAccount } from '@mysten/wallet-standard' import { hexToBytes } from '@noble/hashes/utils' +import { SignTransactionsSuiRequest } from './requestTypes' global.fetch = fetch @@ -165,4 +166,28 @@ describe('SUI client tests', () => { await smartDelay() expect(disconnecFn).toHaveBeenCalledOnce() }) + test('#getPendingRequests()', async () => { + const tx = new TransactionBlock() + const coin = tx.splitCoins(tx.gas, [tx.pure(100)]) + tx.transferObjects([coin], tx.pure(RECEIVER_SUI_ADDRESS)) + tx.setSenderIfNotSet(RECEIVER_SUI_ADDRESS) + app.signTransactionBlock({ + transactionBlock: tx, + account: aliceWalletAccount, + chain: 'sui:testnet' + }) + app.signTransactionBlock({ + transactionBlock: tx, + account: aliceWalletAccount, + chain: 'sui:testnet' + }) + await smartDelay(500) + await smartDelay(500) + const requests = await client.getPendingRequests() + expect(requests.length).toBe(2) + expect(requests[0].type).toBe(ContentType.SignTransactions) + expect(requests[1].type).toBe(ContentType.SignTransactions) + const payload1 = requests[0] as SignTransactionsSuiRequest + expect(payload1.transactions.length).toBe(1) + }) }) diff --git a/sdk/apps/sui/src/http-client.test.ts b/sdk/apps/sui/src/http-client.test.ts index c50e8f03..23deedbe 100644 --- a/sdk/apps/sui/src/http-client.test.ts +++ b/sdk/apps/sui/src/http-client.test.ts @@ -70,10 +70,10 @@ describe('SUI http-client tests', () => { await smartDelay() // Query for request const pendingRequest = (await client.getPendingRequests({ sessionId: app.sessionId }))[0] - if (pendingRequest.content.type !== ContentType.SignTransactions) { + if (pendingRequest.type !== ContentType.SignTransactions) { throw new Error('Wrong content type') } - const pendingTx = pendingRequest.content.transactions[0].transaction + const pendingTx = pendingRequest.transactions[0].transaction const { signature, transactionBlockBytes } = await signTransactionBlock( TransactionBlock.from(pendingTx), alice_keypair diff --git a/sdk/apps/sui/src/http-client.ts b/sdk/apps/sui/src/http-client.ts index 641323c1..28b722d4 100644 --- a/sdk/apps/sui/src/http-client.ts +++ b/sdk/apps/sui/src/http-client.ts @@ -1,5 +1,5 @@ import { HttpBaseClient, HttpBaseClientInitialize } from '@nightlylabs/nightly-connect-base' -import { SUI_NETWORK } from './utils' +import { SUI_NETWORK, parseRequest } from './utils' import { HttpConnectSessionRequest } from '../../../bindings/HttpConnectSessionRequest' import { HttpGetPendingRequestsRequest } from '../../../bindings/HttpGetPendingRequestsRequest' import { HttpGetPendingRequestRequest } from '../../../bindings/HttpGetPendingRequestRequest' @@ -21,10 +21,12 @@ export class HttpClientSui { await this.baseClient.connect(connect) } public getPendingRequests = async (request: Omit) => { - return await this.baseClient.getPendingRequests(request) + const requests = await this.baseClient.getPendingRequests(request) + return requests.map((rq) => parseRequest(rq, request.sessionId)) } public getPendingRequest = async (request: Omit) => { - return await this.baseClient.getPendingRequest(request) + const rq = await this.baseClient.getPendingRequest(request) + return parseRequest(rq, request.sessionId) } public resolveSignTransaction = async ({ requestId, diff --git a/sdk/apps/sui/src/index.ts b/sdk/apps/sui/src/index.ts index 91b1c2ad..cb5d62d6 100644 --- a/sdk/apps/sui/src/index.ts +++ b/sdk/apps/sui/src/index.ts @@ -6,4 +6,5 @@ export { type ResolveSignSuiTransactions, type RejectRequest as RejectHttpRequest } from './http-client' -export * from './utils' \ No newline at end of file +export * from './utils' +export * from './requestTypes' \ No newline at end of file diff --git a/sdk/apps/sui/src/requestTypes.ts b/sdk/apps/sui/src/requestTypes.ts new file mode 100644 index 00000000..54b19d41 --- /dev/null +++ b/sdk/apps/sui/src/requestTypes.ts @@ -0,0 +1,24 @@ +import { ContentType, MessageToSign, TransactionToSign } from '@nightlylabs/nightly-connect-base' + +export interface SignTransactionsSuiRequest { + type: ContentType.SignTransactions + requestId: string + transactions: Array + sessionId: string +} +export interface SignMessagesSuiRequest { + type: ContentType.SignMessages + requestId: string + messages: Array + sessionId: string +} +export interface CustomSuiRequest { + type: ContentType.Custom + requestId: string + content?: string + sessionId: string +} +export type SuiRequest = + | SignTransactionsSuiRequest + | SignMessagesSuiRequest + | CustomSuiRequest diff --git a/sdk/apps/sui/src/utils.ts b/sdk/apps/sui/src/utils.ts index a8cd756e..3a38fea7 100644 --- a/sdk/apps/sui/src/utils.ts +++ b/sdk/apps/sui/src/utils.ts @@ -8,8 +8,10 @@ import { Ed25519Keypair } from '@mysten/sui.js' import { blake2b } from '@noble/hashes/blake2b' -import { AppSuiInitialize } from './app' -import { RELAY_ENDPOINT } from '@nightlylabs/nightly-connect-base' +import { AppBaseInitialize, ContentType, RELAY_ENDPOINT, RequestContent } from '@nightlylabs/nightly-connect-base' +import { CustomSuiRequest, SignMessagesSuiRequest, SignTransactionsSuiRequest, SuiRequest } from './requestTypes' + +export type AppSuiInitialize = Omit export const SUI_NETWORK = 'Sui' @@ -48,3 +50,35 @@ export const signTransactionBlock = async (tx: TransactionBlock, account: Ed2551 }) return { transactionBlockBytes, signature } } + +export const parseRequest = (request: RequestContent, sessionId: string): SuiRequest => { + switch (request.content.type) { + case ContentType.SignTransactions: { + const signTransactionsRequest: SignTransactionsSuiRequest = { + type: ContentType.SignTransactions, + requestId: request.requestId, + sessionId: sessionId, + transactions: request.content.transactions + } + return signTransactionsRequest + } + case ContentType.SignMessages: { + const signMessagesRequest: SignMessagesSuiRequest = { + type: ContentType.SignMessages, + requestId: request.requestId, + sessionId: sessionId, + messages: request.content.messages + } + return signMessagesRequest + } + case ContentType.Custom: { + const customRequest: CustomSuiRequest = { + type: ContentType.Custom, + content: request.content.content, + requestId: request.requestId, + sessionId: sessionId + } + return customRequest + } + } +} \ No newline at end of file