Skip to content

Commit

Permalink
Merge branch 'main' into feat.ord_index_check
Browse files Browse the repository at this point in the history
  • Loading branch information
popcnt1 authored Jul 6, 2024
2 parents 5493449 + a4fee7b commit 2b79f09
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 38 deletions.
4 changes: 2 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions sdk/typescript/build-scripts/src/utils/buildPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { execSync } from 'child_process'
import { existsSync, promises as fs } from 'fs'
import * as path from 'path'
import { build, BuildOptions } from 'esbuild'
// import { polyfillNode } from 'esbuild-plugin-polyfill-node'

interface PackageJSON {
name?: string
Expand Down Expand Up @@ -57,6 +58,11 @@ async function buildCJS(
logLevel: 'error',
target: 'es2020',
entryPoints,
// plugins: [
// polyfillNode({
// globals: { process: true },
// }),
// ],
outdir: 'dist/cjs',
sourcemap: true,
...buildOptions,
Expand Down Expand Up @@ -87,6 +93,11 @@ async function buildESM(
format: 'esm',
logLevel: 'error',
target: 'es2020',
// plugins: [
// polyfillNode({
// globals: { process: true },
// }),
// ],
entryPoints,
outdir: 'dist/esm',
sourcemap: true,
Expand Down
4 changes: 2 additions & 2 deletions sdk/typescript/rooch-sdk-kit/src/feature/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

export type SupportChain = 'rooch' | 'bitcoin' | 'eth'
export type SupportWallet = 'metamask' | 'unisat' | 'okx'
export type SupportChain = 'rooch' | 'bitcoin'
export type SupportWallet = 'unisat' | 'okx' | 'onekey' //| 'onekey-hardware'
4 changes: 2 additions & 2 deletions sdk/typescript/rooch-sdk-kit/src/provider/walletProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import {
} from '../hooks/index.js'
import { useSessionStore } from '../hooks/useSessionsStore.js'
import { getDefaultStorage, StorageType, checkWallets } from '../utils/index.js'
import { SupportChain } from '../feature/index.js'
import { SupportChain, SupportWallet } from '../feature/index.js'
import { getRegisteredWallets } from '../wellet/util.js'
import { getWallets } from '../wellet/wallets.js'
import { useWalletChanged } from '../hooks/index.js'

type WalletProviderProps = {
preferredWallets?: string[]
preferredWallets?: SupportWallet[]

chain?: SupportChain

Expand Down
17 changes: 13 additions & 4 deletions sdk/typescript/rooch-sdk-kit/src/utils/walletUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@
// SPDX-License-Identifier: Apache-2.0

import { SupportChain } from '../feature/index.js'
import { UniSatWallet, Wallet, OkxWallet } from '../wellet/index.js'
import {
Wallet,
UniSatWallet,
OkxWallet,
OnekeyWallet,
// OnekeyHardwareWallet,
} from '../wellet/index.js'

export async function checkWallets(filter?: SupportChain) {
const wallets: Wallet[] = [new UniSatWallet(), new OkxWallet()].filter(
(wallet) => wallet.getChain() === filter || !filter,
)
const wallets: Wallet[] = [
new UniSatWallet(),
new OkxWallet(),
new OnekeyWallet(),
// new OnekeyHardwareWallet(),
].filter((wallet) => wallet.getChain() === filter || !filter)

return await Promise.all(wallets.filter(async (w) => await w.checkInstalled()))
}
1 change: 1 addition & 0 deletions sdk/typescript/rooch-sdk-kit/src/wellet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './wallet.js'
export * from './bitcoin.js'
export * from './unisat.js'
export * from './okx.js'
export * from './onekey.js'
2 changes: 1 addition & 1 deletion sdk/typescript/rooch-sdk-kit/src/wellet/okx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class OkxWallet extends BitcoinWallet {
async connect(): Promise<ThirdPartyAddress[]> {
const obj = await this.getTarget().connect()
this.currentAddress = new BitcoinAddress(obj.address)
this.publicKey = obj.compressedPublicKey
this.publicKey = obj.compressedPublicKey !== '' ? obj.compressedPublicKey : obj.publicKey
this.address = [this.currentAddress]

return this.address
Expand Down
101 changes: 101 additions & 0 deletions sdk/typescript/rooch-sdk-kit/src/wellet/onekey-hardware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// // Copyright (c) RoochNetwork
// // SPDX-License-Identifier: Apache-2.0
//
// import onekey from '@onekeyfe/hd-web-sdk'
//
// import { BitcoinAddress, Bytes, ThirdPartyAddress, str, bytes } from '@roochnetwork/rooch-sdk'
// import { BitcoinWallet } from '../wellet/index.js'
//
// type Device = {
// connectId: string | null
// uuid: string
// deviceId: string | null
// deviceType: string
// name: string
// }
//
// export class OnekeyHardwareWallet extends BitcoinWallet {
// devices?: Device[]
//
// getName(): string {
// return 'onekey-hardware'
// }
//
// async sign(msg: Bytes): Promise<Bytes> {
// const msgStr = str('utf8', msg)
// const sign = await this.getTarget().signMessage(msgStr)
// return bytes('base64', sign).subarray(1)
// }
//
// getTarget(): any {
// return undefined
// }
//
// async connect(): Promise<ThirdPartyAddress[]> {
// if (this.devices && this.devices.length > 0 && !this.currentAddress) {
// await onekey.HardwareWebSdk.init({
// debug: true,
// })
// }
//
// const { connectId, deviceId } = this.devices![0]
//
// const address = await onekey.HardwareWebSdk.btcGetAddress(connectId!, deviceId!, {
// path: `m/86'/0'/x'/x/x`, // taproot
// coin: 'btc',
// })
// if (!address.success) {
// throw Error(address.payload.error)
// }
//
// const publicKey = await onekey.HardwareWebSdk.btcGetPublicKey(connectId!, deviceId!, {
// path: `m/86'/0'/x'/x/x`,
// coin: 'btc',
// })
//
// if (!publicKey.success) {
// throw Error(publicKey.payload.error)
// }
//
// this.address = [new BitcoinAddress(address.payload.address)]
// this.currentAddress = this.address[0]
// this.publicKey = publicKey.payload.xpub
//
// return this.address
// }
//
// switchNetwork(): void {
// this.getTarget().switchNetwork()
// }
//
// getNetwork(): string {
// return this.getTarget().getNetwork()
// }
//
// getSupportNetworks(): string[] {
// return ['livenet']
// }
//
// onAccountsChanged(callback: (account: string[]) => void): void {
// this.getTarget().on('accountsChanged', callback)
// }
//
// removeAccountsChanged(callback: (account: string[]) => void): void {
// this.getTarget().removeListener('accountsChanged', callback)
// }
//
// onNetworkChanged(callback: (network: string) => void): void {
// this.getTarget().on('networkChanged', callback)
// }
//
// removeNetworkChanged(callback: (network: string) => void): void {
// this.getTarget().removeListener('networkChanged', callback)
// }
//
// async checkInstalled(): Promise<boolean> {
// // TODO: check feature, Ignore wallets that do not support bitcoin
// const devices = await onekey.HardwareWebSdk.searchDevices()
// this.devices = devices.success ? (devices.payload as []) : []
// return devices.success
// }
// }
66 changes: 66 additions & 0 deletions sdk/typescript/rooch-sdk-kit/src/wellet/onekey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

import { BitcoinAddress, Bytes, ThirdPartyAddress, str, bytes } from '@roochnetwork/rooch-sdk'
import { BitcoinWallet } from '../wellet/index.js'

export class OnekeyWallet extends BitcoinWallet {
getName(): string {
return 'onekey'
}

async sign(msg: Bytes): Promise<Bytes> {
const msgStr = str('utf8', msg)
const sign = await this.getTarget().signMessage(msgStr)
return bytes('base64', sign).subarray(1)
}

getTarget(): any {
return (window as any).$onekey?.btc
}

async connect(): Promise<ThirdPartyAddress[]> {
let addresses: string[] = await this.getTarget().getAccounts()

if (!addresses || addresses.length === 0) {
await this.getTarget().requestAccounts()
return this.connect()
}

let publicKey = await this.getTarget().getPublicKey()

this.address = addresses.map((item) => new BitcoinAddress(item))
this.currentAddress = this.address[0]
this.publicKey = publicKey

return this.address
}

switchNetwork(): void {
this.getTarget().switchNetwork()
}

getNetwork(): string {
return this.getTarget().getNetwork()
}

getSupportNetworks(): string[] {
return ['livenet']
}

onAccountsChanged(callback: (account: string[]) => void): void {
this.getTarget().on('accountsChanged', callback)
}

removeAccountsChanged(callback: (account: string[]) => void): void {
this.getTarget().removeListener('accountsChanged', callback)
}

onNetworkChanged(callback: (network: string) => void): void {
this.getTarget().on('networkChanged', callback)
}

removeNetworkChanged(callback: (network: string) => void): void {
this.getTarget().removeListener('networkChanged', callback)
}
}
26 changes: 0 additions & 26 deletions sdk/typescript/rooch-sdk-kit/src/wellet/type.ts

This file was deleted.

1 change: 1 addition & 0 deletions sdk/typescript/rooch-sdk-kit/tsconfig.esm.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"target": "ESNext",
"module": "Node16",
"moduleResolution": "Node16",
"isolatedModules": true,
"outDir": "dist/esm"
}
}
2 changes: 1 addition & 1 deletion sdk/typescript/templates/react-counter/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ ReactDOM.createRoot(document.getElementById("root")!).render(
<Theme appearance="dark">
<QueryClientProvider client={queryClient}>
<RoochProvider networks={networkConfig} defaultNetwork='localnet'>
<WalletProvider preferredWallets={['okx']} chain={'bitcoin'} autoConnect>
<WalletProvider preferredWallets={['onekey-hardware']} chain={'bitcoin'} autoConnect>
<App/>
</WalletProvider>
</RoochProvider>
Expand Down

0 comments on commit 2b79f09

Please sign in to comment.