Skip to content

Commit

Permalink
fix sui main package
Browse files Browse the repository at this point in the history
  • Loading branch information
NorbertBodziony committed Sep 24, 2023
1 parent fc9da28 commit 855de10
Show file tree
Hide file tree
Showing 10 changed files with 282 additions and 81 deletions.
6 changes: 3 additions & 3 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 @@ -53,4 +53,4 @@
"eventemitter3": "^5.0.1",
"uuid": "^9.0.0"
}
}
}
16 changes: 0 additions & 16 deletions sdk/apps/sui/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,7 @@ export default [
plugins: [typescript(), nodeResolve(), commonjs(), terser()],
external: [
'@mysten/sui.js',
'@mysten/sui.js/dist/cjs/signers/types',
'@mysten/sui.js/client',
'@mysten/sui.js/transactions',
'@mysten/sui.js/keypairs/ed25519',
'@mysten/sui.js/dist/cjs/providers/json-rpc-provider',
'@mysten/sui.js/dist/cjs/rpc/connection',
'@mysten/sui.js/verify',
'@mysten/sui.js/utils',
'@mysten/sui.js/cryptography',
'@mysten/wallet-standard',
'@noble/hashes',
'@nightlylabs/nightly-connect-base',
Expand Down Expand Up @@ -64,15 +56,7 @@ export default [
],
external: [
'@mysten/sui.js',
'@mysten/sui.js/dist/cjs/signers/types',
'@mysten/sui.js/client',
'@mysten/sui.js/transactions',
'@mysten/sui.js/keypairs/ed25519',
'@mysten/sui.js/dist/cjs/providers/json-rpc-provider',
'@mysten/sui.js/dist/cjs/rpc/connection',
'@mysten/sui.js/verify',
'@mysten/sui.js/utils',
'@mysten/sui.js/cryptography',
'@mysten/wallet-standard',
'@noble/hashes',
'@nightlylabs/nightly-connect-base',
Expand Down
3 changes: 2 additions & 1 deletion sdk/apps/sui/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { UserConnectedEvent } from '../../../bindings/UserConnectedEvent'
import { SignedMessage, SignedTransaction } from '@mysten/sui.js/dist/cjs/signers/types'
// 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,
Expand Down
4 changes: 2 additions & 2 deletions sdk/apps/sui/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AppDisconnectedEvent } from '../../../bindings/AppDisconnectedEvent'
import { SuiTransactionBlockResponse } from '@mysten/sui.js/client'
import { SignedMessage, SignedTransaction } from '@mysten/sui.js/dist/cjs/signers/types'
import type { SuiTransactionBlockResponse } from '@mysten/sui.js/client'
import type { SignedMessage, SignedTransaction } from '@mysten/sui.js/src/signers/types'
import {
BaseClient,
ClientBaseInitialize,
Expand Down
45 changes: 29 additions & 16 deletions sdk/apps/sui/src/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ 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 } from '@mysten/sui.js/verify'
import { IntentScope, messageWithIntent, toSerializedSignature } from '@mysten/sui.js/cryptography'
import { verifyPersonalMessage, verifyTransactionBlock } from '@mysten/sui.js/verify'
import {
IntentScope,
messageWithIntent,
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 @@ -89,24 +94,24 @@ describe('SUI client tests', () => {
chain: 'sui:testnet'
})

const isValid = await verifyPersonalMessage(
new TextEncoder().encode(signedTx.transactionBlockBytes),
signedTx.signature
)
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 @@ -122,9 +127,17 @@ describe('SUI client tests', () => {
message: new TextEncoder().encode(msgToSign),
account: aliceWalletAccount
})
const signData = new TextEncoder().encode(msgToSign)
const isValid = await verifyPersonalMessage(signData, signedMessage.signature)
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
15 changes: 8 additions & 7 deletions sdk/apps/sui/src/http-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import {
smartDelay
} from '@nightlylabs/nightly-connect-base'
import { HttpClientSui } from './http-client'
import { toB64 } from '@mysten/sui.js/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 } from '@mysten/sui.js/verify'
import { verifyPersonalMessage, verifyTransactionBlock } from '@mysten/sui.js/verify'
import { hexToBytes } from '@noble/hashes/utils'
import { WalletAccount } from '@mysten/wallet-standard'

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

const isValid = await verifyPersonalMessage(
new TextEncoder().encode(signedTx.transactionBlockBytes),
signedTx.signature
)
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//dist/cjs/signers/types'
import { type SignedTransaction } from '@mysten/sui.js/src/signers/types'
import { HttpGetSessionInfoResponse } from '../../../bindings/HttpGetSessionInfoResponse'

export class HttpClientSui {
Expand Down
11 changes: 4 additions & 7 deletions sdk/apps/sui/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TransactionBlock } from '@mysten/sui.js/transactions'
import { Ed25519Keypair } from '@mysten/sui.js/keypairs/ed25519'
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 {
Expand All @@ -14,8 +14,7 @@ import {
SignTransactionsSuiRequest,
SuiRequest
} from './requestTypes'
import { JsonRpcProvider } from '@mysten/sui.js/dist/cjs/providers/json-rpc-provider'
import { Connection } from '@mysten/sui.js/dist/cjs/rpc/connection'
import { SuiClient } from '@mysten/sui.js/client'

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

Expand All @@ -37,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
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"]
}
Loading

0 comments on commit 855de10

Please sign in to comment.