Skip to content

Commit

Permalink
Aptos refactor (#197)
Browse files Browse the repository at this point in the history
* refactor-aptos

* aptos-refactor

* fix transactions

* remove logs

* fix code organisation

* fix connect
  • Loading branch information
LukassF authored Aug 20, 2024
1 parent b52bcef commit c96d9b2
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 182 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.6",
"@nightlylabs/wallet-selector-aptos": "0.1.7",
"@nightlylabs/wallet-selector-base": "^0.4.1",
"@nightlylabs/wallet-selector-polkadot": "0.2.7",
"@nightlylabs/wallet-selector-solana": "0.3.6",
Expand Down
41 changes: 30 additions & 11 deletions sdk/apps/modal-example/src/routes/aptos.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,26 @@ import {
AccountAuthenticator,
AccountAuthenticatorEd25519,
AnyRawTransaction,
Aptos
Aptos,
AccountPublicKey,
Network,
AptosConfig
} from '@aptos-labs/ts-sdk'
import { AccountInfo, AptosSignMessageInput, UserResponseStatus } from '@aptos-labs/wallet-standard'
import {
AccountInfo,
AptosSignAndSubmitTransactionInput,
AptosSignMessageInput,
UserResponseStatus
} from '@aptos-labs/wallet-standard'
import { NightlyConnectAptosAdapter } from '@nightlylabs/wallet-selector-aptos'
import { createEffect, createSignal, onMount, Show } from 'solid-js'
import { Title } from '@solidjs/meta'
import toast from 'solid-toast'

const aptos = new Aptos() // default to devnet
const aptosConfig = new AptosConfig({
network: Network.MAINNET
})
const aptos = new Aptos(aptosConfig)

export default function AptosPage() {
const [adapter, setAdapter] = createSignal<NightlyConnectAptosAdapter>()
Expand All @@ -35,7 +46,9 @@ export default function AptosPage() {
})

adapter.on('connect', (accInfo) => {
setAccountInfo(accInfo)
if (accInfo && 'address' in accInfo) {
setAccountInfo(accInfo)
}
})

adapter.on('disconnect', () => {
Expand All @@ -44,7 +57,9 @@ export default function AptosPage() {
})

adapter.on('accountChange', (accInfo) => {
setAccountInfo(accInfo)
if (accInfo && 'address' in accInfo) {
setAccountInfo(accInfo)
}
})

setAdapter(adapter)
Expand All @@ -70,7 +85,7 @@ export default function AptosPage() {
<Title>Aptos Example</Title>
<div id="modalAnchor" />
<Show
when={!!accountInfo()}
when={!!accountInfo()?.address}
fallback={
<button
onClick={() => {
Expand All @@ -88,12 +103,12 @@ export default function AptosPage() {
Connect
</button>
}>
<h1>Current address: {accountInfo()?.address.toString()}</h1>
<h1>Current address: {accountInfo()?.address?.toString()}</h1>
<button
onClick={async () => {
try {
const transaction = await aptos.transaction.build.simple({
sender: accountInfo()!.address.toString(),
sender: accountInfo()!.address?.toString(),
data: {
function: '0x1::coin::transfer',
typeArguments: ['0x1::aptos_coin::AptosCoin'],
Expand All @@ -104,6 +119,7 @@ export default function AptosPage() {
}
})
const signedTx = await adapter()!.signAndSubmitTransaction(transaction)

// Verify the transaction was signed
if (signedTx.status !== UserResponseStatus.APPROVED) {
toast.error('Transaction was not approved')
Expand All @@ -122,7 +138,7 @@ export default function AptosPage() {
onClick={async () => {
try {
const transaction = await aptos.transaction.build.simple({
sender: accountInfo()!.address.toString(),
sender: accountInfo()!.address?.toString(),
data: {
function: '0x1::coin::transfer',
typeArguments: ['0x1::aptos_coin::AptosCoin'],
Expand Down Expand Up @@ -173,8 +189,11 @@ export default function AptosPage() {
nonce: 'YOLO'
}
const signed = await adapter()!.signMessage(msgToSign)
if (signed.status !== UserResponseStatus.APPROVED) {
throw new Error('Message was not approved')
if ('signature' in signed) {
if (!signed.signature) throw new Error('Message was not approved')
} else {
if (signed.status !== UserResponseStatus.APPROVED)
throw new Error('Message was not approved')
}
toast.success('Message was signed!')
} catch (e) {
Expand Down
73 changes: 49 additions & 24 deletions sdk/apps/modal-example/src/routes/aptosCustom.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import { AccountAuthenticator, AccountAuthenticatorEd25519, Aptos } from '@aptos-labs/ts-sdk'
import { AccountInfo, AptosSignMessageInput, UserResponseStatus } from '@aptos-labs/wallet-standard'
import {
AccountAuthenticator,
AccountAuthenticatorEd25519,
AnyRawTransaction,
Aptos,
AccountPublicKey,
Network,
AptosConfig
} from '@aptos-labs/ts-sdk'
import {
AccountInfo,
AptosSignAndSubmitTransactionInput,
AptosSignMessageInput,
UserResponseStatus
} from '@aptos-labs/wallet-standard'
import { NightlyConnectAptosAdapter } from '@nightlylabs/wallet-selector-aptos'
import { createEffect, createSignal, onMount, Show } from 'solid-js'
import { Title } from '@solidjs/meta'
import toast from 'solid-toast'

const aptos = new Aptos() // default to devnet
const aptosConfig = new AptosConfig({
network: Network.MAINNET
})
const aptos = new Aptos(aptosConfig)

export default function AptosPage() {
const [adapter, setAdapter] = createSignal<NightlyConnectAptosAdapter>()
Expand All @@ -30,19 +46,19 @@ export default function AptosPage() {
'--nc-img-logo': 'url(https://alephzero.org/aleph-design/brand-elements/logo-day.svg)'
},
stylesOverride: `
.nc_headerWrapper {
background-color: blue;
}
.nc_headerLogo {
width: 200px;
}
.nc_modalContent {
border-radius: 0;
border: 3px dashed var(--nc-color-primary);
}
`,
.nc_headerWrapper {
background-color: blue;
}
.nc_headerLogo {
width: 200px;
}
.nc_modalContent {
border-radius: 0;
border: 3px dashed var(--nc-color-primary);
}
`,
qrConfigOverride: {
dotsOptions: {
color: 'purple'
Expand All @@ -59,7 +75,9 @@ export default function AptosPage() {
})

adapter.on('connect', (accInfo) => {
setAccountInfo(accInfo)
if (accInfo && 'address' in accInfo) {
setAccountInfo(accInfo)
}
})

adapter.on('disconnect', () => {
Expand All @@ -68,7 +86,9 @@ export default function AptosPage() {
})

adapter.on('accountChange', (accInfo) => {
setAccountInfo(accInfo)
if (accInfo && 'address' in accInfo) {
setAccountInfo(accInfo)
}
})

setAdapter(adapter)
Expand All @@ -94,7 +114,7 @@ export default function AptosPage() {
<Title>Aptos Example</Title>
<div id="modalAnchor" />
<Show
when={!!accountInfo()}
when={!!accountInfo()?.address}
fallback={
<button
onClick={() => {
Expand All @@ -112,12 +132,12 @@ export default function AptosPage() {
Connect
</button>
}>
<h1>Current address: {accountInfo()?.address.toString()}</h1>
<h1>Current address: {accountInfo()?.address?.toString()}</h1>
<button
onClick={async () => {
try {
const transaction = await aptos.transaction.build.simple({
sender: accountInfo()!.address.toString(),
sender: accountInfo()!.address?.toString(),
data: {
function: '0x1::coin::transfer',
typeArguments: ['0x1::aptos_coin::AptosCoin'],
Expand All @@ -128,6 +148,7 @@ export default function AptosPage() {
}
})
const signedTx = await adapter()!.signAndSubmitTransaction(transaction)

// Verify the transaction was signed
if (signedTx.status !== UserResponseStatus.APPROVED) {
toast.error('Transaction was not approved')
Expand All @@ -146,7 +167,7 @@ export default function AptosPage() {
onClick={async () => {
try {
const transaction = await aptos.transaction.build.simple({
sender: accountInfo()!.address.toString(),
sender: accountInfo()!.address?.toString(),
data: {
function: '0x1::coin::transfer',
typeArguments: ['0x1::aptos_coin::AptosCoin'],
Expand Down Expand Up @@ -197,8 +218,12 @@ export default function AptosPage() {
nonce: 'YOLO'
}
const signed = await adapter()!.signMessage(msgToSign)
if (signed.status !== UserResponseStatus.APPROVED) {
throw new Error('Message was not approved')

if ('signature' in signed) {
if (!signed.signature) throw new Error('Message was not approved')
} else {
if (signed.status !== UserResponseStatus.APPROVED)
throw new Error('Message was not approved')
}
toast.success('Message was signed!')
} catch (e) {
Expand Down
4 changes: 2 additions & 2 deletions sdk/packages/aptos/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
TransactionToSign
} from '@nightlylabs/nightly-connect-base'
import { EventEmitter } from 'eventemitter3'
import { UserDisconnectedEvent } from '../../../bindings/UserDisconnectedEvent'
import { WalletMetadata } from '../../../bindings/WalletMetadata'
import {
AppAptosInitialize,
APTOS_NETWORK,
Expand All @@ -25,8 +27,6 @@ import {
serializeAptosTx,
serializeObject
} from './utils'
import { UserDisconnectedEvent } from '../../../bindings/UserDisconnectedEvent'
import { WalletMetadata } from '../../../bindings/WalletMetadata'

interface AptosAppEvents {
userConnected: (e: AccountInfo, networkInfo: NetworkInfo) => void
Expand Down
2 changes: 1 addition & 1 deletion sdk/packages/selector-aptos/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nightlylabs/wallet-selector-aptos",
"version": "0.1.6",
"version": "0.1.7",
"description": "",
"type": "module",
"exports": {
Expand Down
Loading

0 comments on commit c96d9b2

Please sign in to comment.