Skip to content

Commit

Permalink
refactor(jssdk): use util-crypto instead of wasm-crypto
Browse files Browse the repository at this point in the history
  • Loading branch information
Leechael committed Oct 19, 2023
1 parent 5ba7387 commit 53bd7c5
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 22 deletions.
4 changes: 2 additions & 2 deletions frontend/packages/sdk/src/OnChainRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions frontend/packages/sdk/src/contracts/PinkBlueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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') {
Expand Down
8 changes: 4 additions & 4 deletions frontend/packages/sdk/src/contracts/PinkContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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<ContractCallOutcome> => {
if (typeof origin === 'string') {
Expand Down
4 changes: 2 additions & 2 deletions frontend/packages/sdk/src/contracts/PinkLoggerContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -165,7 +165,7 @@ function sidevmQueryWithReader({ phactory, remotePubkey, address, cert }: Sidevm
return async function unsafeRunSidevmQuery<T>(sidevmMessage: Record<string, any>): Promise<T> {
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>('InkResponse', response)
if (inkResponse.result.isErr) {
Expand Down
12 changes: 6 additions & 6 deletions frontend/packages/sdk/src/pruntime/certificate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Check failure on line 6 in frontend/packages/sdk/src/pruntime/certificate.ts

View workflow job for this annotation

GitHub Actions / test-jssdk

Member 'cryptoWaitReady' of the import declaration should be sorted alphabetically
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'
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -84,7 +84,7 @@ function CertificateBody(pubkey: string, ttl: number, config_bits: number = 0) {
}

export async function signCertificate(params: CertificateParams): Promise<CertificateData> {
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.'
Expand Down Expand Up @@ -162,7 +162,7 @@ export async function unstable_signEip712Certificate({
compactPubkey: string
ttl?: number
}): Promise<CertificateData> {
await waitReady()
await cryptoWaitReady()
const [secret, pubkey] = generatePair()
const address = account.address || account
const eip712Cert = CertificateBody(u8aToHex(pubkey), ttl)
Expand Down
4 changes: 2 additions & 2 deletions frontend/packages/sdk/src/pruntime/coders.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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)),
Expand Down
5 changes: 3 additions & 2 deletions frontend/packages/sdk/src/pruntime/pinkQuery.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 53bd7c5

Please sign in to comment.