Skip to content

Instantly share code, notes, and snippets.

@dohaki
Created October 29, 2019 16:33
Show Gist options
  • Save dohaki/313d4bf3bb5644d4c2c179531cd173f3 to your computer and use it in GitHub Desktop.
Save dohaki/313d4bf3bb5644d4c2c179531cd173f3 to your computer and use it in GitHub Desktop.

Revisions

  1. dohaki created this gist Oct 29, 2019.
    45 changes: 45 additions & 0 deletions WalletFromEthers.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    import { ethers } from "ethers";
    import { EthersWalletData, IdentityWalletData } from "../typings";
    import { TL_WALLET_VERSION } from "./TLWallet";

    export class WalletFromEthers extends ethers.Wallet {
    public static fromWalletData(
    walletData: EthersWalletData | IdentityWalletData
    ) {
    const { signingKey } = walletData.meta
    const { privateKey, mnemonic } = signingKey
    return new this(privateKey, mnemonic)
    }

    public static fromEthers(
    walletFromEthers: ethers.Wallet
    ) {
    const { privateKey, mnemonic } = walletFromEthers
    return new this(privateKey, mnemonic)
    }

    constructor(privateKey: string, mnemonic: string) {
    const signingKeyFromEthers = new ethers.utils.SigningKey(
    privateKey
    )
    // @ts-ignore
    signingKeyFromEthers.mnemonic = mnemonic
    // @ts-ignore
    signingKeyFromEthers.path = DEFAULT_DERIVATION_PATH
    super(signingKeyFromEthers)
    }

    public toWalletData(walletType, address) {
    return {
    address,
    version: TL_WALLET_VERSION,
    type: walletType,
    meta: {
    signingKey: {
    mnemonic: this.mnemonic,
    privateKey: this.privateKey
    }
    }
    }
    }
    }