diff --git a/.eslintrc.js b/.eslintrc.js index 3ac28bc5fc..4c04d6b727 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -25,6 +25,7 @@ module.exports = { 'out', 'generated', 'templates', + 'grow-rooch-v1', 'dist', 'coverage', 'next-env.d.ts', diff --git a/.prettierignore b/.prettierignore index 5977c1a2ef..4aff90eb7a 100644 --- a/.prettierignore +++ b/.prettierignore @@ -4,11 +4,11 @@ !/dashboard !/sdk !/rooch-portal-v1 -!/grow-rooch-v1 !/.config !/*.js # ignore generated files +grow-rooch-v1 .vscode .next node_modules diff --git a/grow-rooch-v1/src/pages/mint/mint-page.tsx b/grow-rooch-v1/src/pages/mint/mint-page.tsx index a4b1f0a298..c2239c0897 100644 --- a/grow-rooch-v1/src/pages/mint/mint-page.tsx +++ b/grow-rooch-v1/src/pages/mint/mint-page.tsx @@ -1 +1 @@ -export const MintPage = () => {} +// export const MintPage = () => {} diff --git a/sdk/typescript/rooch-sdk-kit/src/provider/clientProvider.tsx b/sdk/typescript/rooch-sdk-kit/src/provider/clientProvider.tsx index 3c16e1fa21..96e12825cf 100644 --- a/sdk/typescript/rooch-sdk-kit/src/provider/clientProvider.tsx +++ b/sdk/typescript/rooch-sdk-kit/src/provider/clientProvider.tsx @@ -1,7 +1,7 @@ // Copyright (c) RoochNetwork // SPDX-License-Identifier: Apache-2.0 -import {createContext, useRef, useEffect, useState} from 'react' +import { createContext, useRef, useEffect, useState } from 'react' import type { ReactNode } from 'react' import { diff --git a/sdk/typescript/rooch-sdk/src/account/interface.ts b/sdk/typescript/rooch-sdk/src/account/interface.ts index 72074191ed..fa93c02c2b 100644 --- a/sdk/typescript/rooch-sdk/src/account/interface.ts +++ b/sdk/typescript/rooch-sdk/src/account/interface.ts @@ -5,6 +5,5 @@ import { IAuthorizer } from '../auth' export interface IAccount { getAuthorizer(): IAuthorizer - getAddress(): string | undefined - getRoochAddress(): Promise + getAddress(): string } diff --git a/sdk/typescript/rooch-sdk/src/account/roochAccount.ts b/sdk/typescript/rooch-sdk/src/account/roochAccount.ts index 023a4e1219..aec2c0b717 100644 --- a/sdk/typescript/rooch-sdk/src/account/roochAccount.ts +++ b/sdk/typescript/rooch-sdk/src/account/roochAccount.ts @@ -14,27 +14,24 @@ import { IAccount } from '../account/interface' export class RoochAccount implements IAccount { private readonly keypair: Ed25519Keypair private readonly client: RoochClient - private roochAddress?: string + private address?: string private authorizer?: IAuthorizer - public constructor(client: RoochClient) { + public constructor(client: RoochClient, keyPair?: Ed25519Keypair) { this.client = client - this.keypair = new Ed25519Keypair() + this.keypair = keyPair ?? Ed25519Keypair.generate() } public getKeypar(): Ed25519Keypair { return this.keypair } - public getAddress(): string | undefined { - return undefined - } - - public getRoochAddress(): Promise { - if (!this.roochAddress) { - this.roochAddress = this.keypair.getPublicKey().toRoochAddress() + public getAddress(): string { + if (!this.address) { + this.address = this.keypair.getPublicKey().toRoochAddress() } - return Promise.resolve(this.roochAddress) + + return this.address } public getAuthorizer(): IAuthorizer { @@ -47,12 +44,12 @@ export class RoochAccount implements IAccount { async sendTransaction( funcId: FunctionId, - args: Arg[], - tyArgs: TypeTag[], - opts: SendRawTransactionOpts, + args?: Arg[], + tyArgs?: TypeTag[], + opts?: SendRawTransactionOpts, ): Promise { return this.client.sendRawTransaction({ - address: await this.getRoochAddress(), + address: this.getAddress(), authorizer: this.getAuthorizer(), funcId, args, @@ -63,14 +60,14 @@ export class RoochAccount implements IAccount { async getBalance(coinType: string): Promise { return this.client.getBalance({ - address: await this.getRoochAddress(), + address: this.getAddress(), coinType, }) } async getBalances(cursor: string, limit: string): Promise { return this.client.getBalances({ - address: await this.getRoochAddress(), + address: this.getAddress(), cursor, limit, }) diff --git a/sdk/typescript/rooch-sdk/src/account/roochSessionAccount.ts b/sdk/typescript/rooch-sdk/src/account/roochSessionAccount.ts index 1b2aa10f92..6816dae959 100644 --- a/sdk/typescript/rooch-sdk/src/account/roochSessionAccount.ts +++ b/sdk/typescript/rooch-sdk/src/account/roochSessionAccount.ts @@ -1,21 +1,19 @@ // Copyright (c) RoochNetwork // SPDX-License-Identifier: Apache-2.0 -import { FunctionId, TypeTag, Arg } from '../types' +import { FunctionId, TypeTag, Arg, IPage } from '../types' import { addressToListTuple, addressToSeqNumber, encodeArg, encodeFunctionCall } from '../utils' import { RoochAccount } from './roochAccount' import { RoochClient } from '../client/roochClient' -import { SendRawTransactionOpts } from '../client/roochClientTypes' +import { SendRawTransactionOpts, SessionInfo } from '../client/roochClientTypes' import { IAccount } from '../account/interface.ts' import { IAuthorizer } from '../auth' import { RoochTransactionData, AccountAddress as BCSAccountAddress, - RoochTransaction, } from '../generated/runtime/rooch_types/mod' import { DEFAULT_MAX_GAS_AMOUNT } from '../constants' -import { BcsSerializer } from '../generated/runtime/bcs/bcsSerializer' const SCOPE_LENGTH = 3 const SCOPE_MODULE_ADDRESSS = 0 @@ -83,7 +81,7 @@ export class RoochSessionAccount implements IAccount { const args: Arg[] = [ { type: { Vector: 'U8' }, - value: addressToSeqNumber(await this.getAuthKey()), + value: addressToSeqNumber(this.getAuthKey()), }, { type: { Vector: 'Address' }, @@ -102,7 +100,7 @@ export class RoochSessionAccount implements IAccount { value: BigInt(this.maxInactiveInterval), }, ] - const number = await this.client.getSequenceNumber(await this.getRoochAddress()) + const sequenceNumber = await this.client.getSequenceNumber(this.getAddress()) const bcsArgs = args.map((arg) => encodeArg(arg)) const scriptFunction = encodeFunctionCall( '0x3::session_key::create_session_key_with_multi_scope_entry', @@ -110,8 +108,8 @@ export class RoochSessionAccount implements IAccount { bcsArgs, ) const txData = new RoochTransactionData( - new BCSAccountAddress(addressToListTuple(await this.getRoochAddress())), - BigInt(number), + new BCSAccountAddress(addressToListTuple(this.getAddress())), + BigInt(sequenceNumber), BigInt(this.client.getChainId()), BigInt(opts?.maxGasAmount ?? DEFAULT_MAX_GAS_AMOUNT), scriptFunction, @@ -121,36 +119,21 @@ export class RoochSessionAccount implements IAccount { } protected async register(txData: RoochTransactionData): Promise { - const transactionDataPayload = (() => { - const se = new BcsSerializer() - txData.serialize(se) - return se.getBytes() - })() - - const authResult = await this.account.getAuthorizer().auth(transactionDataPayload) - const transaction = new RoochTransaction(txData, authResult) - - const transactionPayload = (() => { - const se = new BcsSerializer() - transaction.serialize(se) - return se.getBytes() - })() - - await this.client.sendRawTransaction(transactionPayload) + const s = await this.client.sendRawTransaction({ + authorizer: this.account.getAuthorizer(), + data: txData, + }) + console.log(s) return this } - public getAuthKey(): Promise { - return this.sessionAccount.getRoochAddress() - } - - getAddress(): string | undefined { - return undefined + public getAuthKey(): string { + return this.sessionAccount.getAddress() } - getRoochAddress(): Promise { - return this.account.getRoochAddress() + getAddress(): string { + return this.account.getAddress() } getAuthorizer(): IAuthorizer { @@ -172,8 +155,8 @@ export class RoochSessionAccount implements IAccount { opts?: SendRawTransactionOpts, ): Promise { return this.client.sendRawTransaction({ - address: await this.account.getRoochAddress(), - authorizer: this.sessionAccount.getAuthorizer(), + address: this.getAddress(), + authorizer: this.getAuthorizer(), funcId, args, tyArgs, @@ -182,29 +165,52 @@ export class RoochSessionAccount implements IAccount { } public async isExpired(): Promise { - if (this.localCreateSessionTime + this.maxInactiveInterval > Date.now() / 1000) { - return Promise.resolve(true) - } + // if (this.localCreateSessionTime + this.maxInactiveInterval > Date.now() / 1000) { + // return Promise.resolve(true) + // } - return this.client.sessionIsExpired( - await this.account.getRoochAddress(), - await this.getAuthKey(), - ) + return this.client.sessionIsExpired(this.account.getAddress(), this.getAuthKey()) + } + + public async getSessionKey() { + const session = this.client.executeViewFunction({ + funcId: '0x3::session_key::get_session_key', + tyArgs: [], + args: [ + { + type: 'Address', + value: this.getAddress(), + }, + { + type: { Vector: 'U8' }, + value: addressToSeqNumber(this.getAuthKey()), + }, + ], + }) + + return session + } + + public async querySessionKeys( + cursor: string | null, + limit: number, + ): Promise> { + return this.client.querySessionKeys(this.getAddress(), cursor, limit) } public async destroy(opts?: SendRawTransactionOpts): Promise { - return await this.sendTransaction( - '0x3::session_key::remove_session_key_entry', - [ + return await this.client.sendRawTransaction({ + funcId: '0x3::session_key::remove_session_key_entry', + args: [ { type: { Vector: 'U8' }, - value: addressToSeqNumber(await this.getAuthKey()), + value: addressToSeqNumber(this.getAuthKey()), }, ], - [], - opts || { - maxGasAmount: 100000000, - }, - ) + tyArgs: [], + address: this.getAddress(), + authorizer: this.account.getAuthorizer(), + opts: opts, + }) } } diff --git a/sdk/typescript/rooch-sdk/src/client/roochClient.ts b/sdk/typescript/rooch-sdk/src/client/roochClient.ts index b31ba00630..59b1e4b23f 100644 --- a/sdk/typescript/rooch-sdk/src/client/roochClient.ts +++ b/sdk/typescript/rooch-sdk/src/client/roochClient.ts @@ -177,7 +177,7 @@ export class RoochClient { if ( typeof params === 'object' && params !== null && - 'authenticator' in params && + 'authorizer' in params && 'data' in params ) { const { data, authorizer } = params as SendTransactionDataParams @@ -347,7 +347,6 @@ export class RoochClient { return 0 } - // @ts-ignore /** * Query account's sessionKey * @@ -361,12 +360,12 @@ export class RoochClient { limit: number, ): Promise> { const accessPath = `/resource/${address}/0x3::session_key::SessionKeys` - const state = await this.getStates(accessPath) + const states = await this.getStates(accessPath) - if (!state) { + if (!states || (Array.isArray(states) && states.length === 0)) { throw new Error('not found state') } - const stateView = state as any + const stateView = states as any const tableId = stateView[0].decoded_value.value.keys.value.handle.value.id diff --git a/sdk/typescript/rooch-sdk/src/constants/gas.ts b/sdk/typescript/rooch-sdk/src/constants/gas.ts index 015b2ae5dc..719ff17e3c 100644 --- a/sdk/typescript/rooch-sdk/src/constants/gas.ts +++ b/sdk/typescript/rooch-sdk/src/constants/gas.ts @@ -1,4 +1,4 @@ // Copyright (c) RoochNetwork // SPDX-License-Identifier: Apache-2.0 -export const DEFAULT_MAX_GAS_AMOUNT = 1000000 +export const DEFAULT_MAX_GAS_AMOUNT = 100000000 diff --git a/sdk/typescript/rooch-sdk/test/e2e/sdk.test.ts b/sdk/typescript/rooch-sdk/test/e2e/sdk.test.ts index 7192d1666a..81c08ab8f3 100644 --- a/sdk/typescript/rooch-sdk/test/e2e/sdk.test.ts +++ b/sdk/typescript/rooch-sdk/test/e2e/sdk.test.ts @@ -6,11 +6,11 @@ import { describe, it, expect, beforeAll, afterAll } from 'vitest' import { RoochClient, Ed25519Keypair, - PrivateKeyAuth, - Account, + RoochAccount, addressToSeqNumber, - bcsTypes, + bcs, LocalNetwork, + RoochSessionAccount, } from '../../src' import { RoochServer } from './servers/rooch-server' import { RoochCli } from './cli/rooch-cli' @@ -78,7 +78,7 @@ describe('SDK', () => { const multiChainIDEther = 60 const ethAddress = '0xd33293B247A74f9d49c1F6253d909d51242562De' - const ma = new bcsTypes.MultiChainAddress( + const ma = new bcs.MultiChainAddress( BigInt(multiChainIDEther), addressToSeqNumber(ethAddress), ) @@ -106,57 +106,25 @@ describe('SDK', () => { }) }) - describe('#runFunction', () => { + describe('#sendTransaction', () => { it('call function with private key auth should be ok', async () => { const client = new RoochClient(LocalNetwork) const kp = Ed25519Keypair.deriveKeypair( 'nose aspect organ harbor move prepare raven manage lamp consider oil front', ) - const roochAddress = kp.getPublicKey().toRoochAddress() - const authorizer = new PrivateKeyAuth(kp) - - const account = new Account(client, roochAddress, authorizer) + const account = new RoochAccount(client, kp) expect(account).toBeDefined() - const tx = await account.runFunction( + const tx = await account.sendTransaction( '0x3::account::create_account_entry', - [], [ { type: 'Address', - value: roochAddress, + value: account.getAddress(), }, ], - { - maxGasAmount: 100000000, - }, - ) - - expect(tx).toBeDefined() - }) - - it('call function with struct be ok', async () => { - const client = new RoochClient(LocalNetwork) - - const kp = Ed25519Keypair.deriveKeypair( - 'nose aspect organ harbor move prepare raven manage lamp consider oil front', - ) - const roochAddress = kp.getPublicKey().toRoochAddress() - const authorizer = new PrivateKeyAuth(kp) - - const account = new Account(client, roochAddress, authorizer) - expect(account).toBeDefined() - - const tx = await account.runFunction( - '0x3::account::create_account_entry', [], - [ - { - type: 'Address', - value: roochAddress, - }, - ], { maxGasAmount: 100000000, }, @@ -171,21 +139,19 @@ describe('SDK', () => { const kp = Ed25519Keypair.deriveKeypair( 'nose aspect organ harbor move prepare raven manage lamp consider oil front', ) - const roochAddress = kp.getPublicKey().toRoochAddress() - const authorizer = new PrivateKeyAuth(kp) - const account = new Account(client, roochAddress, authorizer) + const account = new RoochAccount(client, kp) expect(account).toBeDefined() - const tx = await account.runFunction( + const tx = await account.sendTransaction( `${defaultAddress}::entry_function::emit_object_id`, - [], [ { type: 'ObjectID', value: '0x3134', }, ], + [], { maxGasAmount: 2000000, }, @@ -200,15 +166,12 @@ describe('SDK', () => { const kp = Ed25519Keypair.deriveKeypair( 'nose aspect organ harbor move prepare raven manage lamp consider oil front', ) - const roochAddress = kp.getPublicKey().toRoochAddress() - const authorizer = new PrivateKeyAuth(kp) - const account = new Account(client, roochAddress, authorizer) + const account = new RoochAccount(client, kp) expect(account).toBeDefined() - const tx = await account.runFunction( + const tx = await account.sendTransaction( `${defaultAddress}::entry_function::emit_object`, - [], [ { type: 'Object', @@ -219,6 +182,7 @@ describe('SDK', () => { }, }, ], + [], { maxGasAmount: 2000000, }, @@ -233,21 +197,19 @@ describe('SDK', () => { const kp = Ed25519Keypair.deriveKeypair( 'nose aspect organ harbor move prepare raven manage lamp consider oil front', ) - const roochAddress = kp.getPublicKey().toRoochAddress() - const authorizer = new PrivateKeyAuth(kp) - const account = new Account(client, roochAddress, authorizer) + const account = new RoochAccount(client, kp) expect(account).toBeDefined() - const tx = await account.runFunction( + const tx = await account.sendTransaction( `${defaultAddress}::entry_function::emit_vec_u8`, - [], [ { type: 'Raw', value: arrayify('0xffff'), }, ], + [], { maxGasAmount: 2000000, }, @@ -260,15 +222,12 @@ describe('SDK', () => { const client = new RoochClient(LocalNetwork) const kp = Ed25519Keypair.generate() - const roochAddress = kp.getPublicKey().toRoochAddress() - const authorizer = new PrivateKeyAuth(kp) - const account = new Account(client, roochAddress, authorizer) + const account = new RoochAccount(client, kp) expect(account).toBeDefined() - const tx = await account.runFunction( + const tx = await account.sendTransaction( `${defaultAddress}::fixed_supply_coin::faucet`, - [], [ { type: 'Object', @@ -279,6 +238,7 @@ describe('SDK', () => { }, }, ], + [], { maxGasAmount: 200000000, }, @@ -286,34 +246,30 @@ describe('SDK', () => { expect(tx).toBeDefined() - const fscBalance = await account.coinBalance(`${defaultAddress}::fixed_supply_coin::FSC`) - expect(fscBalance).toBe(BigInt(10000)) + const fscBalance = await account.getBalance(`${defaultAddress}::fixed_supply_coin::FSC`) + expect(fscBalance.balance).toBe('10000') }) }) describe('#getTransactions', () => { it('get transaction by index should be ok', async () => { - /* const client = new RoochClient() const kp = Ed25519Keypair.deriveKeypair( 'nose aspect organ harbor move prepare raven manage lamp consider oil front', ) - const roochAddress = kp.getPublicKey().toRoochAddress() - const authorizer = new PrivateKeyAuth(kp) - - const account = new Account(client, roochAddress, authorizer) + const account = new RoochAccount(client, kp) expect(account).toBeDefined() - const tx = await account.runFunction( + const tx = await account.sendTransaction( '0x3::account::create_account_entry', - [], [ { type: 'Address', - value: roochAddress, + value: account.getAddress(), }, ], + [], { maxGasAmount: 2000000, }, @@ -321,9 +277,8 @@ describe('SDK', () => { expect(tx).toBeDefined() - const result = client.getTransactionsByHash([tx]) + const result = client.getTransactionsByHashes([tx]) expect(result).toBeDefined() - */ }) }) @@ -335,51 +290,25 @@ describe('SDK', () => { }) }) - describe('#sessionKey', () => { - it('Create session account by registerSessionKey should be ok', async () => { + describe('#sessionAccount', () => { + it('Create session account should be ok', async () => { const client = new RoochClient(LocalNetwork) - const kp = Ed25519Keypair.deriveKeypair( - 'fiber tube acid imitate frost coffee choose crowd grass topple donkey submit', - ) - const roochAddress = kp.getPublicKey().toRoochAddress() - const authorizer = new PrivateKeyAuth(kp) - - const account = new Account(client, roochAddress, authorizer) - expect(account).toBeDefined() - - // create session account - const kp2 = Ed25519Keypair.generate() - await account.registerSessionKey( - kp2.getPublicKey().toRoochAddress(), + const account = new RoochAccount(client) + const sessionAccount = await RoochSessionAccount.CREATE( + client, + account, ['0x3::empty::empty'], 100, ) - const auth = new PrivateKeyAuth(kp2) - const sessionAccount = new Account(client, roochAddress, auth) + expect(sessionAccount).toBeDefined() - // view session Keys - const sessionKey = kp2.getPublicKey().toRoochAddress() - const session = await client.executeViewFunction({ - funcId: '0x3::session_key::get_session_key', - tyArgs: [], - args: [ - { - type: 'Address', - value: roochAddress, - }, - { - type: { Vector: 'U8' }, - value: addressToSeqNumber(sessionKey), - }, - ], - }) + // view session Key + const session = await sessionAccount.getSessionKey() expect(session).toBeDefined() // run function with sessoin key - const tx = await sessionAccount.runFunction('0x3::empty::empty', [], [], { - maxGasAmount: 200000000, - }) + const tx = await sessionAccount.sendTransaction('0x3::empty::empty', [], []) expect(tx).toBeDefined() }) @@ -390,30 +319,21 @@ describe('SDK', () => { const kp = Ed25519Keypair.deriveKeypair( 'fiber tube acid imitate frost coffee choose crowd grass topple donkey submit', ) - const roochAddress = kp.getPublicKey().toRoochAddress() - const authorizer = new PrivateKeyAuth(kp) - - const account = new Account(client, roochAddress, authorizer) - expect(account).toBeDefined() - // create session account - const kp2 = Ed25519Keypair.generate() - await account.registerSessionKey( - kp2.getPublicKey().toRoochAddress(), + const sessionAccount = await RoochSessionAccount.CREATE( + client, + new RoochAccount(client, kp), ['0x3::empty::empty'], 100, ) - const auth = new PrivateKeyAuth(kp2) - const sessionAccount = new Account(client, roochAddress, auth) + expect(sessionAccount).toBeDefined() // check session key expired - const expired = await account.isSessionKeyExpired(kp2.getPublicKey().toRoochAddress()) + const expired = await sessionAccount.isExpired() expect(expired).toBeFalsy() - // run function with sessoin key - const tx = await sessionAccount.runFunction('0x3::empty::empty', [], [], { - maxGasAmount: 200000000, - }) + // run function with session + const tx = await sessionAccount.sendTransaction('0x3::empty::empty', [], []) expect(tx).toBeDefined() }) @@ -421,109 +341,45 @@ describe('SDK', () => { it('Remove session key should be ok', async () => { const client = new RoochClient(LocalNetwork) - const kp = Ed25519Keypair.deriveKeypair( - 'fiber tube acid imitate frost coffee choose crowd grass topple donkey submit', - ) - const roochAddress = kp.getPublicKey().toRoochAddress() - const authorizer = new PrivateKeyAuth(kp) - - const account = new Account(client, roochAddress, authorizer) - expect(account).toBeDefined() - - // create session account - const kp2 = Ed25519Keypair.generate() - await account.registerSessionKey( - kp2.getPublicKey().toRoochAddress(), + // create session key + const account = new RoochAccount(client) + const sessionAccount = await RoochSessionAccount.CREATE( + client, + account, ['0x3::empty::empty'], 100, ) - - // view session Keys - const sessionKey = kp2.getPublicKey().toRoochAddress() - const session = await client.executeViewFunction({ - funcId: '0x3::session_key::get_session_key', - tyArgs: [], - args: [ - { - type: 'Address', - value: roochAddress, - }, - { - type: { Vector: 'U8' }, - value: addressToSeqNumber(sessionKey), - }, - ], - }) - expect(session).toBeDefined() - expect(session.return_values![0].value.value).not.toBe('0x00') - - // run function with sessoin key - const tx = await account.removeSessionKey(sessionKey) - expect(tx).toBeDefined() - - // view session Keys - const session2 = await client.executeViewFunction({ - funcId: '0x3::session_key::get_session_key', - tyArgs: [], - args: [ - { - type: 'Address', - value: roochAddress, - }, - { - type: { Vector: 'U8' }, - value: addressToSeqNumber(sessionKey), - }, - ], - }) - - expect(session2).toBeDefined() - expect(session2.return_values![0].value.value).toBe('0x00') - }) - - it('Create session account by createSessionAccount should be ok', async () => { - const client = new RoochClient(LocalNetwork) - - const kp = Ed25519Keypair.generate() - const roochAddress = kp.getPublicKey().toRoochAddress() - const authorizer = new PrivateKeyAuth(kp) - - const account = new Account(client, roochAddress, authorizer) - expect(account).toBeDefined() - - // create session account - const sessionAccount = await account.createSessionAccount(['0x3::empty::empty'], 100) expect(sessionAccount).toBeDefined() - // run function with sessoin key - const tx = await sessionAccount.runFunction('0x3::empty::empty', [], [], { - maxGasAmount: 200000000, - }) + // view session key + const sessionKey = await sessionAccount.getSessionKey() + expect(sessionKey).toBeDefined() + expect(sessionKey.return_values![0].value.value).not.toBe('0x00') + // destroy session + const tx = await sessionAccount.destroy() expect(tx).toBeDefined() + + // view session key + const sessionKey2 = await sessionAccount.getSessionKey() + expect(sessionKey2).toBeDefined() + expect(sessionKey2.return_values![0].value.value).toBe('0x00') }) it('Create session account with multi scopes should be ok', async () => { const client = new RoochClient(LocalNetwork) - const kp = Ed25519Keypair.generate() - const roochAddress = kp.getPublicKey().toRoochAddress() - const authorizer = new PrivateKeyAuth(kp) - - const account = new Account(client, roochAddress, authorizer) - expect(account).toBeDefined() - - // create session account - const sessionAccount = await account.createSessionAccount( + const account = new RoochAccount(client) + const sessionAccount = await RoochSessionAccount.CREATE( + client, + account, ['0x3::empty::empty', '0x1::*::*'], 100, ) expect(sessionAccount).toBeDefined() - // run function with sessoin key - const tx = await sessionAccount.runFunction('0x3::empty::empty', [], [], { - maxGasAmount: 200000000, - }) + // run function with session + const tx = await sessionAccount.sendTransaction('0x3::empty::empty', [], []) expect(tx).toBeDefined() }) @@ -531,41 +387,40 @@ describe('SDK', () => { it('Session account runFunction out of score should fail', async () => { const client = new RoochClient(LocalNetwork) - const kp = Ed25519Keypair.generate() - const roochAddress = kp.getPublicKey().toRoochAddress() - const authorizer = new PrivateKeyAuth(kp) - - const account = new Account(client, roochAddress, authorizer) - expect(account).toBeDefined() - - // create session account - const sessionAccount = await account.createSessionAccount(['0x2::account::*'], 100) + const account = new RoochAccount(client) + const sessionAccount = await RoochSessionAccount.CREATE( + client, + account, + ['0x2::account::*'], + 100, + ) expect(sessionAccount).toBeDefined() + + try { + const tx = await sessionAccount.sendTransaction('0x3::empty::empty', [], []) + expect(tx).toBeUndefined() + } catch (e) { + expect(e).toBeDefined() + } }) it('Query session keys should be ok', async () => { const client = new RoochClient(LocalNetwork) - const kp = Ed25519Keypair.generate() - const roochAddress = kp.getPublicKey().toRoochAddress() - const authorizer = new PrivateKeyAuth(kp) - - const account = new Account(client, roochAddress, authorizer) - expect(account).toBeDefined() - - //TODO for loop to check the timestamp is updated, or wait for timestamp sync when start rooch server - // wait timestamp sync - await new Promise((resolve) => setTimeout(resolve, 10000)) - - // create session account - const sessionAccount = await account.createSessionAccount( + const account = new RoochAccount(client) + const sessionAccount = await RoochSessionAccount.CREATE( + client, + account, ['0x3::empty::empty', '0x1::*::*'], 100, ) expect(sessionAccount).toBeDefined() + // wait timestamp sync + await new Promise((resolve) => setTimeout(resolve, 10000)) + // query session Keys - const page = await account.querySessionKeys(null, 10) + const page = await sessionAccount.querySessionKeys(null, 10) expect(page).toBeDefined() expect(page.hasNextPage).toBeFalsy() expect(page.nextCursor).toBeDefined()