Skip to content

Commit

Permalink
sui types
Browse files Browse the repository at this point in the history
  • Loading branch information
awojciak committed Aug 10, 2023
1 parent e892e34 commit f6843f3
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 16 deletions.
3 changes: 2 additions & 1 deletion sdk/apps/polkadot/src/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''
Expand Down Expand Up @@ -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 })
Expand Down
5 changes: 2 additions & 3 deletions sdk/apps/sui/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@ import {
SuiSignTransactionBlockInput
} from '@mysten/wallet-standard'
import {
AppBaseInitialize,
BaseApp,
DeeplinkConnect,
getWalletsMetadata,
MessageToSign,
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<AppBaseInitialize, 'network'>

interface SuiAppEvents {
userConnected: (e: UserConnectedEvent) => void
userDisconnected: (e: UserDisconnectedEvent) => void
Expand Down
8 changes: 5 additions & 3 deletions sdk/apps/sui/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -69,13 +70,14 @@ export class ClientSui extends EventEmitter<ClientSuiEvents> {
await this.baseClient.connect(connect)
this.sessionId = connect.sessionId
}
public getPendingRequests = async (sessionId?: string) => {
public getPendingRequests = async (sessionId?: string): Promise<SuiRequest[]> => {
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 ({
Expand Down
27 changes: 26 additions & 1 deletion sdk/apps/sui/src/e2e.test.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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

Expand Down Expand Up @@ -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)
})
})
4 changes: 2 additions & 2 deletions sdk/apps/sui/src/http-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions sdk/apps/sui/src/http-client.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -21,10 +21,12 @@ export class HttpClientSui {
await this.baseClient.connect(connect)
}
public getPendingRequests = async (request: Omit<HttpGetPendingRequestsRequest, 'clientId'>) => {
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<HttpGetPendingRequestRequest, 'clientId'>) => {
return await this.baseClient.getPendingRequest(request)
const rq = await this.baseClient.getPendingRequest(request)
return parseRequest(rq, request.sessionId)
}
public resolveSignTransaction = async ({
requestId,
Expand Down
3 changes: 2 additions & 1 deletion sdk/apps/sui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export {
type ResolveSignSuiTransactions,
type RejectRequest as RejectHttpRequest
} from './http-client'
export * from './utils'
export * from './utils'
export * from './requestTypes'
24 changes: 24 additions & 0 deletions sdk/apps/sui/src/requestTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { ContentType, MessageToSign, TransactionToSign } from '@nightlylabs/nightly-connect-base'

export interface SignTransactionsSuiRequest {
type: ContentType.SignTransactions
requestId: string
transactions: Array<TransactionToSign>
sessionId: string
}
export interface SignMessagesSuiRequest {
type: ContentType.SignMessages
requestId: string
messages: Array<MessageToSign>
sessionId: string
}
export interface CustomSuiRequest {
type: ContentType.Custom
requestId: string
content?: string
sessionId: string
}
export type SuiRequest =
| SignTransactionsSuiRequest
| SignMessagesSuiRequest
| CustomSuiRequest
38 changes: 36 additions & 2 deletions sdk/apps/sui/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AppBaseInitialize, 'network'>

export const SUI_NETWORK = 'Sui'

Expand Down Expand Up @@ -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
}
}
}

0 comments on commit f6843f3

Please sign in to comment.