import { ethers , Wallet as etherWallet } from 'ethers' ; import { ArianeeConfig } from '../../models/arianeeConfiguration' ; import { isPrivateKeyValid } from '../isPrivateKeyValid/isPrivateKeyValid' ; import { ArianeeWallet , ClassicConfiguration } from './wallet' ; const Web3 = require ( 'web3' ) ; export class ArianeeWalletBuilder { private container ; private web3 = new Web3 ( ) ; constructor ( private arianeeConfig ) { } private buildAriaWalletFrom ( configuration ) { if ( this . web3 . utils . isAddress ( configuration . account . address ) ) { return new ArianeeWallet ( configuration , this . arianeeConfig ) ; } throw new Error ( 'invalid address' ) ; } public fromPassPhrase ( passphrase ) { let privateKeyFromPassphrase ; if ( isNaN ( + passphrase ) ) { privateKeyFromPassphrase = this . web3 . utils . padLeft ( this . web3 . utils . asciiToHex ( passphrase ) , 64 ) ; } else { privateKeyFromPassphrase = this . web3 . utils . padLeft ( this . web3 . utils . numberToHex ( passphrase ) , 64 ) ; } return this . fromPrivateKey ( privateKeyFromPassphrase ) ; } /**
     * From a randomKey and return ArianeeProtocol
     */ public fromRandomKey ( ) { const randomWallet = etherWallet . createRandom ( ) ; const mnemonic = randomWallet . mnemonic ; const account = this . web3 . eth . accounts . privateKeyToAccount ( randomWallet . privateKey ) ; return this . buildAriaWalletFrom ( { account , mnemonic : mnemonic . phrase } ) ; } param data public fromRandomMnemonic ( ) { return this . fromRandomKey ( ) ; } public readOnlyWallet ( ) { return new ArianeeWallet ( { account : { address : '0x0000000000000000000000000000000000000000' } } , this . arianeeConfig ) ; } param mnemonic public fromMnemonic ( mnemonic ) { const isValidMnemonic = ethers . utils . isValidMnemonic ( mnemonic ) ; if ( isValidMnemonic ) { const { privateKey } = etherWallet . fromMnemonic ( mnemonic ) ; const account = this . web3 . eth . accounts . privateKeyToAccount ( privateKey ) ; return this . buildAriaWalletFrom ( { account , mnemonic } ) ; } else { throw new Error ( 'invalid mnemonic' ) ; } } param privateKey param bdHVaultURL public fromPrivateKey ( privateKey ) { if ( ! isPrivateKeyValid ( privateKey ) ) { throw new Error ( 'privateKey should start with 0x and should be 0x+64 characters' ) ; } const account = this . web3 . eth . accounts . privateKeyToAccount ( privateKey ) ; return this . buildAriaWalletFrom ( { account } ) ; } public fromExternalWallet = ( data ) => { const wallet = new ArianeeWallet ( { account : { address : data . address } } , this . arianeeConfig ) ; wallet . setCustomSign ( data . customSign ) ; return wallet ; } ; public async fromCustomWeb3 ( web3 ) { this . web3 = web3 ; let account ; if ( web3 . eth . accounts . wallet [ 0 ] ) { account = { address : web3 . eth . accounts . wallet [ 0 ] . address , privateKey : web3 . eth . accounts . wallet [ 0 ] . privateKey } ; } else { const remoteWallet = await web3 . eth . getAccounts ( ) ; if ( remoteWallet [ 0 ] ) { account = { address : remoteWallet [ 0 ] } ; } else { throw new Error ( 'Their is no account in the custom web3 instance' ) ; } } return this . buildAriaWalletFrom ( { account , web3 : web3 } ) ; } public fromMetamask ( address ) { const account = { address : address } ; const wallet = this . buildAriaWalletFrom ( { account } ) ; wallet . walletservice . metamask = true ; return wallet ; } }