Skip to content

Commit

Permalink
Merge pull request #1456 from Phala-Network/imp-jssk-ink-command-nonce
Browse files Browse the repository at this point in the history
JSSDK: optional nonce in trx and access the trx nonce in SubmittableResult
  • Loading branch information
Leechael authored Dec 6, 2023
2 parents 2903ded + 61ecbdc commit b98d674
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
19 changes: 17 additions & 2 deletions frontend/packages/sdk/src/contracts/PinkContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { applyOnEvent } from '@polkadot/api-contract/util'
import type { Bytes, Null, Result, Struct, Text, Vec, u8 } from '@polkadot/types'
import type { AccountId, ContractExecResult, EventRecord } from '@polkadot/types/interfaces'
import type { Codec, IEnum, IKeyringPair, ISubmittableResult, Registry } from '@polkadot/types/types'
import { BN, BN_ZERO, hexAddPrefix, hexToU8a } from '@polkadot/util'
import { BN, BN_ZERO, hexAddPrefix, hexToU8a, isHex } from '@polkadot/util'
import { sr25519Agreement, sr25519PairFromSeed } from '@polkadot/util-crypto'
import { from } from 'rxjs'
import type { OnChainRegistry } from '../OnChainRegistry'
Expand Down Expand Up @@ -59,6 +59,8 @@ export interface PinkContractOptions extends ContractOptions {

//
plain?: boolean

nonce?: `0x${string}`
}

