okx / js-wallet-sdk

Multi-chain typescript signature sdk, supports bitcoin, ethereum, solana, cosmos, etc.
https://okx.github.io/js-wallet-sdk/#/
MIT License
211 stars 74 forks source link

@okxweb3/coin-bitcoin genarate address is not match okx wallet ,only 【legacy】 match #109

Closed shifenhutu closed 4 months ago

shifenhutu commented 4 months ago

my code :

import { bip32, bip39 } from '@okxweb3/crypto-lib';
import { EthWallet } from '@okxweb3/coin-ethereum';
import { BtcWallet } from '@okxweb3/coin-bitcoin';

 const mnemonic = "bachelor sense wreck narrow visit morning bar insane park bridge add despair"

  let wallet_btc = new BtcWallet()
  const hdPath_btc = await wallet_btc.getDerivedPath({ index: 0 });
  console.log("hdPath_btc",hdPath_btc);
  let derivePrivateKey_btc = await wallet_btc.getDerivedPrivateKey({ mnemonic: mnemonic, hdPath: hdPath_btc });
  console.log("derivePrivateKey_btc",derivePrivateKey_btc);

  let newAddress_btc_legacy = await wallet_btc.getNewAddress({ privateKey: derivePrivateKey_btc,addressType: "Legacy" });
  let newAddress_btc_segwit_native = await wallet_btc.getNewAddress({ privateKey: derivePrivateKey_btc,  addressType: "segwit_native",});
  let newAddress_btc_segwit_nested = await wallet_btc.getNewAddress({ privateKey: derivePrivateKey_btc,addressType: "segwit_nested" });
  let newAddress_btc_segwit_taproot = await wallet_btc.getNewAddress({ privateKey: derivePrivateKey_btc,addressType: "segwit_taproot" });

  console.log("newAddress_btc_segwit_taproot:", newAddress_btc_segwit_taproot.address);
  console.log("newAddress_btc_legacy:", newAddress_btc_legacy.address);
  console.log("newAddress_btc_segwit_nested:", newAddress_btc_segwit_nested.address);
  console.log("newAddress_btc_segwit_native:", newAddress_btc_segwit_native.address);

i got

mnemonic bachelor sense wreck narrow visit morning bar insane park bridge add despair
hdPath_btc m/44'/0'/0'/0/0

derivePrivateKey_btc L3ggXYWxvKauWtHwGNvkfxRxLEJcBpeyL5d5JNgQUno9YhiKoyCb
newAddress_btc_segwit_taproot: bc1pgfxgwzclewwp55vfndwjjgqf4ywjw3c2ed44wvmmdxtxjnyr4n3sptjqe3
newAddress_btc_legacy: 1CUjK4j44BAjqjZX3ornaPbWKwJXNngRFs
newAddress_btc_segwit_nested: 31jesan1STgxGLE8k2xZhJhe2YVso9Aewc
newAddress_btc_segwit_native: bc1q0hn77dvkte0xxjr2y2tzwllj4gphw3782lz03y

but it dose not match the okx wallet , only legacy match

(this picture is from okx chrome extend wallet ) image

leverwwz commented 4 months ago

my code :

import { bip32, bip39 } from '@okxweb3/crypto-lib';
import { EthWallet } from '@okxweb3/coin-ethereum';
import { BtcWallet } from '@okxweb3/coin-bitcoin';

 const mnemonic = "bachelor sense wreck narrow visit morning bar insane park bridge add despair"

  let wallet_btc = new BtcWallet()
  const hdPath_btc = await wallet_btc.getDerivedPath({ index: 0 });
  console.log("hdPath_btc",hdPath_btc);
  let derivePrivateKey_btc = await wallet_btc.getDerivedPrivateKey({ mnemonic: mnemonic, hdPath: hdPath_btc });
  console.log("derivePrivateKey_btc",derivePrivateKey_btc);

  let newAddress_btc_legacy = await wallet_btc.getNewAddress({ privateKey: derivePrivateKey_btc,addressType: "Legacy" });
  let newAddress_btc_segwit_native = await wallet_btc.getNewAddress({ privateKey: derivePrivateKey_btc,  addressType: "segwit_native",});
  let newAddress_btc_segwit_nested = await wallet_btc.getNewAddress({ privateKey: derivePrivateKey_btc,addressType: "segwit_nested" });
  let newAddress_btc_segwit_taproot = await wallet_btc.getNewAddress({ privateKey: derivePrivateKey_btc,addressType: "segwit_taproot" });

  console.log("newAddress_btc_segwit_taproot:", newAddress_btc_segwit_taproot.address);
  console.log("newAddress_btc_legacy:", newAddress_btc_legacy.address);
  console.log("newAddress_btc_segwit_nested:", newAddress_btc_segwit_nested.address);
  console.log("newAddress_btc_segwit_native:", newAddress_btc_segwit_native.address);

i got

mnemonic bachelor sense wreck narrow visit morning bar insane park bridge add despair
hdPath_btc m/44'/0'/0'/0/0

derivePrivateKey_btc L3ggXYWxvKauWtHwGNvkfxRxLEJcBpeyL5d5JNgQUno9YhiKoyCb
newAddress_btc_segwit_taproot: bc1pgfxgwzclewwp55vfndwjjgqf4ywjw3c2ed44wvmmdxtxjnyr4n3sptjqe3
newAddress_btc_legacy: 1CUjK4j44BAjqjZX3ornaPbWKwJXNngRFs
newAddress_btc_segwit_nested: 31jesan1STgxGLE8k2xZhJhe2YVso9Aewc
newAddress_btc_segwit_native: bc1q0hn77dvkte0xxjr2y2tzwllj4gphw3782lz03y

but it dose not match the okx wallet , only legacy match

(this picture is from okx chrome extend wallet ) image

============================================== hdPath_btc m/44'/0'/0'/0/0 this derived path only for legacy address. different type address has different derive path.

async getDerivedPath(param: GetDerivedPathParam): Promise<any> {
        if (!param.segwitType) {
            // normal address
            return `m/44'/0'/0'/0/${param.index}`;
        }

        if (param.segwitType == segwitType.SEGWIT_NESTED) {
            return `m/84'/0'/0'/0/${param.index}`;
        } else if (param.segwitType == segwitType.SEGWIT_NESTED_49) {
            return `m/49'/0'/0'/0/${param.index}`;
        } else if (param.segwitType == segwitType.SEGWIT_NATIVE) {
            return `m/84'/0'/0'/0/${param.index}`;
        } else if (param.segwitType == segwitType.SEGWIT_TAPROOT) {
            return `m/86'/0'/0'/0/${param.index}`
        } else {
            return Promise.reject(DerivePathError);
        }
    }

============================ if you want to get other type of address, i think u need to change the derive path, thank u.