Skip to content

Commit

Permalink
Merge pull request #64 from nightly-labs/selector/pubkey-try-catch
Browse files Browse the repository at this point in the history
[DRAFT!] use try...catch on userConnected event + bump mysten packages
  • Loading branch information
NorbertBodziony authored Sep 25, 2023
2 parents a138691 + fe6c7e9 commit 1084fd5
Show file tree
Hide file tree
Showing 21 changed files with 402 additions and 384 deletions.
14 changes: 7 additions & 7 deletions sdk/apps/modal-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
"vite": "^4.1.4"
},
"dependencies": {
"@mysten/sui.js": "^0.37.1",
"@mysten/wallet-adapter-wallet-standard": "^0.7.9",
"@nightlylabs/wallet-selector-solana": "0.2.2",
"@mysten/sui.js": "^0.42.0",
"@mysten/wallet-adapter-wallet-standard": "^0.8.0",
"@nightlylabs/wallet-selector-solana": "0.2.3",
"@nightlylabs/nightly-connect-solana": "0.0.26",
"@nightlylabs/nightly-connect-sui": "0.0.25",
"@nightlylabs/wallet-selector-sui": "0.2.2",
"@nightlylabs/nightly-connect-sui": "0.0.26",
"@nightlylabs/wallet-selector-sui": "0.2.3",
"@nightlylabs/nightly-connect-polkadot": "0.0.6",
"@nightlylabs/wallet-selector-polkadot": "0.1.2",
"@nightlylabs/wallet-selector-polkadot": "0.1.3",
"@polkadot/extension-inject": "^0.46.5",
"@polkadot/api": "^10.9.1",
"@solana/web3.js": "^1.77.2",
Expand All @@ -38,4 +38,4 @@
"engines": {
"node": ">=16.8"
}
}
}
4 changes: 2 additions & 2 deletions sdk/apps/modal-example/src/routes/sui.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createEffect, createSignal, onMount, Show } from 'solid-js'
import { Title } from 'solid-start'
import { NightlyConnectSuiAdapter } from '@nightlylabs/wallet-selector-sui'
import { TransactionBlock } from '@mysten/sui.js'
import { TransactionBlock } from '@mysten/sui.js/transactions'
import toast from 'solid-toast'

