Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support change network mobile aptos #205

Merged
merged 3 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
37 changes: 37 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,7 @@ export default function AptosPage() {
const [adapter, setAdapter] = createSignal<NightlyConnectAptosAdapter>()
const [eager, setEager] = createSignal(false)
const [accountInfo, setAccountInfo] = createSignal<AccountInfo>()

onMount(async () => {
NightlyConnectAptosAdapter.build(
{
Expand Down Expand Up @@ -203,6 +204,42 @@ 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!`)
}
} catch (error) {
toast.error("Couldn't change network")
console.log(error)
}
}}>
Change network
</button>
<button
onClick={() => {
adapter()?.disconnect()
Expand Down
4 changes: 2 additions & 2 deletions sdk/packages/aptos/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nightlylabs/nightly-connect-aptos",
"version": "0.0.2",
"version": "0.0.3",
"type": "module",
"exports": {
".": {
Expand Down Expand Up @@ -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.28",
"@nightlylabs/nightly-connect-base": "^0.0.29",
"eventemitter3": "^5.0.1"
}
}
11 changes: 11 additions & 0 deletions sdk/packages/aptos/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
AccountInfo,
AptosChangeNetworkMethod,
AptosSignAndSubmitTransactionMethod,
AptosSignMessageMethod,
AptosSignMessageOutput,
Expand Down Expand Up @@ -151,4 +152,14 @@ export class AppAptos extends EventEmitter<AptosAppEvents> {
args: deserializeObject(signedTx[0].message) as AptosSignMessageOutput
}
}

changeNetwork: AptosChangeNetworkMethod = async (input) => {
const changedNetworkSuccess = await this.base.changeNetwork(input)
return {
status: UserResponseStatus.APPROVED,
args: {
success: !!changedNetworkSuccess.newNetwork
}
}
}
}
56 changes: 46 additions & 10 deletions sdk/packages/aptos/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import { AppDisconnectedEvent } from '../../../bindings/AppDisconnectedEvent'
import { AnyRawTransaction } from '@aptos-labs/ts-sdk'
import {
AccountInfo,
AptosChangeNetworkInput,
AptosSignAndSubmitTransactionOutput,
AptosSignMessageInput,
AptosSignMessageOutput,
AptosSignTransactionOutput,
NetworkInfo
} from '@aptos-labs/wallet-standard'
import {
BaseClient,
ClientBaseInitialize,
Connect as ConnectBase
} from '@nightlylabs/nightly-connect-base'
import { EventEmitter } from 'eventemitter3'
import { AppDisconnectedEvent } from '../../../bindings/AppDisconnectedEvent'
import { GetInfoResponse } from '../../../bindings/GetInfoResponse'
import { AptosRequest } from './requestTypes'
import {
Expand All @@ -16,15 +26,6 @@ import {
serializeObject,
serializePendingTransactionResponse
} from './utils'
import { AnyRawTransaction } from '@aptos-labs/ts-sdk'
import {
AccountInfo,
AptosSignAndSubmitTransactionOutput,
AptosSignMessageInput,
AptosSignMessageOutput,
AptosSignTransactionOutput,
NetworkInfo
} from '@aptos-labs/wallet-standard'
export interface SignAndSubmitTransactionEvent {
sessionId: string
requestId: string
Expand All @@ -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 @@ -136,6 +151,22 @@ export class ClientAptos extends EventEmitter<ClientAptosEvents> {
return requests.map((request) => parseRequest(request, sessionIdToUse))
}

public resolveChangeNetwork = async ({
requestId,
newNetwork,
sessionId
}: ResolveChangeNetwork) => {
const sessionIdToUse = sessionId || this.sessionId
if (sessionIdToUse === undefined) {
throw new Error('Session id is undefined')
}
await this.baseClient.resolveChangeNetwork({
requestId,
newNetwork,
sessionId: sessionIdToUse
})
}

public resolveSignTransaction = async ({
requestId,
signedTransactions,
Expand Down Expand Up @@ -236,3 +267,8 @@ export interface ResolveSignAptosMessage {
signedMessages: Array<AptosSignMessageOutput>
sessionId?: string
}
export interface ResolveChangeNetwork {
requestId: string
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
Loading
Loading