interface SendOptions {
Expand Down Expand Up @@ -91,10 +93,12 @@ class PinkContractSubmittableResult extends ContractSubmittableResult {
#isFinalized: boolean = false
#contract: PinkContractPromise
#message: AbiMessage
#nonce: string

constructor(
registry: OnChainRegistry,
contract: PinkContractPromise,
nonce: string,
message: AbiMessage,
result: ISubmittableResult,
contractEvents?: DecodedEvent[]
Expand All @@ -103,6 +107,11 @@ class PinkContractSubmittableResult extends ContractSubmittableResult {
this.#registry = registry
this.#contract = contract
this.#message = message
this.#nonce = nonce
}

get nonce() {
return this.#nonce
}

protected async throwsOnErrorLog(chainHeight: number): Promise<void> {
Expand Down Expand Up @@ -329,7 +338,8 @@ export class PinkContractPromise<

if (!options.cert) {
throw new Error(
'You need to provide the `cert` parameter in the options to process a Phat Contract query. Please check the document for a more detailed code snippet: https://www.npmjs.com/package/@phala/sdk'
'You need to provide the `cert` parameter in the options to process a Phat Contract query. ' +
'Please check the document for a more detailed code snippet: https://www.npmjs.com/package/@phala/sdk'
)
}

Expand Down Expand Up @@ -400,11 +410,14 @@ export class PinkContractPromise<
options: PinkContractOptions,
params: unknown[]
): SubmittableExtrinsic<'promise'> => {
options.nonce && assert(isHex(options.nonce) && options.nonce.length === 66, 'Invalid nonce provided')
const nonce = options.nonce || hexAddPrefix(randomHex(32))
const command = options.plain ? PlainInkCommand : EncryptedInkCommand
const message = this.abi.findMessage(messageOrId)
const payload = command(
this.contractKey,
message.toU8a(params),
nonce,
options.value,
convertWeight(options.gasLimit || BN_ZERO).v2Weight,
options.storageDepositLimit
Expand All @@ -415,6 +428,7 @@ export class PinkContractPromise<
return new PinkContractSubmittableResult(
this.phatRegistry,
this,
nonce,
message,
result,
applyOnEvent(result, ['ContractEmitted', 'ContractExecution'], (records: EventRecord[]) => {
Expand Down Expand Up @@ -446,6 +460,7 @@ export class PinkContractPromise<
value: options.value,
storageDepositLimit: options.storageDepositLimit,
plain: options.plain,
nonce: options.nonce,
}

const tx = this.#tx[messageOrId]
Expand Down
15 changes: 13 additions & 2 deletions frontend/packages/sdk/src/contracts/PinkLoggerContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,15 @@ export interface GetLogRequest {
count: number
block_number?: number
type?: LogTypeLiteral | LogTypeLiteral[]

// Event type logs specified filter.
topic?: LiteralTopic

// Use for decode event data
abi?: AbiLike

// MessageOutput type logs specified filter.
nonce?: string
}

export interface SerMessageLog {
Expand Down Expand Up @@ -416,7 +423,7 @@ export class PinkLoggerContractPromise {
async tail(counts: number, request: Omit<GetLogRequest, 'from' | 'count'>): Promise<GetLogResponse>
async tail(counts: number, from: number, request?: Omit<GetLogRequest, 'from' | 'count'>): Promise<GetLogResponse>
async tail(...params: any[]): Promise<GetLogResponse> {
const { abi, type, topic, ...request }: GetLogRequest = buildGetLogRequest(
const { abi, type, topic, nonce, ...request }: GetLogRequest = buildGetLogRequest(
params,
(x) => {
if (!x.from) {
Expand All @@ -433,6 +440,8 @@ export class PinkLoggerContractPromise {
} else if (type === 'Event' && topic) {
const topicHash = getTopicHash(topic)
result.records = result.records.filter((record) => record.type === type && record.topics[0] === topicHash)
} else if (type === 'MessageOutput' && nonce) {
result.records = result.records.filter((record) => record.type === type && record.nonce === nonce)
} else {
result.records = result.records.filter((record) => record.type === type)
}
Expand All @@ -450,7 +459,7 @@ export class PinkLoggerContractPromise {
async head(counts: number, request: Omit<GetLogRequest, 'from' | 'count'>): Promise<GetLogResponse>
async head(counts: number, from: number, request?: Omit<GetLogRequest, 'from' | 'count'>): Promise<GetLogResponse>
async head(...params: any[]): Promise<GetLogResponse> {
const { abi, type, topic, ...request }: GetLogRequest = buildGetLogRequest(
const { abi, type, topic, nonce, ...request }: GetLogRequest = buildGetLogRequest(
params,
(x) => x.from || 0,
() => ({ from: 0, count: 10 })
Expand All @@ -462,6 +471,8 @@ export class PinkLoggerContractPromise {
} else if (type === 'Event' && topic) {
const topicHash = getTopicHash(topic)
result.records = result.records.filter((record) => record.type === type && record.topics[0] === topicHash)
} else if (type === 'MessageOutput' && nonce) {
result.records = result.records.filter((record) => record.type === type && record.nonce === nonce)
} else {
result.records = result.records.filter((record) => record.type === type)
}
Expand Down
8 changes: 4 additions & 4 deletions frontend/packages/sdk/src/pruntime/coders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ export function InkQueryInstantiate(
export function PlainInkCommand(
address: ContractAddress,
encParams: Uint8Array,
nonce: string,
value: LooseNumber | undefined,
gas: { refTime: LooseNumber },
storageDepositLimit?: LooseNumber
) {
return phalaTypes.createType('CommandPayload', {
Plain: phalaTypes.createType('InkCommand', {
InkMessage: {
nonce: hexAddPrefix(randomHex(32)),
// FIXME: unexpected u8a prefix
nonce,
message: phalaTypes.createType('Vec<u8>', encParams).toHex(),
transfer: value,
gasLimit: gas.refTime,
Expand All @@ -104,6 +104,7 @@ export function PlainInkCommand(
export function EncryptedInkCommand(
address: string,
encParams: Uint8Array,
nonce: string,
value: LooseNumber | undefined,
gas: { refTime: LooseNumber },
storageDepositLimit?: LooseNumber
Expand All @@ -112,8 +113,7 @@ export function EncryptedInkCommand(
const commandAgreementKey = sr25519Agreement(sk, hexToU8a(address))
const payload = phalaTypes.createType('InkCommand', {
InkMessage: {
nonce: hexAddPrefix(randomHex(32)),
// FIXME: unexpected u8a prefix
nonce,
message: phalaTypes.createType('Vec<u8>', encParams).toHex(),
transfer: value,
gasLimit: gas.refTime,
Expand Down

0 comments on commit b98d674

Please sign in to comment.