Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSSDK: remove wasm dependencies & optimized the bundling size #1417

Merged
merged 9 commits into from
Oct 20, 2023
36 changes: 26 additions & 10 deletions frontend/packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
{
"name": "@phala/sdk",
"version": "0.5.3-nightly-20231013",
"version": "0.5.4-nightly-20231020",
"description": "Phala Phat Contract JS SDK",
"license": "Apache-2.0",
"homepage": "https://github.com/Phala-Network/js-sdk/tree/main/packages/sdk#readme",
"author": [
"Leechael Yim <[email protected]>"
],
"homepage": "https://github.com/Phala-Network/phala-blockchain/tree/master/frontend/packages/sdk#readme",
"repository": {
"type": "git",
"url": "https://github.com/Phala-Network/js-sdk.git",
"directory": "packages/sdk"
"url": "https://github.com/Phala-Network/phala-blockchain.git",
"directory": "frontend/packages/sdk"
},
"bugs": {
"url": "https://github.com/Phala-Network/phala-blockchain/issues"
},
"main": "dist/node/index",
"browser": "dist/browser/index",
"keywords": [
"phala",
"phala-network",
"phat-contract"
],
"exports": {
".": {
"import": {
Expand All @@ -24,7 +35,10 @@
}
},
"files": [
"dist/*"
"dist/*/*.js",
"dist/*/*.ts",
"dist/*/*.mjs",
"dist/*/*.mts"
],
"scripts": {
"prebuild": "shx rm -rf dist",
Expand All @@ -38,19 +52,21 @@
"eslint": "eslint --fix --no-eslintrc --c .eslintrc '*.{js,json,ts}' '{src,tests,benchmarks}/**/*.{ts,tsx}'",
"eslint:ci": "eslint --no-eslintrc --c .eslintrc '*.{js,json,ts}' '{src,tests}/**/*.{ts,tsx}'",
"test": "vitest --ui --coverage",
"test:ci": "vitest"
"test:ci": "vitest --run"
},
"dependencies": {
"@phala/typedefs": "^0.2.33",
"@polkadot/api": "^10.9.1",
"@polkadot/api-contract": "^10.9.1",
"@polkadot/keyring": "^12.3.2",
"@polkadot/types": "^10.9.1",
"@polkadot/types-augment": "^10.9.1",
"@polkadot/util": "^12.3.2",
"@polkadot/util-crypto": "^12.3.2",
"crypto-browserify": "^3.12.0",
"browserify-cipher": "^1.0.1",
"cross-fetch": "^4.0.0",
"protobufjs": "^7.2.4",
"undici": "^5.22.1",
"randombytes": "^2.1.0",
"rxjs": "^7.8.1",
"viem": "^1.5.0"
},
"devDependencies": {
Expand All @@ -77,7 +93,7 @@
"ts-node": "^10.9.1",
"tsup": "^7.1.0",
"typescript": "^5.1.6",
"vitest": "^0.33.0"
"vitest": "^0.34.6"
},
"engines": {
"node": ">=16"
Expand Down
27 changes: 27 additions & 0 deletions frontend/packages/sdk/pbconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"messageNames": [
"google.protobuf.Empty",
"prpc.PrpcError",
"pruntime_rpc.PhactoryAPI.GetInfo",
"pruntime_rpc.PhactoryInfo",
"pruntime_rpc.PhactoryAPI.ContractQuery",
"pruntime_rpc.ContractQueryRequest",
"pruntime_rpc.ContractQueryResponse",
"pruntime_rpc.PhactoryAPI.GetContractInfo",
"pruntime_rpc.GetContractInfoRequest",
"pruntime_rpc.GetContractInfoResponse",
"pruntime_rpc.PhactoryAPI.GetRuntimeInfo",
"pruntime_rpc.GetRuntimeInfoRequest",
"pruntime_rpc.InitRuntimeResponse",
"pruntime_rpc.PhactoryAPI.UploadSidevmCode",
"pruntime_rpc.SidevmCode",
"pruntime_rpc.PhactoryAPI.CalculateContractId",
"pruntime_rpc.ContractParameters",
"pruntime_rpc.ContractId",
"pruntime_rpc.PhactoryAPI.AddEndpoint",
"pruntime_rpc.AddEndpointRequest",
"pruntime_rpc.GetEndpointResponse",
"pruntime_rpc.Certificate",
"pruntime_rpc.SignatureType"
]
}
2 changes: 1 addition & 1 deletion frontend/packages/sdk/scripts/build_proto.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ downloadProto "pruntime_rpc"

echo "Generating static code from proto files"
rm -rf src/pruntime/proto/*
node_modules/.bin/pbjs -w commonjs -t static-module -o src/pruntime/proto/index.js proto/*.proto
node_modules/.bin/pbjs -w commonjs -t static-module --filter pbconfig.json -o src/pruntime/proto/index.js proto/*.proto
node_modules/.bin/pbts -o src/pruntime/proto/index.d.ts src/pruntime/proto/index.js

echo "Done"
5 changes: 3 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 Expand Up @@ -213,6 +213,7 @@ export class OnChainRegistry {
try {
await this.#phactory.getInfo({})
} catch (err) {
console.error(err)
throw new Error(
'Phactory API not compatible, you might need downgrade your @phala/sdk or connect to an up-to-date endpoint.'
)
Expand Down
2 changes: 1 addition & 1 deletion frontend/packages/sdk/src/abis/fetchers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type Bool } from '@polkadot/types'
import { fetch } from 'undici'
import fetch from 'cross-fetch'
import { type PinkContractPromise } from '../contracts/PinkContract'
import { type OnChainRegistry } from '../OnChainRegistry'
import { type CertificateData } from '../pruntime/certificate'
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
8 changes: 6 additions & 2 deletions frontend/packages/sdk/src/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
declare module 'crypto-browserify' {
export * from 'crypto'
declare module 'browserify-cipher' {
export { createCipheriv, createDecipheriv } from 'crypto'
}

declare module 'randombytes' {
export { randombytes as default } from 'crypto'
}
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 { cryptoWaitReady, decodeAddress, sr25519PairFromSeed } 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'
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
2 changes: 1 addition & 1 deletion frontend/packages/sdk/src/pruntime/createPruntimeClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fetch } from 'undici'
import fetch from 'cross-fetch'
import { prpc, pruntime_rpc as pruntimeRpc } from './proto'

/**
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
Loading
Loading