Skip to content

Commit

Permalink
chore: update ecdh api
Browse files Browse the repository at this point in the history
  • Loading branch information
AricRedemption committed Oct 22, 2024
1 parent 9befb33 commit c076ed5
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/lib/actions/common/ecdh.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as ecdh from 'crypto'
import { getNet } from '@/lib/network'
import { getActiveWallet } from '@/lib/wallet'
import { AddressType, BaseWallet, ScriptType } from '@metalet/utxo-wallet-service'

export async function process(
_: unknown,
{ path = "m/100'/0'/0'/0/0", externalPubKey }: { path?: string; externalPubKey: string }
) {
const network = getNet()
const activeWallet = await getActiveWallet()

const wallet = new BaseWallet({
// @ts-ignore
path,
network,
addressIndex: 0,
scriptType: ScriptType.P2PKH,
mnemonic: activeWallet.mnemonic,
addressType: AddressType.Legacy,
})

const privateKey = wallet.getPrivateKeyBuffer()

const _externalPubKey = Buffer.from(externalPubKey, 'hex')

const ecdhKey = ecdh.createECDH('secp256k1')
// @ts-ignore
ecdhKey.setPrivateKey(privateKey)

// 返回协商密钥
const sharedSecret = ecdhKey.computeSecret(_externalPubKey)

return sharedSecret.toString('hex')
}
16 changes: 16 additions & 0 deletions src/lib/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,22 @@ export async function getV3CurrentWallet() {
return wallet
}

export async function getActiveWallet() {
const currentWalletId = await getCurrentWalletId()
if (!currentWalletId) {
throw new Error('currentWalletId id not found')
}
const wallets = await getV3EncryptedWallets()
if (!wallets.length) {
throw new Error('wallets not found')
}
let wallet = wallets.find((wallet) => wallet.id === currentWalletId)
if (!wallet) {
throw new Error('wallets not found')
}
return wallet
}

export async function getWalletOnlyAccount(walletId: string, accountId: string) {
if (!walletId) {
throw new Error('wallet id not found')
Expand Down

0 comments on commit c076ed5

Please sign in to comment.