【Web3】keyStore导出帐户、privatekey Get Wallet Address

目录

初始化

keyStore 导出账户

1.walletObj 导出

2.web3导出

privatekey Get Wallet Address


初始化

npm install [email protected]

npm install ethereumjs-wallet

import { onMounted } from 'vue'
import * as bip39 from 'bip39'
import { hdkey } from 'ethereumjs-wallet'

const MyMnemonic = ref(
  'Your Mnemonic Word'
)

onMounted(async () => { 
 const Seed = await bip39.mnemonicToSeed(MyMnemonic.value)
  //通过hdkey将seed生成HD Wallet
  const hdkeyWallet = hdkey.fromMasterSeed(Seed)
  //生成wallet中在m/44'/60'/0'/0/i路径的keypair
  const keypair = hdkeyWallet.derivePath("m/44'/60'/0'/0/0")

  //通过keypair 获取wallet地址和私钥
  //获取wallet对象
  const walletObj = keypair.getWallet()
}) 

keyStore 导出账户

介绍:两种导出方式 

1.walletObj 导出

2.web3导出

1.walletObj 导出

import ethwallet, { hdkey } from 'ethereumjs-wallet'

onMounted(async () => {
let keystore = await walletObj.toV3('111111') // 参数必须为 字符串

//通过keystore解密私钥
const Decode = await ethwallet.fromV3(keystore, '111111')
const walletObjDecode = Decode.getPrivateKey().toString('hex')
console.log('wallet对象', walletObjDecode)

})

2.web3导出

初始化

npm install web3

  import Web3 from 'web3'
  //初始化网络
  const Web3Network = new Web3( Web3.givenProvider || '你的网络地址')

keyStore导出

  
const MyprivateKey = ref(
  '你的privateKey 地址'
)
 onMounted(async () => { 

  const Web3keystore = Web3Network.eth.accounts.encrypt(
    MyprivateKey.value,
    '111111'
  )

  //通过keystore解密私钥
  const web3Decode = Web3Network.eth.accounts.decrypt(Web3keystore, '111111')
  console.log('web3', web3Decode.privateKey)
  })

privatekey Get Wallet Address

npm install buffer

 import ethwallet from 'ethereumjs-wallet' 
 const MyprivateKey = ref('你的privatekey')

 const privatekey = Buffer(MyprivateKey.value, 'hex')
 const wallet = ethwallet.fromPrivateKey(privatekey)
 const KeywalletAddress = wallet.getAddressString()
 console.log(`wallet地址:`, KeywalletAddress)

你可能感兴趣的:(web3,javascript,vue.js,前端,区块链)