Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
LukassF committed Sep 17, 2024
1 parent 84ef3c6 commit fb0d430
Show file tree
Hide file tree
Showing 19 changed files with 259 additions and 76 deletions.
2 changes: 1 addition & 1 deletion sdk/apps/modal-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@nightlylabs/nightly-connect-polkadot": "0.0.16",
"@nightlylabs/nightly-connect-solana": "0.0.29",
"@nightlylabs/nightly-connect-sui": "0.1.0",
"@nightlylabs/wallet-selector-aptos": "0.1.7",
"@nightlylabs/wallet-selector-aptos": "0.1.8",
"@nightlylabs/wallet-selector-base": "^0.4.1",
"@nightlylabs/wallet-selector-polkadot": "0.2.7",
"@nightlylabs/wallet-selector-solana": "0.3.6",
Expand Down
39 changes: 39 additions & 0 deletions sdk/apps/modal-example/src/routes/aptos.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export default function AptosPage() {
const [adapter, setAdapter] = createSignal<NightlyConnectAptosAdapter>()
const [eager, setEager] = createSignal(false)
const [accountInfo, setAccountInfo] = createSignal<AccountInfo>()
const [currentNetwork, setCurrentNetwork] = createSignal<string>()

onMount(async () => {
NightlyConnectAptosAdapter.build(
{
Expand Down Expand Up @@ -203,6 +205,43 @@ export default function AptosPage() {
}}>
Sign message
</button>
<button
onClick={async () => {
try {
const network = await adapter()!.network()

let changeNetworkResponse
if (network.chainId === 27) {
// Movement network is active
changeNetworkResponse = await adapter()!.changeNetwork({
chainId: 1,
name: Network.MAINNET,
url: 'https://fullnode.mainnet.aptoslabs.com/v1'
})
} else if ([1, 2, 147].includes(network.chainId)) {
// Aptos network is active (mainnet, devnet or testnet)
changeNetworkResponse = await adapter()!.changeNetwork({
chainId: 27,
name: Network.CUSTOM,
url: 'https://aptos.testnet.suzuka.movementlabs.xyz/v1'
})
}

if (
changeNetworkResponse &&
changeNetworkResponse.status === UserResponseStatus.APPROVED
) {
const changedNetwork = await adapter()!.network()
toast.success(`Changed network to ${currentNetwork}!`)
setCurrentNetwork(changedNetwork.chainId === 27 ? 'Aptos' : 'Movement')
}
} catch (error) {
toast.error("Couldn't change network")
console.log(error)
}
}}>
Change to {currentNetwork()}
</button>
<button
onClick={() => {
adapter()?.disconnect()
Expand Down
5 changes: 1 addition & 4 deletions sdk/packages/aptos/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,7 @@ export class AppAptos extends EventEmitter<AptosAppEvents> {
}

changeNetwork: AptosChangeNetworkMethod = async (input) => {
const changedNetworkSuccess = await this.base.changeNetwork({
...input,
name: input.name as string
})
const changedNetworkSuccess = await this.base.changeNetwork(input)
return {
status: UserResponseStatus.APPROVED,
args: {
Expand Down
17 changes: 16 additions & 1 deletion sdk/packages/aptos/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AnyRawTransaction } from '@aptos-labs/ts-sdk'
import {
AccountInfo,
AptosChangeNetworkInput,
AptosSignAndSubmitTransactionOutput,
AptosSignMessageInput,
AptosSignMessageOutput,
Expand Down Expand Up @@ -40,7 +41,13 @@ export interface SignMessagesEvent {
sessionId: string
messages: Array<AptosSignMessageInput>
}
interface ChangeNetworkEvent {
requestId: string
sessionId: string
newNetwork: AptosChangeNetworkInput
}
export interface ClientAptosEvents {
changeNetwork: (e: ChangeNetworkEvent) => void
signAndSubmitTransaction: (e: SignAndSubmitTransactionEvent) => void
signTransaction: (e: SignTransactionEvent) => void
signMessage: (e: SignMessagesEvent) => void
Expand Down Expand Up @@ -82,6 +89,14 @@ export class ClientAptos extends EventEmitter<ClientAptosEvents> {
}
this.emit('signMessage', event)
})
baseClient.on('changeNetwork', (e) => {
const event: ChangeNetworkEvent = {
sessionId: e.sessionId,
requestId: e.responseId,
newNetwork: e.newNetwork
}
this.emit('changeNetwork', event)
})
baseClient.on('appDisconnected', (e) => {
this.emit('appDisconnected', e)
})
Expand Down Expand Up @@ -254,6 +269,6 @@ export interface ResolveSignAptosMessage {
}
export interface ResolveChangeNetwork {
requestId: string
newNetwork: NetworkInfo
newNetwork: AptosChangeNetworkInput
sessionId?: string
}
33 changes: 21 additions & 12 deletions sdk/packages/aptos/src/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { APTOS_NETWORK } from './utils'
import { Account, Aptos, Ed25519PrivateKey, Network } from '@aptos-labs/ts-sdk'
import {
AccountInfo,
AptosChangeNetworkInput,
AptosSignMessageInput,
NetworkInfo,
UserResponseStatus
Expand Down Expand Up @@ -99,9 +100,7 @@ describe('Aptos client tests', () => {
}
})

const signedTx = await app.signTransaction({
rawTransaction: transaction.rawTransaction
})
const signedTx = await app.signTransaction(transaction)
// Verify the transaction was signed
if (signedTx.status !== UserResponseStatus.APPROVED) {
throw new Error('Transaction was not approved')
Expand Down Expand Up @@ -140,6 +139,22 @@ describe('Aptos client tests', () => {
await smartDelay()
const _signedMessage = await app.signMessage(msgToSign)
})
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: AptosChangeNetworkInput = {
name: Network.MAINNET,
chainId: 27
}
const _changedNetwork = await app.changeNetwork(newNetwork)
})
test('#on("signAndSubmitTransaction")', async () => {
const bobAddress = '0xb0b'

Expand Down Expand Up @@ -172,9 +187,7 @@ describe('Aptos client tests', () => {
}
})

const submittedTx = await app.signAndSubmitTransaction({
rawTransaction: transaction.rawTransaction
})
const submittedTx = await app.signAndSubmitTransaction(transaction)
// Verify the transaction was signed
if (submittedTx.status !== UserResponseStatus.APPROVED) {
throw new Error('Transaction was not approved')
Expand All @@ -193,12 +206,8 @@ describe('Aptos client tests', () => {
functionArguments: [bobAddress, 100]
}
})
app.signAndSubmitTransaction({
rawTransaction: transaction.rawTransaction
})
app.signAndSubmitTransaction({
rawTransaction: transaction.rawTransaction
})
app.signAndSubmitTransaction(transaction)
app.signAndSubmitTransaction(transaction)
await smartDelay(500)
const requests = await client.getPendingRequests()
expect(requests.length).toBe(2)
Expand Down
39 changes: 32 additions & 7 deletions sdk/packages/aptos/src/http-client.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Account, Aptos, Ed25519PrivateKey, Network } from '@aptos-labs/ts-sdk'
import {
AccountInfo,
AptosChangeNetworkInput,
AptosSignMessageInput,
NetworkInfo,
UserResponseStatus
Expand All @@ -10,7 +11,11 @@ import { assert, beforeAll, beforeEach, describe, expect, test, vi } from 'vites
import { TEST_RELAY_ENDPOINT, smartDelay } from '../../../commonTestUtils'
import { AppAptos } from './app'
import { HttpClientAptos } from './http-client'
import { SignMessagesAptosRequest, SignTransactionsAptosRequest } from './requestTypes'
import {
ChangeNetworkAptosRequest,
SignMessagesAptosRequest,
SignTransactionsAptosRequest
} from './requestTypes'
import { TEST_APP_INITIALIZE } from './testUtils'
import { APTOS_NETWORK, serializeConnectData } from './utils'
// Edit an assertion and save to see HMR in action
Expand Down Expand Up @@ -78,9 +83,7 @@ describe('Aptos http-client tests', () => {
functionArguments: [bobAddress, 100]
}
})
const promiseSignTransaction = app.signTransaction({
rawTransaction: transaction.rawTransaction
})
const promiseSignTransaction = app.signTransaction(transaction)
await smartDelay()
// Query for request
const pendingRequest = (
Expand Down Expand Up @@ -123,9 +126,7 @@ describe('Aptos http-client tests', () => {
functionArguments: [bobAddress, 100]
}
})
const promiseSignTransaction = app.signAndSubmitTransaction({
rawTransaction: transaction.rawTransaction
})
const promiseSignTransaction = app.signAndSubmitTransaction(transaction)
await smartDelay()
// Query for request
const pendingRequest = (
Expand Down Expand Up @@ -190,4 +191,28 @@ describe('Aptos http-client tests', () => {
throw new Error('Transaction was not approved')
}
})
test('#resolveChangeNetwork()', async () => {
const newNetwork: AptosChangeNetworkInput = {
name: Network.MAINNET,
chainId: 27
}

const _changedNetwork = app.changeNetwork(newNetwork)
await smartDelay()

const pendingRequest = (
await client.getPendingRequests({ sessionId: app.sessionId })
)[0] as ChangeNetworkAptosRequest
expect(pendingRequest.type).toBe(ContentType.ChangeNetwork)
expect(pendingRequest.newNetwork.chainId).toBe(27)
expect(pendingRequest.newNetwork.name).toBe(Network.MAINNET)

const payload = pendingRequest.newNetwork

await client.resolveChangeNetwork({
requestId: pendingRequest.requestId,
sessionId: app.sessionId,
newNetwork: payload
})
})
})
17 changes: 17 additions & 0 deletions sdk/packages/aptos/src/http-client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
AptosChangeNetworkInput,
AptosSignAndSubmitTransactionOutput,
AptosSignMessageOutput,
AptosSignTransactionOutput
Expand Down Expand Up @@ -69,6 +70,17 @@ export class HttpClientAptos {
sessionId: sessionId
})
}
public resolveChangeNetwork = async ({
requestId,
newNetwork,
sessionId
}: ResolveChangeNetwork) => {
await this.baseClient.resolveChangeNetwork({
requestId,
newNetwork,
sessionId: sessionId ?? ''
})
}
public resolveSignMessage = async ({
requestId,
signedMessages,
Expand Down Expand Up @@ -110,3 +122,8 @@ export interface ResolveSignAndSubmitTransactions {
signedTransactions: Array<AptosSignAndSubmitTransactionOutput>
sessionId: string
}
export interface ResolveChangeNetwork {
requestId: string
newNetwork: AptosChangeNetworkInput
sessionId?: string
}
9 changes: 8 additions & 1 deletion sdk/packages/aptos/src/requestTypes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AnyRawTransaction } from '@aptos-labs/ts-sdk'
import { AptosSignMessageInput } from '@aptos-labs/wallet-standard'
import { AptosChangeNetworkInput, AptosSignMessageInput } from '@aptos-labs/wallet-standard'
import { ContentType } from '@nightlylabs/nightly-connect-base'

export interface SignTransactionsAptosRequest {
Expand All @@ -15,6 +15,12 @@ export interface SignMessagesAptosRequest {
messages: Array<AptosSignMessageInput>
sessionId: string
}
export interface ChangeNetworkAptosRequest {
type: ContentType.ChangeNetwork
requestId: string
newNetwork: AptosChangeNetworkInput
sessionId: string
}
export interface CustomAptosRequest {
type: ContentType.Custom
requestId: string
Expand All @@ -24,4 +30,5 @@ export interface CustomAptosRequest {
export type AptosRequest =
| SignTransactionsAptosRequest
| SignMessagesAptosRequest
| ChangeNetworkAptosRequest
| CustomAptosRequest
2 changes: 1 addition & 1 deletion sdk/packages/aptos/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export const parseRequest = (request: RequestContent, sessionId: string): AptosR
case ContentType.ChangeNetwork: {
const changeNetworkRequest: CustomAptosRequest = {
type: ContentType.Custom,
content: request.content.newNetwork.chainId.toString(),
content: JSON.stringify(request.content),
requestId: request.requestId,
sessionId: sessionId
}
Expand Down
2 changes: 2 additions & 0 deletions sdk/packages/base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
"cross-fetch": "^3.1.6",
"eventemitter3": "^5.0.1",
"isomorphic-localstorage": "^1.0.2",
"@aptos-labs/wallet-standard": "^0.0.11",
"@aptos-labs/ts-sdk": "^1.9.1",
"isomorphic-ws": "^5.0.0",
"uuid": "^9.0.0",
"ws": "^8.13.0"
Expand Down
11 changes: 3 additions & 8 deletions sdk/packages/base/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AptosChangeNetworkInput } from '@aptos-labs/wallet-standard'
import { EventEmitter } from 'eventemitter3'
import WebSocket from 'isomorphic-ws'
import { AppToServer } from '../../../bindings/AppToServer'
Expand All @@ -8,13 +9,7 @@ import { ServerToApp } from '../../../bindings/ServerToApp'
import { UserConnectedEvent } from '../../../bindings/UserConnectedEvent'
import { UserDisconnectedEvent } from '../../../bindings/UserDisconnectedEvent'
import { WalletMetadata } from '../../../bindings/WalletMetadata'
import {
ChangeNetworkTo,
ContentType,
MessageToSign,
RequestInternal,
TransactionToSign
} from './content'
import { ContentType, MessageToSign, RequestInternal, TransactionToSign } from './content'
import { triggerDeeplink } from './deeplinks'
import { AppBaseInitialize } from './initializeTypes'
import {
Expand Down Expand Up @@ -233,7 +228,7 @@ export class BaseApp extends EventEmitter<BaseEvents> {
return response.messages
}

changeNetwork = async (newNetwork: ChangeNetworkTo) => {
changeNetwork = async (newNetwork: AptosChangeNetworkInput) => {
const response = (await this.sendRequest({
type: ContentType.ChangeNetwork,
newNetwork: newNetwork
Expand Down
Loading

0 comments on commit fb0d430

Please sign in to comment.