export default function Sui() {
Expand Down Expand Up @@ -104,7 +104,7 @@ export default function Sui() {
onClick={async () => {
try {
const accounts = await adapter()!.getAccounts()
await adapter()!.signMessage!({
await adapter()!.signPersonalMessage!({
message: new TextEncoder().encode('I love Nightly'),
account: accounts[0]
})
Expand Down
4 changes: 2 additions & 2 deletions sdk/apps/modal-example/src/routes/suiInitOnConnect.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createSignal, onMount, Show } from 'solid-js'
import { Title } from 'solid-start'
import { NightlyConnectSuiAdapter } from '@nightlylabs/wallet-selector-sui'
import { TransactionBlock } from '@mysten/sui.js'
import { TransactionBlock } from '@mysten/sui.js/transactions'
import toast from 'solid-toast'

export default function Sui() {
Expand Down Expand Up @@ -82,7 +82,7 @@ export default function Sui() {
onClick={async () => {
try {
const accounts = await adapter()!.getAccounts()
await adapter()!.signMessage!({
await adapter()!.signPersonalMessage!({
message: new TextEncoder().encode('I love Nightly'),
account: accounts[0]
})
Expand Down
12 changes: 6 additions & 6 deletions sdk/apps/nextjs-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
"lint": "next lint"
},
"dependencies": {
"@nightlylabs/wallet-selector-solana": "0.2.2",
"@nightlylabs/wallet-selector-sui": "0.2.2",
"@mysten/wallet-adapter-wallet-standard": "0.7.12",
"@mysten/wallet-adapter-base": "0.7.12",
"@mysten/sui.js": "0.37.1",
"@mysten/wallet-kit": "0.4.12",
"@nightlylabs/wallet-selector-solana": "0.2.3",
"@nightlylabs/wallet-selector-sui": "0.2.3",
"@mysten/wallet-adapter-wallet-standard": "0.8.0",
"@mysten/wallet-adapter-base": "0.9.0",
"@mysten/sui.js": "0.42.0",
"@mysten/wallet-kit": "0.7.3",
"@solana/web3.js": "^1.77.2",
"@types/node": "20.4.2",
"@types/react": "18.2.15",
Expand Down
10 changes: 5 additions & 5 deletions sdk/apps/sui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nightlylabs/nightly-connect-sui",
"version": "0.0.25",
"version": "0.0.26",
"type": "module",
"exports": {
".": {
Expand Down Expand Up @@ -31,7 +31,7 @@
},
"devDependencies": {
"@rollup/plugin-commonjs": "^25.0.0",
"@rollup/plugin-node-resolve": "^15.1.0",
"@rollup/plugin-node-resolve": "^15.2.1",
"@rollup/plugin-terser": "^0.4.3",
"@rollup/plugin-typescript": "^11.1.1",
"@types/node": "^20.3.0",
Expand All @@ -46,11 +46,11 @@
"typescript": "^5.1.3"
},
"dependencies": {
"@mysten/sui.js": "^0.37.1",
"@mysten/wallet-standard": "^0.5.12",
"@mysten/sui.js": "^0.42.0",
"@mysten/wallet-standard": "^0.8.0",
"@nightlylabs/nightly-connect-base": "0.0.25",
"@noble/hashes": "^1.3.0",
"eventemitter3": "^5.0.1",
"uuid": "^9.0.0"
}
}
}
2 changes: 2 additions & 0 deletions sdk/apps/sui/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default [
plugins: [typescript(), nodeResolve(), commonjs(), terser()],
external: [
'@mysten/sui.js',
'@mysten/sui.js/client',
'@mysten/wallet-standard',
'@noble/hashes',
'@nightlylabs/nightly-connect-base',
Expand Down Expand Up @@ -55,6 +56,7 @@ export default [
],
external: [
'@mysten/sui.js',
'@mysten/sui.js/client',
'@mysten/wallet-standard',
'@noble/hashes',
'@nightlylabs/nightly-connect-base',
Expand Down
7 changes: 4 additions & 3 deletions sdk/apps/sui/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { UserConnectedEvent } from '../../../bindings/UserConnectedEvent'
import { SignedMessage, SignedTransaction } from '@mysten/sui.js'
// import { SignedMessage, SignedTransaction } from '@mysten/sui.js/dist/esm/signers/types'
import type { SignedMessage, SignedTransaction } from '@mysten/sui.js/src/signers/types'
import {
SuiSignAndExecuteTransactionBlockInput,
SuiSignAndExecuteTransactionBlockOutput,
SuiSignMessageInput,
SuiSignPersonalMessageInput,
SuiSignTransactionBlockInput
} from '@mysten/wallet-standard'
import {
Expand Down Expand Up @@ -98,7 +99,7 @@ export class AppSui extends EventEmitter<SuiAppEvents> {
return JSON.parse(signedTx[0].transaction)
}

signMessage = async (input: SuiSignMessageInput, encoding?: string) => {
signMessage = async (input: SuiSignPersonalMessageInput, encoding?: string) => {
const request: MessageToSign = {
message: new TextDecoder().decode(input.message),
metadata: JSON.stringify({ encoding: encoding || 'hex', account: input.account })
Expand Down
3 changes: 2 additions & 1 deletion sdk/apps/sui/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AppDisconnectedEvent } from '../../../bindings/AppDisconnectedEvent'
import { SignedMessage, SignedTransaction, SuiTransactionBlockResponse } from '@mysten/sui.js'
import type { SuiTransactionBlockResponse } from '@mysten/sui.js/client'
import type { SignedMessage, SignedTransaction } from '@mysten/sui.js/src/signers/types'
import {
BaseClient,
ClientBaseInitialize,
Expand Down
56 changes: 29 additions & 27 deletions sdk/apps/sui/src/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ import { assert, beforeAll, beforeEach, describe, expect, test, vi } from 'vites
import { AppSui } from './app'
import { ClientSui } from './client'
import { signTransactionBlock, SUI_NETWORK, TEST_APP_INITIALIZE } from './utils'

import { fromB64, toB64 } from '@mysten/sui.js/utils'
import { TransactionBlock } from '@mysten/sui.js/transactions'
import { Ed25519Keypair } from '@mysten/sui.js/keypairs/ed25519'
import { verifyPersonalMessage, verifyTransactionBlock } from '@mysten/sui.js/verify'
import {
Ed25519Keypair,
fromB64,
IntentScope,
messageWithIntent,
toB64,
toSerializedSignature,
TransactionBlock,
verifyMessage
} from '@mysten/sui.js'
parseSerializedSignature,
toSerializedSignature
} from '@mysten/sui.js/cryptography'
import { blake2b } from '@noble/hashes/blake2b'
import { fetch } from 'cross-fetch'
import { WalletAccount } from '@mysten/wallet-standard'
Expand Down Expand Up @@ -95,25 +94,24 @@ describe('SUI client tests', () => {
chain: 'sui:testnet'
})

const isValid = await verifyMessage(
signedTx.transactionBlockBytes,
signedTx.signature,
IntentScope.TransactionData
)
expect(isValid).toBeTruthy()
try {
// Will throw if invalid
await verifyTransactionBlock(fromB64(signedTx.transactionBlockBytes), signedTx.signature)
} catch (error) {
assert(false, 'Transaction block is invalid')
}
})
test('#on("signMessages")', async () => {
const msgToSign = 'Hello World'
const msgToSign = 'I love Nightly'
client.on('signMessages', async (e) => {
const msg = e.messages[0].message
const msgTo64 = toB64(new TextEncoder().encode(msg))
const intentMessage = messageWithIntent(IntentScope.PersonalMessage, fromB64(msgTo64))
const digest = blake2b(intentMessage, { dkLen: 32 })
const signature = alice_keypair.signData(digest)
const msgTo64 = new TextEncoder().encode(msg)
const { signature } = await alice_keypair.signPersonalMessage(msgTo64)
await verifyPersonalMessage(msgTo64, signature)
const signedMessage = {
messageBytes: msg,
signature: toSerializedSignature({
signature,
signature: fromB64(signature),
signatureScheme: 'ED25519',
pubKey: alice_keypair.getPublicKey()
})
Expand All @@ -129,13 +127,17 @@ describe('SUI client tests', () => {
message: new TextEncoder().encode(msgToSign),
account: aliceWalletAccount
})
const signData = new TextEncoder().encode(msgToSign)
const isValid = await verifyMessage(
signData,
signedMessage.signature,
IntentScope.PersonalMessage
)
expect(isValid).toBe(true)
try {
// We need to deserialize the signature
const parsedSignature = parseSerializedSignature(signedMessage.signature)
// Will throw if invalid
await verifyPersonalMessage(
new TextEncoder().encode(msgToSign),
toB64(parsedSignature.signature!)
)
} catch (error) {
assert(false, 'Message is invalid')
}
})
test('#on("signAndExecuteSignTransaction")', async () => {
const tx = new TransactionBlock()
Expand Down
18 changes: 10 additions & 8 deletions sdk/apps/sui/src/http-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import {
smartDelay
} from '@nightlylabs/nightly-connect-base'
import { HttpClientSui } from './http-client'

import { Ed25519Keypair, IntentScope, toB64, TransactionBlock, verifyMessage } from '@mysten/sui.js'
import { fromB64, toB64 } from '@mysten/sui.js/utils'
import { TransactionBlock } from '@mysten/sui.js/transactions'
import { Ed25519Keypair } from '@mysten/sui.js/keypairs/ed25519'
import { verifyPersonalMessage, verifyTransactionBlock } from '@mysten/sui.js/verify'
import { hexToBytes } from '@noble/hashes/utils'
import { WalletAccount } from '@mysten/wallet-standard'

Expand Down Expand Up @@ -93,11 +95,11 @@ describe('SUI http-client tests', () => {
await smartDelay()
const signedTx = await promiseSignTransaction

const isValid = await verifyMessage(
signedTx.transactionBlockBytes,
signedTx.signature,
IntentScope.TransactionData
)
expect(isValid).toBeTruthy()
try {
// Will throw if invalid
await verifyTransactionBlock(fromB64(signedTx.transactionBlockBytes), signedTx.signature)
} catch (error) {
assert(false, 'Transaction block is invalid')
}
})
})
2 changes: 1 addition & 1 deletion sdk/apps/sui/src/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SUI_NETWORK, parseRequest } from './utils'
import { HttpConnectSessionRequest } from '../../../bindings/HttpConnectSessionRequest'
import { HttpGetPendingRequestsRequest } from '../../../bindings/HttpGetPendingRequestsRequest'
import { HttpGetPendingRequestRequest } from '../../../bindings/HttpGetPendingRequestRequest'
import { SignedTransaction } from '@mysten/sui.js'
import { type SignedTransaction } from '@mysten/sui.js/src/signers/types'
import { HttpGetSessionInfoResponse } from '../../../bindings/HttpGetSessionInfoResponse'

export class HttpClientSui {
Expand Down
33 changes: 18 additions & 15 deletions sdk/apps/sui/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import {
JsonRpcProvider,
Connection,
TransactionBlock,
messageWithIntent,
IntentScope,
toSerializedSignature,
Ed25519Keypair
} from '@mysten/sui.js'
import { type TransactionBlock } from '@mysten/sui.js/transactions'
import { type Ed25519Keypair } from '@mysten/sui.js/keypairs/ed25519'
import { IntentScope, messageWithIntent, toSerializedSignature } from '@mysten/sui.js/cryptography'
import { blake2b } from '@noble/hashes/blake2b'
import { AppBaseInitialize, ContentType, RELAY_ENDPOINT, RequestContent } from '@nightlylabs/nightly-connect-base'
import { CustomSuiRequest, SignMessagesSuiRequest, SignTransactionsSuiRequest, SuiRequest } from './requestTypes'
import {
AppBaseInitialize,
ContentType,
RELAY_ENDPOINT,
RequestContent
} from '@nightlylabs/nightly-connect-base'
import {
CustomSuiRequest,
SignMessagesSuiRequest,
SignTransactionsSuiRequest,
SuiRequest
} from './requestTypes'
import { SuiClient } from '@mysten/sui.js/client'

export type AppSuiInitialize = Omit<AppBaseInitialize, 'network'>

Expand All @@ -31,9 +36,7 @@ export function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms))
}

const suiConnection = new JsonRpcProvider(
new Connection({ fullnode: 'https://fullnode.testnet.sui.io/' })
)
const suiConnection = new SuiClient({ url: 'https://fullnode.testnet.sui.io/' })
export const signTransactionBlock = async (tx: TransactionBlock, account: Ed25519Keypair) => {
const transactionBlockBytes = await tx.build({
provider: suiConnection,
Expand Down Expand Up @@ -81,4 +84,4 @@ export const parseRequest = (request: RequestContent, sessionId: string): SuiReq
return customRequest
}
}
}
}
3 changes: 2 additions & 1 deletion sdk/apps/sui/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@
"ts-node": {
"require": ["tsconfig-paths/register"]
},
"include": ["../../bindings/*", "src/*"]
"include": ["../../bindings/*", "src/*"],
"exclude": ["node_modules", "dist"]
}
2 changes: 1 addition & 1 deletion sdk/packages/selector-polkadot/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nightlylabs/wallet-selector-polkadot",
"version": "0.1.2",
"version": "0.1.3",
"description": "",
"type": "module",
"exports": {
Expand Down
Loading

0 comments on commit 1084fd5

Please sign in to comment.