diff --git a/sdk/apps/modal-example/package.json b/sdk/apps/modal-example/package.json index 4e266604..3afe1f9a 100644 --- a/sdk/apps/modal-example/package.json +++ b/sdk/apps/modal-example/package.json @@ -20,12 +20,12 @@ "@aptos-labs/wallet-standard": "^0.0.11", "@mysten/sui": "1.4.0", "@nightlylabs/nightly-connect-polkadot": "0.0.16", - "@nightlylabs/nightly-connect-solana": "0.0.29", + "@nightlylabs/nightly-connect-solana": "0.0.3", "@nightlylabs/nightly-connect-sui": "0.1.0", "@nightlylabs/wallet-selector-aptos": "0.1.11", "@nightlylabs/wallet-selector-base": "^0.4.2", "@nightlylabs/wallet-selector-polkadot": "0.2.7", - "@nightlylabs/wallet-selector-solana": "0.3.7", + "@nightlylabs/wallet-selector-solana": "0.3.8", "@nightlylabs/wallet-selector-sui": "0.4.0", "@polkadot/api": "^10.10.1", "@polkadot/extension-inject": "^0.46.5", diff --git a/sdk/packages/aptos/package.json b/sdk/packages/aptos/package.json index e585d901..b64086a4 100644 --- a/sdk/packages/aptos/package.json +++ b/sdk/packages/aptos/package.json @@ -1,6 +1,6 @@ { "name": "@nightlylabs/nightly-connect-aptos", - "version": "0.0.4", + "version": "0.0.5", "type": "module", "exports": { ".": { @@ -45,7 +45,7 @@ "dependencies": { "@aptos-labs/ts-sdk": "^1.9.1", "@aptos-labs/wallet-standard": "^0.0.11", - "@nightlylabs/nightly-connect-base": "^0.0.29", + "@nightlylabs/nightly-connect-base": "^0.0.3", "eventemitter3": "^5.0.1" } } diff --git a/sdk/packages/aptos/src/app.ts b/sdk/packages/aptos/src/app.ts index 332b56e8..e04a620e 100644 --- a/sdk/packages/aptos/src/app.ts +++ b/sdk/packages/aptos/src/app.ts @@ -153,7 +153,11 @@ export class AppAptos extends EventEmitter { } changeNetwork: AptosChangeNetworkMethod = async (input) => { - const changedNetworkSuccess = await this.base.changeNetwork(input) + console.log(input,'wtf') + const changedNetworkSuccess = await this.base.changeNetwork({ + ...input, + id: input.chainId.toString() + }) return { status: UserResponseStatus.APPROVED, args: { diff --git a/sdk/packages/aptos/src/client.ts b/sdk/packages/aptos/src/client.ts index a95b1b1d..10153a99 100644 --- a/sdk/packages/aptos/src/client.ts +++ b/sdk/packages/aptos/src/client.ts @@ -1,4 +1,4 @@ -import { AnyRawTransaction } from '@aptos-labs/ts-sdk' +import { AnyRawTransaction, Network } from '@aptos-labs/ts-sdk' import { AccountInfo, AptosChangeNetworkInput, @@ -90,10 +90,15 @@ export class ClientAptos extends EventEmitter { this.emit('signMessage', event) }) baseClient.on('changeNetwork', (e) => { + const newNetwork: AptosChangeNetworkInput = { + name: e.newNetwork.name! as Network, + url: e.newNetwork.url, + chainId: +e.newNetwork.id + } const event: ChangeNetworkEvent = { sessionId: e.sessionId, requestId: e.responseId, - newNetwork: e.newNetwork + newNetwork: newNetwork } this.emit('changeNetwork', event) }) @@ -162,7 +167,7 @@ export class ClientAptos extends EventEmitter { } await this.baseClient.resolveChangeNetwork({ requestId, - newNetwork, + newNetwork: { ...newNetwork, id: newNetwork.chainId.toString() }, sessionId: sessionIdToUse }) } diff --git a/sdk/packages/aptos/src/http-client.ts b/sdk/packages/aptos/src/http-client.ts index 8c7ebbf0..be484229 100644 --- a/sdk/packages/aptos/src/http-client.ts +++ b/sdk/packages/aptos/src/http-client.ts @@ -77,7 +77,7 @@ export class HttpClientAptos { }: ResolveChangeNetwork) => { await this.baseClient.resolveChangeNetwork({ requestId, - newNetwork, + newNetwork: { ...newNetwork, id: newNetwork.chainId.toString() }, sessionId: sessionId ?? '' }) } diff --git a/sdk/packages/aptos/src/utils.ts b/sdk/packages/aptos/src/utils.ts index 25b97ff5..45db85da 100644 --- a/sdk/packages/aptos/src/utils.ts +++ b/sdk/packages/aptos/src/utils.ts @@ -3,6 +3,7 @@ import { AccountAuthenticator, AnyRawTransaction, Deserializer, + Network, PendingTransactionResponse, RawTransaction, Serializer @@ -11,6 +12,7 @@ import { AccountInfo, NetworkInfo } from '@aptos-labs/wallet-standard' import { ContentType, RequestContent } from '@nightlylabs/nightly-connect-base' import { AptosRequest, + ChangeNetworkAptosRequest, CustomAptosRequest, SignMessagesAptosRequest, SignTransactionsAptosRequest @@ -199,9 +201,13 @@ export const parseRequest = (request: RequestContent, sessionId: string): AptosR return customRequest } case ContentType.ChangeNetwork: { - const changeNetworkRequest: CustomAptosRequest = { - type: ContentType.Custom, - content: JSON.stringify(request.content), + const changeNetworkRequest: ChangeNetworkAptosRequest = { + type: ContentType.ChangeNetwork, + newNetwork: { + chainId: +request.content.newNetwork.id, + name: request.content.newNetwork.name as Network, + url: request.content.newNetwork.url + }, requestId: request.requestId, sessionId: sessionId } diff --git a/sdk/packages/base/package.json b/sdk/packages/base/package.json index dab6ad32..96ed54ee 100644 --- a/sdk/packages/base/package.json +++ b/sdk/packages/base/package.json @@ -1,6 +1,6 @@ { "name": "@nightlylabs/nightly-connect-base", - "version": "0.0.29", + "version": "0.0.3", "type": "module", "exports": { ".": { diff --git a/sdk/packages/base/src/app.ts b/sdk/packages/base/src/app.ts index 03257ff1..4b4027f8 100644 --- a/sdk/packages/base/src/app.ts +++ b/sdk/packages/base/src/app.ts @@ -1,4 +1,3 @@ -import { AptosChangeNetworkInput } from '@aptos-labs/wallet-standard' import { EventEmitter } from 'eventemitter3' import WebSocket from 'isomorphic-ws' import { AppToServer } from '../../../bindings/AppToServer' @@ -9,7 +8,13 @@ import { ServerToApp } from '../../../bindings/ServerToApp' import { UserConnectedEvent } from '../../../bindings/UserConnectedEvent' import { UserDisconnectedEvent } from '../../../bindings/UserDisconnectedEvent' import { WalletMetadata } from '../../../bindings/WalletMetadata' -import { ContentType, MessageToSign, RequestInternal, TransactionToSign } from './content' +import { + ContentType, + MessageToSign, + NetworkToChange, + RequestInternal, + TransactionToSign +} from './content' import { triggerDeeplink } from './deeplinks' import { AppBaseInitialize } from './initializeTypes' import { @@ -228,7 +233,7 @@ export class BaseApp extends EventEmitter { return response.messages } - changeNetwork = async (newNetwork: AptosChangeNetworkInput) => { + changeNetwork = async (newNetwork: NetworkToChange) => { const response = (await this.sendRequest({ type: ContentType.ChangeNetwork, newNetwork: newNetwork diff --git a/sdk/packages/base/src/client.test.ts b/sdk/packages/base/src/client.test.ts index 9e76dbc4..f442bfaf 100644 --- a/sdk/packages/base/src/client.test.ts +++ b/sdk/packages/base/src/client.test.ts @@ -1,10 +1,9 @@ import { Network } from '@aptos-labs/ts-sdk' -import { AptosChangeNetworkInput } from '@aptos-labs/wallet-standard' import { assert, beforeAll, beforeEach, describe, expect, test, vi } from 'vitest' import { smartDelay } from '../../../commonTestUtils' import { BaseApp } from './app' import { BaseClient, Connect } from './client' -import { ContentType, MessageToSign, TransactionToSign } from './content' +import { ContentType, MessageToSign, NetworkToChange, TransactionToSign } from './content' import { SignedMessage, SignedTransaction } from './responseContent' import { testAppBaseInitialize, testClientBaseInitialize } from './testUtils' @@ -81,12 +80,12 @@ describe('Base Client tests', () => { assert(signed.length === 2) }) test('#on("changeNetwork")', async () => { - const randomNewNetwork: AptosChangeNetworkInput = { + const randomNewNetwork: NetworkToChange = { name: Network.MAINNET, - chainId: 27 + id: '27' } client.on('changeNetwork', async (e) => { - assert(e.newNetwork.chainId === 27) + assert(+e.newNetwork.id === 27) // resolve await client.resolveChangeNetwork({ sessionId: e.sessionId, @@ -97,7 +96,7 @@ describe('Base Client tests', () => { await smartDelay() const newNetwork = await baseApp.changeNetwork(randomNewNetwork) - assert(newNetwork.newNetwork.chainId === 27) + assert(+newNetwork.newNetwork.id === 27) }) test('#getPendingRequests', async () => { const randomSignMessage: MessageToSign[] = [{ message: '1' }, { message: '13' }] diff --git a/sdk/packages/base/src/client.ts b/sdk/packages/base/src/client.ts index 8e6a6124..3f402c5a 100644 --- a/sdk/packages/base/src/client.ts +++ b/sdk/packages/base/src/client.ts @@ -1,5 +1,4 @@ // import LocalStorage from 'isomorphic-localstorage' -import { AptosChangeNetworkInput } from '@aptos-labs/wallet-standard' import { EventEmitter } from 'eventemitter3' import WebSocket from 'isomorphic-ws' import { AppDisconnectedEvent } from '../../../bindings/AppDisconnectedEvent' @@ -14,7 +13,13 @@ import { GetPendingRequestsResponse } from '../../../bindings/GetPendingRequests import { GetSessionsRequest } from '../../../bindings/GetSessionsRequest' import { GetSessionsResponse } from '../../../bindings/GetSessionsResponse' import { ServerToClient } from '../../../bindings/ServerToClient' -import { MessageToSign, RequestContent, RequestInternal, TransactionToSign } from './content' +import { + MessageToSign, + NetworkToChange, + RequestContent, + RequestInternal, + TransactionToSign +} from './content' import { ClientBaseInitialize } from './initializeTypes' import { ResponseContent, @@ -37,7 +42,7 @@ export interface SignMessagesEvent { export interface ChangeNetworkEvent { responseId: string sessionId: string - newNetwork: AptosChangeNetworkInput + newNetwork: NetworkToChange } export interface CustomEvent { responseId: string @@ -301,7 +306,7 @@ export interface ResolveSignTransactions { } export interface ResolveChangeNetwork { requestId: string - newNetwork: AptosChangeNetworkInput + newNetwork: NetworkToChange sessionId: string } diff --git a/sdk/packages/base/src/content.ts b/sdk/packages/base/src/content.ts index cd7a7407..5da4dba5 100644 --- a/sdk/packages/base/src/content.ts +++ b/sdk/packages/base/src/content.ts @@ -1,5 +1,3 @@ -import { AptosChangeNetworkInput } from '@aptos-labs/wallet-standard' - export enum ContentType { SignMessages = 'SignMessages', SignTransactions = 'SignTransactions', @@ -11,6 +9,11 @@ export interface MessageToSign { message: string metadata?: string } +export interface NetworkToChange { + name?: string + id: string + url?: string +} export interface SignMessagesContent { type: ContentType.SignMessages messages: MessageToSign[] @@ -22,7 +25,7 @@ export interface TransactionToSign { export interface ChangeNetworkContent { type: ContentType.ChangeNetwork - newNetwork: AptosChangeNetworkInput + newNetwork: NetworkToChange } export interface SignTransactionsContent { type: ContentType.SignTransactions diff --git a/sdk/packages/base/src/http-client.test.ts b/sdk/packages/base/src/http-client.test.ts index 2b63b98c..7f12b95f 100644 --- a/sdk/packages/base/src/http-client.test.ts +++ b/sdk/packages/base/src/http-client.test.ts @@ -1,10 +1,9 @@ import { Network } from '@aptos-labs/ts-sdk' -import { AptosChangeNetworkInput } from '@aptos-labs/wallet-standard' import { assert, beforeAll, beforeEach, describe, expect, test, vi } from 'vitest' import { smartDelay, TEST_RELAY_ENDPOINT } from '../../../commonTestUtils' import { BaseApp } from './app' import { Connect } from './client' -import { MessageToSign, TransactionToSign } from './content' +import { MessageToSign, NetworkToChange, TransactionToSign } from './content' import { HttpBaseClient } from './http-client' import { SignedMessage, SignedTransaction } from './responseContent' import { testAppBaseInitialize } from './testUtils' @@ -94,9 +93,9 @@ describe('Http Base Client tests', () => { assert(signed.length === 2) }) test('#resolveChangeNetwork()', async () => { - const randomNewNetwork: AptosChangeNetworkInput = { + const randomNewNetwork: NetworkToChange = { name: Network.MAINNET, - chainId: 27 + id: '27' } // send change network const newNetworkResponse = baseApp.changeNetwork(randomNewNetwork) @@ -111,7 +110,7 @@ describe('Http Base Client tests', () => { await smartDelay() const newNetwork = await newNetworkResponse - assert(newNetwork.newNetwork.chainId === 27) + assert(+newNetwork.newNetwork.id === 27) }) test('#reject()', async () => { try { diff --git a/sdk/packages/base/src/http-client.ts b/sdk/packages/base/src/http-client.ts index 8d87ac19..3d3a96db 100644 --- a/sdk/packages/base/src/http-client.ts +++ b/sdk/packages/base/src/http-client.ts @@ -1,5 +1,4 @@ // import LocalStorage from 'isomorphic-localstorage' -import { AptosChangeNetworkInput } from '@aptos-labs/wallet-standard' import { fetch } from 'cross-fetch' import { HttpConnectSessionRequest } from '../../../bindings/HttpConnectSessionRequest' import { HttpConnectSessionResponse } from '../../../bindings/HttpConnectSessionResponse' @@ -16,7 +15,7 @@ import { HttpGetSessionsRequest } from '../../../bindings/HttpGetSessionsRequest import { HttpGetSessionsResponse } from '../../../bindings/HttpGetSessionsResponse' import { HttpResolveRequestRequest } from '../../../bindings/HttpResolveRequestRequest' import { HttpResolveRequestResponse } from '../../../bindings/HttpResolveRequestResponse' -import { RequestContent } from './content' +import { NetworkToChange, RequestContent } from './content' import { CustomResponseContent, RejectResponseContent, @@ -233,7 +232,7 @@ export interface HttpResolveSignMessages { } export interface HttpResolveChangeNetwork { requestId: string - newNetwork: AptosChangeNetworkInput + newNetwork: NetworkToChange sessionId: string clientId?: string } diff --git a/sdk/packages/base/src/responseContent.ts b/sdk/packages/base/src/responseContent.ts index 066eb55a..07a4aa66 100644 --- a/sdk/packages/base/src/responseContent.ts +++ b/sdk/packages/base/src/responseContent.ts @@ -1,4 +1,4 @@ -import { AptosChangeNetworkInput } from '@aptos-labs/wallet-standard' +import { NetworkToChange } from './content' export enum ResponseContentType { SignMessages = 'SignMessagesResponse', @@ -26,7 +26,7 @@ export interface SignTransactionsResponseContent { } export interface ChangeNetworkResponseContent { type: ResponseContentType.ChangedNetwork - newNetwork: AptosChangeNetworkInput + newNetwork: NetworkToChange } export interface RejectResponseContent { type: ResponseContentType.Reject diff --git a/sdk/packages/selector-aptos/package.json b/sdk/packages/selector-aptos/package.json index 476ba84b..0e9e17f7 100644 --- a/sdk/packages/selector-aptos/package.json +++ b/sdk/packages/selector-aptos/package.json @@ -26,9 +26,9 @@ "dependencies": { "@aptos-labs/ts-sdk": "^1.9.1", "@aptos-labs/wallet-standard": "^0.0.11", - "@nightlylabs/nightly-connect-aptos": "0.0.4", + "@nightlylabs/nightly-connect-aptos": "0.0.5", "@nightlylabs/wallet-selector-base": "0.4.2", - "@nightlylabs/nightly-connect-base": "^0.0.29", + "@nightlylabs/nightly-connect-base": "^0.0.3", "@wallet-standard/core": "^1.0.3", "eventemitter3": "^5.0.1" }, diff --git a/sdk/packages/selector-base/package.json b/sdk/packages/selector-base/package.json index 58f06bae..ea54faf7 100644 --- a/sdk/packages/selector-base/package.json +++ b/sdk/packages/selector-base/package.json @@ -24,7 +24,7 @@ "author": "", "license": "ISC", "dependencies": { - "@nightlylabs/nightly-connect-base": "0.0.29", + "@nightlylabs/nightly-connect-base": "^0.0.3", "@nightlylabs/wallet-selector-modal": "0.2.1", "@wallet-standard/core": "^1.0.3", "isomorphic-localstorage": "^1.0.2" diff --git a/sdk/packages/selector-solana/package.json b/sdk/packages/selector-solana/package.json index c775e42e..efbe576f 100644 --- a/sdk/packages/selector-solana/package.json +++ b/sdk/packages/selector-solana/package.json @@ -1,6 +1,6 @@ { "name": "@nightlylabs/wallet-selector-solana", - "version": "0.3.7", + "version": "0.3.8", "description": "", "type": "module", "exports": { @@ -24,8 +24,8 @@ "author": "", "license": "ISC", "dependencies": { - "@nightlylabs/nightly-connect-solana": "^0.0.29", - "@nightlylabs/wallet-selector-base": "^0.4.1", + "@nightlylabs/nightly-connect-solana": "^0.0.3", + "@nightlylabs/wallet-selector-base": "^0.4.2", "@solana/wallet-adapter-base": "^0.9.22", "@solana/wallet-standard": "^1.0.2", "@solana/web3.js": "^1.77.2", diff --git a/sdk/packages/selector-solana/src/adapter.ts b/sdk/packages/selector-solana/src/adapter.ts index 3807c9f6..83d9318f 100644 --- a/sdk/packages/selector-solana/src/adapter.ts +++ b/sdk/packages/selector-solana/src/adapter.ts @@ -900,11 +900,10 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { } // Check if remote connection is established if (this._connectionType === ConnectionType.Nightly) { - // TODO: add support on mobile - throw new Error('Not supported on mobile yet') + await this._app!.changeNetwork({ genesisHash, url }) } - // @ts-ignore + // @ts-expect-error Window not declared const nightlySolana = window.nightly?.solana // check if we are connected with nightly wallet @@ -912,7 +911,6 @@ export class NightlyConnectAdapter extends BaseMessageSignerWalletAdapter { throw new Error('Only supported on Nightly wallet') } - // @ts-ignore await nightlySolana.changeNetwork({ genesisHash, url diff --git a/sdk/packages/solana/package.json b/sdk/packages/solana/package.json index 5ffd5cdf..1e1d5e90 100644 --- a/sdk/packages/solana/package.json +++ b/sdk/packages/solana/package.json @@ -1,6 +1,6 @@ { "name": "@nightlylabs/nightly-connect-solana", - "version": "0.0.29", + "version": "0.0.3", "type": "module", "exports": { ".": { @@ -45,7 +45,7 @@ "typescript": "^5.1.3" }, "dependencies": { - "@nightlylabs/nightly-connect-base": "0.0.28", + "@nightlylabs/nightly-connect-base": "^0.0.3", "@solana/web3.js": "^1.77.2", "eventemitter3": "^5.0.1", "uuid": "^9.0.0" diff --git a/sdk/packages/solana/src/app.ts b/sdk/packages/solana/src/app.ts index 0bf34162..e2a76b84 100644 --- a/sdk/packages/solana/src/app.ts +++ b/sdk/packages/solana/src/app.ts @@ -1,16 +1,17 @@ -import { Keypair, PublicKey, Transaction, VersionedTransaction } from '@solana/web3.js' import { BaseApp, + DeeplinkConnect, getWalletsMetadata, MessageToSign, - TransactionToSign, - DeeplinkConnect + TransactionToSign } from '@nightlylabs/nightly-connect-base' -import { AppSolanaInitialize, SOLANA_NETWORK } from './utils' +import { Keypair, PublicKey, Transaction, VersionedTransaction } from '@solana/web3.js' import { EventEmitter } from 'eventemitter3' -import { UserDisconnectedEvent } from '../../../bindings/UserDisconnectedEvent' import { UserConnectedEvent } from '../../../bindings/UserConnectedEvent' +import { UserDisconnectedEvent } from '../../../bindings/UserDisconnectedEvent' import { WalletMetadata } from '../../../bindings/WalletMetadata' +import { SolanaChangeNetworkInput } from './client' +import { AppSolanaInitialize, SOLANA_NETWORK } from './utils' interface SolanaAppEvents { userConnected: (e: UserConnectedEvent) => void @@ -142,4 +143,15 @@ export class AppSolana extends EventEmitter { const signedTx = await this.base.signMessages([request]) return Uint8Array.from(Buffer.from(signedTx[0].message, 'hex')) } + + changeNetwork = async (input: SolanaChangeNetworkInput) => { + console.log(input, 'lol') + const changeNetworkResponse = await this.base.changeNetwork({ + url: input.genesisHash, + id: input.genesisHash + }) + return { + success: !!changeNetworkResponse.newNetwork + } + } } diff --git a/sdk/packages/solana/src/client.ts b/sdk/packages/solana/src/client.ts index 9bc0c019..5f3fc558 100644 --- a/sdk/packages/solana/src/client.ts +++ b/sdk/packages/solana/src/client.ts @@ -1,16 +1,15 @@ -import { AppDisconnectedEvent } from '../../../bindings/AppDisconnectedEvent' -import { VersionedTransaction } from '@solana/web3.js' import { BaseClient, ClientBaseInitialize, Connect, SignMessagesEvent } from '@nightlylabs/nightly-connect-base' +import { VersionedTransaction } from '@solana/web3.js' import { EventEmitter } from 'eventemitter3' -import { SOLANA_NETWORK } from './utils' +import { AppDisconnectedEvent } from '../../../bindings/AppDisconnectedEvent' import { GetInfoResponse } from '../../../bindings/GetInfoResponse' -import { parseRequest } from './utils' import { SolanaRequest } from './requestTypes' +import { parseRequest, SOLANA_NETWORK } from './utils' export interface SignSolanaTransactionEvent { requestId: string @@ -18,7 +17,14 @@ export interface SignSolanaTransactionEvent { sessionId: string } export type SignSolanaMessageEvent = SignMessagesEvent + +export interface ChangeSolanaNetworkEvent { + requestId: string + sessionId: string + newNetwork: SolanaChangeNetworkInput +} export interface ClientSolanaEvents { + changeNetwork(e: ChangeSolanaNetworkEvent) signTransactions: (e: SignSolanaTransactionEvent) => void signMessages: (e: SignSolanaMessageEvent) => void appDisconnected: (e: AppDisconnectedEvent) => void @@ -41,6 +47,18 @@ export class ClientSolana extends EventEmitter { baseClient.on('signMessages', (e) => { this.emit('signMessages', e) }) + baseClient.on('changeNetwork', (e) => { + const newNetwork: SolanaChangeNetworkInput = { + url: e.newNetwork.url, + genesisHash: e.newNetwork.id + } + const event: ChangeSolanaNetworkEvent = { + sessionId: e.sessionId, + requestId: e.responseId, + newNetwork: newNetwork + } + this.emit('changeNetwork', event) + }) baseClient.on('appDisconnected', (e) => { this.emit('appDisconnected', e) }) @@ -110,6 +128,21 @@ export class ClientSolana extends EventEmitter { sessionId: sessionIdToUse }) } + public resolveChangeNetwork = async ({ + requestId, + newNetwork, + sessionId + }: ResolveChangeSolanaNetwork) => { + const sessionIdToUse = sessionId || this.sessionId + if (sessionIdToUse === undefined) { + throw new Error('Session id is undefined') + } + await this.baseClient.resolveChangeNetwork({ + requestId, + newNetwork: { ...newNetwork, id: newNetwork.genesisHash }, + sessionId: sessionIdToUse + }) + } public resolveSignMessage = async ({ requestId, signature, @@ -142,6 +175,11 @@ export class ClientSolana extends EventEmitter { } } +export interface SolanaChangeNetworkInput { + genesisHash: string + url?: string +} + export interface RejectRequest { requestId: string reason?: string @@ -157,3 +195,9 @@ export interface ResolveSignSolanaMessage { signature: Uint8Array sessionId?: string } + +export interface ResolveChangeSolanaNetwork { + requestId: string + newNetwork: SolanaChangeNetworkInput + sessionId?: string +} diff --git a/sdk/packages/solana/src/e2e.test.ts b/sdk/packages/solana/src/e2e.test.ts index 1e4b0686..2572645a 100644 --- a/sdk/packages/solana/src/e2e.test.ts +++ b/sdk/packages/solana/src/e2e.test.ts @@ -1,14 +1,14 @@ -import { Keypair, LAMPORTS_PER_SOL, SystemProgram, Transaction } from '@solana/web3.js' import { Connect, ContentType } from '@nightlylabs/nightly-connect-base' +import { Keypair, LAMPORTS_PER_SOL, SystemProgram, Transaction } from '@solana/web3.js' import { sha256 } from 'js-sha256' import nacl from 'tweetnacl' import { assert, beforeAll, beforeEach, describe, expect, test } from 'vitest' +import { smartDelay, TEST_RELAY_ENDPOINT } from '../../../commonTestUtils' import { AppSolana } from './app' -import { ClientSolana } from './client' -import { SOLANA_NETWORK } from './utils' -import { TEST_APP_INITIALIZE } from './testUtils' +import { ClientSolana, SolanaChangeNetworkInput } from './client' import { SignTransactionsSolanaRequest } from './requestTypes' -import { smartDelay, TEST_RELAY_ENDPOINT } from '../../../commonTestUtils' +import { TEST_APP_INITIALIZE } from './testUtils' +import { SOLANA_NETWORK } from './utils' // Edit an assertion and save to see HMR in action const alice_keypair = Keypair.generate() @@ -89,6 +89,21 @@ describe('Base Client tests', () => { ) assert(verified) }) + test('#on("changeNetwork")', async () => { + client.on('changeNetwork', async (e) => { + const payload = e.newNetwork + await client.resolveChangeNetwork({ + requestId: e.requestId, + newNetwork: payload + }) + }) + + await smartDelay() + const newNetwork: SolanaChangeNetworkInput = { + genesisHash: 'abcdefgh' + } + const _changedNetwork = await app.changeNetwork(newNetwork) + }) test('#getPendingRequests()', async () => { client.removeListener('signTransactions') const RECEIVER = Keypair.generate() diff --git a/sdk/packages/solana/src/http-client.ts b/sdk/packages/solana/src/http-client.ts index 68d1b48e..574eb5be 100644 --- a/sdk/packages/solana/src/http-client.ts +++ b/sdk/packages/solana/src/http-client.ts @@ -1,10 +1,11 @@ -import { VersionedTransaction } from '@solana/web3.js' import { HttpBaseClient, HttpBaseClientInitialize } from '@nightlylabs/nightly-connect-base' -import { SOLANA_NETWORK, parseRequest } from './utils' +import { VersionedTransaction } from '@solana/web3.js' import { HttpConnectSessionRequest } from '../../../bindings/HttpConnectSessionRequest' -import { HttpGetPendingRequestsRequest } from '../../../bindings/HttpGetPendingRequestsRequest' import { HttpGetPendingRequestRequest } from '../../../bindings/HttpGetPendingRequestRequest' +import { HttpGetPendingRequestsRequest } from '../../../bindings/HttpGetPendingRequestsRequest' import { HttpGetSessionInfoResponse } from '../../../bindings/HttpGetSessionInfoResponse' +import { SolanaChangeNetworkInput } from './client' +import { SOLANA_NETWORK, parseRequest } from './utils' export class HttpClientSolana { baseClient: HttpBaseClient @@ -56,6 +57,17 @@ export class HttpClientSolana { signedMessages: [{ message: Buffer.from(signature).toString('hex') }] }) } + public resolveChangeNetwork = async ({ + requestId, + newNetwork, + sessionId + }: ResolveChangeSolanaNetwork) => { + await this.baseClient.resolveChangeNetwork({ + requestId, + newNetwork: { ...newNetwork, id: newNetwork.genesisHash }, + sessionId: sessionId ?? '' + }) + } public rejectRequest = async ({ requestId, reason, sessionId }: RejectRequest) => { await this.baseClient.reject({ requestId: requestId, reason, sessionId: sessionId }) } @@ -75,3 +87,9 @@ export interface ResolveSignSolanaMessage { signature: Uint8Array sessionId: string } + +export interface ResolveChangeSolanaNetwork { + requestId: string + newNetwork: SolanaChangeNetworkInput + sessionId?: string +} diff --git a/sdk/packages/solana/src/http-e2e.test.ts b/sdk/packages/solana/src/http-e2e.test.ts index 3df26799..794fd4d3 100644 --- a/sdk/packages/solana/src/http-e2e.test.ts +++ b/sdk/packages/solana/src/http-e2e.test.ts @@ -1,11 +1,13 @@ +import { Connect, ContentType, getRandomId } from '@nightlylabs/nightly-connect-base' +import { Keypair, LAMPORTS_PER_SOL, SystemProgram, Transaction } from '@solana/web3.js' import { assert, beforeAll, beforeEach, describe, expect, test } from 'vitest' +import { smartDelay, TEST_RELAY_ENDPOINT } from '../../../commonTestUtils' import { AppSolana } from './app' -import { SOLANA_NETWORK } from './utils' -import { TEST_APP_INITIALIZE } from './testUtils' -import { Connect, getRandomId, ContentType } from '@nightlylabs/nightly-connect-base' -import { Keypair, LAMPORTS_PER_SOL, SystemProgram, Transaction } from '@solana/web3.js' +import { SolanaChangeNetworkInput } from './client' import { HttpClientSolana } from './http-client' -import { smartDelay, TEST_RELAY_ENDPOINT } from '../../../commonTestUtils' +import { ChangeNetworkSolanaRequest } from './requestTypes' +import { TEST_APP_INITIALIZE } from './testUtils' +import { SOLANA_NETWORK } from './utils' // Edit an assertion and save to see HMR in action const alice_keypair = Keypair.generate() @@ -70,4 +72,26 @@ describe('Base Client tests', () => { const signed_transaction = Transaction.from(signed.serialize()) assert(signed_transaction.verifySignatures()) }) + test('#resolveChangeNetwork()', async () => { + const newNetwork: SolanaChangeNetworkInput = { + genesisHash: 'abcdefgh' + } + + const _changedNetwork = app.changeNetwork(newNetwork) + await smartDelay() + + const pendingRequest = ( + await client.getPendingRequests({ sessionId: app.sessionId }) + )[0] as ChangeNetworkSolanaRequest + expect(pendingRequest.type).toBe(ContentType.ChangeNetwork) + expect(pendingRequest.newNetwork.genesisHash).toBe('abcdefgh') + + const payload = pendingRequest.newNetwork + + await client.resolveChangeNetwork({ + requestId: pendingRequest.requestId, + sessionId: app.sessionId, + newNetwork: payload + }) + }) }) diff --git a/sdk/packages/solana/src/requestTypes.ts b/sdk/packages/solana/src/requestTypes.ts index 13bda6b3..698398ac 100644 --- a/sdk/packages/solana/src/requestTypes.ts +++ b/sdk/packages/solana/src/requestTypes.ts @@ -1,5 +1,6 @@ import { ContentType, MessageToSign } from '@nightlylabs/nightly-connect-base' import { VersionedTransaction } from '@solana/web3.js' +import { SolanaChangeNetworkInput } from './client' export interface SignTransactionsSolanaRequest { type: ContentType.SignTransactions @@ -19,7 +20,14 @@ export interface CustomSolanaRequest { content?: string sessionId: string } +export interface ChangeNetworkSolanaRequest { + type: ContentType.ChangeNetwork + requestId: string + newNetwork: SolanaChangeNetworkInput + sessionId: string +} export type SolanaRequest = | SignTransactionsSolanaRequest | SignMessagesSolanaRequest | CustomSolanaRequest + | ChangeNetworkSolanaRequest diff --git a/sdk/packages/solana/src/utils.ts b/sdk/packages/solana/src/utils.ts index 8b428a25..64a5f0fb 100644 --- a/sdk/packages/solana/src/utils.ts +++ b/sdk/packages/solana/src/utils.ts @@ -1,11 +1,12 @@ import { AppBaseInitialize, ContentType, RequestContent } from '@nightlylabs/nightly-connect-base' +import { VersionedTransaction } from '@solana/web3.js' import { + ChangeNetworkSolanaRequest, CustomSolanaRequest, SignMessagesSolanaRequest, SignTransactionsSolanaRequest, SolanaRequest } from './requestTypes' -import { VersionedTransaction } from '@solana/web3.js' export type AppSolanaInitialize = Omit @@ -42,5 +43,14 @@ export const parseRequest = (request: RequestContent, sessionId: string): Solana } return customRequest } + case ContentType.ChangeNetwork: { + const changeNetworkRequest: ChangeNetworkSolanaRequest = { + type: ContentType.ChangeNetwork, + newNetwork: { genesisHash: request.content.newNetwork.id, ...request.content.newNetwork }, + requestId: request.requestId, + sessionId: sessionId + } + return changeNetworkRequest + } } } diff --git a/sdk/pnpm-lock.yaml b/sdk/pnpm-lock.yaml index 8e00cbe8..f912698a 100644 --- a/sdk/pnpm-lock.yaml +++ b/sdk/pnpm-lock.yaml @@ -86,7 +86,7 @@ importers: specifier: 0.0.16 version: link:../../packages/polkadot '@nightlylabs/nightly-connect-solana': - specifier: 0.0.29 + specifier: 0.0.3 version: link:../../packages/solana '@nightlylabs/nightly-connect-sui': specifier: 0.1.0 @@ -101,7 +101,7 @@ importers: specifier: 0.2.7 version: link:../../packages/selector-polkadot '@nightlylabs/wallet-selector-solana': - specifier: 0.3.7 + specifier: 0.3.8 version: link:../../packages/selector-solana '@nightlylabs/wallet-selector-sui': specifier: 0.4.0 @@ -235,7 +235,7 @@ importers: specifier: ^0.0.11 version: 0.0.11 '@nightlylabs/nightly-connect-base': - specifier: ^0.0.29 + specifier: ^0.0.3 version: link:../base eventemitter3: specifier: ^5.0.1 @@ -543,10 +543,10 @@ importers: specifier: ^0.0.11 version: 0.0.11 '@nightlylabs/nightly-connect-aptos': - specifier: 0.0.4 + specifier: 0.0.5 version: link:../aptos '@nightlylabs/nightly-connect-base': - specifier: ^0.0.29 + specifier: ^0.0.3 version: link:../base '@nightlylabs/wallet-selector-base': specifier: 0.4.2 @@ -586,7 +586,7 @@ importers: packages/selector-base: dependencies: '@nightlylabs/nightly-connect-base': - specifier: 0.0.29 + specifier: ^0.0.3 version: link:../base '@nightlylabs/wallet-selector-modal': specifier: 0.2.1 @@ -672,10 +672,10 @@ importers: packages/selector-solana: dependencies: '@nightlylabs/nightly-connect-solana': - specifier: ^0.0.29 + specifier: ^0.0.3 version: link:../solana '@nightlylabs/wallet-selector-base': - specifier: ^0.4.1 + specifier: ^0.4.2 version: link:../selector-base '@solana/wallet-adapter-base': specifier: ^0.9.22 @@ -776,8 +776,8 @@ importers: packages/solana: dependencies: '@nightlylabs/nightly-connect-base': - specifier: 0.0.28 - version: 0.0.28 + specifier: ^0.0.3 + version: link:../base '@solana/web3.js': specifier: ^1.77.2 version: 1.95.3