diff --git a/frontend/packages/sdk/src/OnChainRegistry.ts b/frontend/packages/sdk/src/OnChainRegistry.ts index 17548465eb..b12d46f6fc 100644 --- a/frontend/packages/sdk/src/OnChainRegistry.ts +++ b/frontend/packages/sdk/src/OnChainRegistry.ts @@ -3,7 +3,7 @@ import type { Result, U64 } from '@polkadot/types' import { Enum, Map, Option, Text, U8aFixed, Vec } from '@polkadot/types' import { AccountId } from '@polkadot/types/interfaces' import { BN } from '@polkadot/util' -import { waitReady } from '@polkadot/wasm-crypto' +import { cryptoWaitReady } from '@polkadot/util-crypto' import systemAbi from './abis/system.json' import { PinkContractPromise } from './contracts/PinkContract' import { PinkLoggerContractPromise } from './contracts/PinkLoggerContract' @@ -101,7 +101,7 @@ export class OnChainRegistry { options = { autoConnect: true, ...(options || {}) } const instance = new OnChainRegistry(api) // We should ensure the wasm & api has been initialized here. - await Promise.all([waitReady(), api.isReady]) + await Promise.all([cryptoWaitReady(), api.isReady]) if (options.autoConnect) { await instance.connect( options.clusterId, diff --git a/frontend/packages/sdk/src/contracts/PinkBlueprint.ts b/frontend/packages/sdk/src/contracts/PinkBlueprint.ts index 70938cddae..601f018e15 100644 --- a/frontend/packages/sdk/src/contracts/PinkBlueprint.ts +++ b/frontend/packages/sdk/src/contracts/PinkBlueprint.ts @@ -10,7 +10,7 @@ import { type Option } from '@polkadot/types' import type { AccountId, ContractInstantiateResult, Hash } from '@polkadot/types/interfaces' import type { IKeyringPair, ISubmittableResult } from '@polkadot/types/types' import { BN, BN_ZERO, hexAddPrefix, hexToU8a, isUndefined } from '@polkadot/util' -import { sr25519Agree, sr25519KeypairFromSeed } from '@polkadot/wasm-crypto' +import { sr25519Agreement, sr25519PairFromSeed } from '@polkadot/util-crypto' import { from } from 'rxjs' import type { OnChainRegistry } from '../OnChainRegistry' import { phalaTypes } from '../options' @@ -291,11 +291,11 @@ export class PinkBlueprintPromise { // Generate a keypair for encryption // NOTE: each instance only has a pre-generated pair now, it maybe better to generate a new keypair every time encrypting const seed = hexToU8a(hexAddPrefix(randomHex(32))) - const pair = sr25519KeypairFromSeed(seed) - const [sk, pk] = [pair.slice(0, 64), pair.slice(64)] + const pair = sr25519PairFromSeed(seed) + const [sk, pk] = [pair.secretKey, pair.publicKey] const { cert } = options - const queryAgreementKey = sr25519Agree(hexToU8a(hexAddPrefix(this.phatRegistry.remotePubkey)), sk) + const queryAgreementKey = sr25519Agreement(sk, hexToU8a(hexAddPrefix(this.phatRegistry.remotePubkey))) const inkQueryInternal = async (origin: string | AccountId | Uint8Array) => { if (typeof origin === 'string') { diff --git a/frontend/packages/sdk/src/contracts/PinkContract.ts b/frontend/packages/sdk/src/contracts/PinkContract.ts index 8ad4fdcd81..f740f54a6f 100644 --- a/frontend/packages/sdk/src/contracts/PinkContract.ts +++ b/frontend/packages/sdk/src/contracts/PinkContract.ts @@ -13,7 +13,7 @@ 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 { sr25519Agree, sr25519KeypairFromSeed } from '@polkadot/wasm-crypto' +import { sr25519Agreement, sr25519PairFromSeed } from '@polkadot/util-crypto' import { from } from 'rxjs' import type { OnChainRegistry } from '../OnChainRegistry' import type { CertificateData } from '../pruntime/certificate' @@ -338,10 +338,10 @@ export class PinkContractPromise< // Generate a keypair for encryption // NOTE: each instance only has a pre-generated pair now, it maybe better to generate a new keypair every time encrypting const seed = hexToU8a(hexAddPrefix(randomHex(32))) - const pair = sr25519KeypairFromSeed(seed) - const [sk, pk] = [pair.slice(0, 64), pair.slice(64)] + const pair = sr25519PairFromSeed(seed) + const [sk, pk] = [pair.secretKey, pair.publicKey] - const queryAgreementKey = sr25519Agree(hexToU8a(hexAddPrefix(this.phatRegistry.remotePubkey)), sk) + const queryAgreementKey = sr25519Agreement(sk, hexToU8a(hexAddPrefix(this.phatRegistry.remotePubkey))) const inkQueryInternal = async (origin: string | AccountId | Uint8Array): Promise => { if (typeof origin === 'string') { diff --git a/frontend/packages/sdk/src/contracts/PinkLoggerContract.ts b/frontend/packages/sdk/src/contracts/PinkLoggerContract.ts index 076317dd9e..d125d94f4d 100644 --- a/frontend/packages/sdk/src/contracts/PinkLoggerContract.ts +++ b/frontend/packages/sdk/src/contracts/PinkLoggerContract.ts @@ -5,7 +5,7 @@ import type { Enum, Struct, Text } from '@polkadot/types' import type { AccountId } from '@polkadot/types/interfaces' import type { Result } from '@polkadot/types-codec' import { hexAddPrefix, hexToString, hexToU8a } from '@polkadot/util' -import { sr25519Agree } from '@polkadot/wasm-crypto' +import { sr25519Agreement } from '@polkadot/util-crypto' import type { OnChainRegistry } from '../OnChainRegistry' import { phalaTypes } from '../options' import { type CertificateData, generatePair, signCertificate } from '../pruntime/certificate' @@ -165,7 +165,7 @@ function sidevmQueryWithReader({ phactory, remotePubkey, address, cert }: Sidevm return async function unsafeRunSidevmQuery(sidevmMessage: Record): Promise { const [sk, pk] = generatePair() const encodedQuery = InkQuerySidevmMessage(address, sidevmMessage) - const queryAgreementKey = sr25519Agree(hexToU8a(hexAddPrefix(remotePubkey)), sk) + const queryAgreementKey = sr25519Agreement(sk, hexToU8a(hexAddPrefix(remotePubkey))) const response = await pinkQuery(phactory, pk, queryAgreementKey, encodedQuery.toHex(), cert) const inkResponse = phalaTypes.createType('InkResponse', response) if (inkResponse.result.isErr) { diff --git a/frontend/packages/sdk/src/pruntime/certificate.ts b/frontend/packages/sdk/src/pruntime/certificate.ts index 5338c31785..c1a20af83b 100644 --- a/frontend/packages/sdk/src/pruntime/certificate.ts +++ b/frontend/packages/sdk/src/pruntime/certificate.ts @@ -3,9 +3,8 @@ import type { Signer as InjectedSigner } from '@polkadot/api/types' import type { KeyringPair } from '@polkadot/keyring/types' import type { Signer } from '@polkadot/types/types' import { hexAddPrefix, hexToU8a, u8aToHex } from '@polkadot/util' -import { decodeAddress } from '@polkadot/util-crypto' +import { decodeAddress, sr25519PairFromSeed, cryptoWaitReady } from '@polkadot/util-crypto' import { KeypairType } from '@polkadot/util-crypto/types' -import { sr25519KeypairFromSeed, waitReady } from '@polkadot/wasm-crypto' import { type Account, type Client } from 'viem' import { signTypedData } from 'viem/wallet' import { phalaTypes } from '../options' @@ -49,8 +48,9 @@ const isUsingSigner = (params: CertificateParams): params is CertificateParamsWi export function generatePair(): [Uint8Array, Uint8Array] { const generatedSeed = hexToU8a(hexAddPrefix(randomHex(32))) - const generatedPair = sr25519KeypairFromSeed(generatedSeed) - return [generatedPair.slice(0, 64), generatedPair.slice(64)] + const generatedPair = sr25519PairFromSeed(generatedSeed) + return [generatedPair.secretKey, generatedPair.publicKey] + // return [generatedPair.slice(0, 64), generatedPair.slice(64)] } function getSignatureTypeFromAccount(account: KeyringPair | InjectedAccount) { @@ -84,7 +84,7 @@ function CertificateBody(pubkey: string, ttl: number, config_bits: number = 0) { } export async function signCertificate(params: CertificateParams): Promise { - await waitReady() + await cryptoWaitReady() if (params.api) { console.warn( 'signCertificate not longer need pass the ApiPromise as parameter, it will remove from type hint in the next.' @@ -162,7 +162,7 @@ export async function unstable_signEip712Certificate({ compactPubkey: string ttl?: number }): Promise { - await waitReady() + await cryptoWaitReady() const [secret, pubkey] = generatePair() const address = account.address || account const eip712Cert = CertificateBody(u8aToHex(pubkey), ttl) diff --git a/frontend/packages/sdk/src/pruntime/coders.ts b/frontend/packages/sdk/src/pruntime/coders.ts index c31e976673..53848a3d23 100644 --- a/frontend/packages/sdk/src/pruntime/coders.ts +++ b/frontend/packages/sdk/src/pruntime/coders.ts @@ -1,7 +1,7 @@ import { U8aFixed } from '@polkadot/types' import { type AccountId } from '@polkadot/types/interfaces' import { BN, BN_ZERO, hexAddPrefix, hexToU8a, stringToHex, u8aToHex } from '@polkadot/util' -import { sr25519Agree } from '@polkadot/wasm-crypto' +import { sr25519Agreement } from '@polkadot/util-crypto' import { phalaTypes } from '../options' import { encrypt } from '../utils/aes-256-gcm' import { randomHex } from '../utils/hex' @@ -109,7 +109,7 @@ export function EncryptedInkCommand( storageDepositLimit?: LooseNumber ) { const [sk, pk] = generatePair() - const commandAgreementKey = sr25519Agree(hexToU8a(address), sk) + const commandAgreementKey = sr25519Agreement(sk, hexToU8a(address)) const payload = phalaTypes.createType('InkCommand', { InkMessage: { nonce: hexAddPrefix(randomHex(32)), diff --git a/frontend/packages/sdk/src/pruntime/pinkQuery.ts b/frontend/packages/sdk/src/pruntime/pinkQuery.ts index e43c439b6f..e41de884fe 100644 --- a/frontend/packages/sdk/src/pruntime/pinkQuery.ts +++ b/frontend/packages/sdk/src/pruntime/pinkQuery.ts @@ -1,6 +1,6 @@ import { type CodecMap } from '@polkadot/types' import { hexAddPrefix, hexToU8a, u8aToHex } from '@polkadot/util' -import { sr25519Sign } from '@polkadot/wasm-crypto' +import { sr25519Sign } from '@polkadot/util-crypto' import { phalaTypes } from '../options' import { decrypt, encrypt } from '../utils/aes-256-gcm' import { randomHex } from '../utils/hex' @@ -36,7 +36,8 @@ export async function pinkQuery( const signature: pruntimeRpc.ISignature = { signedBy: certificate, signatureType: pruntimeRpc.SignatureType.Sr25519, - signature: sr25519Sign(pubkey, secret, encodedEncryptedData), + // signature: sr25519Sign(pubkey, secret, encodedEncryptedData), + signature: sr25519Sign(encodedEncryptedData, { publicKey: pubkey, secretKey: secret }), } // Send request.