Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/keysmith/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"@polkadot/keyring": "^10.1.8",
"@polkadot/util": "^10.1.8",
"@polkadot/util-crypto": "^10.1.8",
"@solana/web3.js": "^1.74.0",
"@stablelib/bytes": "^1.0.1",
"@stablelib/chacha20poly1305": "^1.0.1",
"@stablelib/hex": "^1.0.1",
Expand All @@ -70,7 +71,8 @@
"body-parser": "^1.20.1",
"ethers": "^5.7.1",
"googleapis": "^108.0.0",
"randomstring": "^1.2.2"
"randomstring": "^1.2.2",
"uuid": "^9.0.0"
},
"resolutions": {
"sharp": "0.28.0"
Expand Down
10 changes: 10 additions & 0 deletions packages/keysmith/src/chains/_share_/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Me3Wallet } from '../../types'
import { WalletCipher } from '../../safeV2/v2'

export interface IChainContext {
readonly series: string

createWallet(chains: any[], mnemonic: string, pkCipher: WalletCipher): Promise<Me3Wallet[]>

signTx(mainChain, wallet: Me3Wallet, tx: any, pkDecipher: WalletCipher): Promise<string>
}
57 changes: 57 additions & 0 deletions packages/keysmith/src/chains/bitcoin/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import _ from 'lodash'

import { IChainContext } from '../_share_/context'
import { WalletCipher } from '../../safeV2/v2'
import { Me3Wallet } from '../../types'
import { getWalletName } from '../_share_/functions'

const getGenerator = (series: string) => {
switch (_.toLower(series)) {
case 'btc':
return require('bitcore-lib')
case 'ltc':
return require('bitcore-lib-ltc')
case 'bch':
return require('bitcore-lib-cash')
default:
break
}
return undefined
}

export class BitcoinContext implements IChainContext {
readonly series: string

constructor(_series: string) {
this.series = _series
}
createWallet(chains: any[], mnemonic: string, pkCipher: WalletCipher): Promise<Me3Wallet[]> {
const wallets = _.chain(chains)
.map(c => {
const generator = getGenerator(c.series)
if (_.isEmpty(generator)) {
return undefined
}

const value = Buffer.from(mnemonic, 'utf8')
const hash = generator.crypto.Hash.sha256(value)
const bn = generator.crypto.BN.fromBuffer(hash)
const privateKey = new generator.PrivateKey(bn)

return {
walletAddress: privateKey.toAddress().toString(),
secret: pkCipher(privateKey.toString()),
walletName: getWalletName(c.description),
chainName: c.name,
} as Me3Wallet
})
.compact()
.value()
return Promise.resolve(wallets)
}

signTx(mainChain, wallet: Me3Wallet, tx: any, pkDecipher: WalletCipher): Promise<string> {
// TODO: BTC tx signning
return Promise.resolve('')
}
}
17 changes: 17 additions & 0 deletions packages/keysmith/src/chains/cosmos/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { IChainContext } from '../_share_/context'
import { WalletCipher } from '../../safeV2/v2'
import { Me3Wallet } from '../../types'

// TODO
export class CosmosContext implements IChainContext {
readonly series: string

createWallet(chains: any[], mnemonic: string, pkCipher: WalletCipher): Promise<Me3Wallet[]> {
return Promise.resolve([])
}

signTx(mainChain, wallet: Me3Wallet, tx: any, pkDecipher: WalletCipher): Promise<string> {
return Promise.resolve('')
}

}
41 changes: 41 additions & 0 deletions packages/keysmith/src/chains/evm/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { ethers } from 'ethers'
import { Me3Wallet } from '../../types'
import { IChainContext } from '../_share_/context'
import { WalletCipher } from '../../safeV2/v2'
import { TransactionRequest } from './domain'
import { getWalletName } from '../_share_/functions'

export class EvmContext implements IChainContext {
readonly series: string

constructor(_series: string) {
this.series = _series
}

createWallet(chains: any[], mnemonic: string, cipher: WalletCipher): Promise<Me3Wallet[]> {
const wallets = chains.map(c => {
const accountIdx = 0
const path = `${c.path}${accountIdx}`

const wallet = ethers.Wallet.fromMnemonic(mnemonic, path)
return {
walletAddress: wallet.address,
secret: cipher(wallet.privateKey),
chainName: c.name,
walletName: getWalletName(c.description),
} as Me3Wallet
})

return Promise.resolve(wallets)
}

async signTx(_, wallet: Me3Wallet, tx: TransactionRequest, pkDecipher: WalletCipher): Promise<string> {
const rawPK = pkDecipher(wallet.secret)
try {
const wallet = new ethers.Wallet(rawPK)
return await wallet.signTransaction(tx)
} catch (error) {
throw new Error('Invalid privateKey provided')
}
}
}
1 change: 1 addition & 0 deletions packages/keysmith/src/chains/evm/domain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type TransactionRequest = Record<string, any>
45 changes: 45 additions & 0 deletions packages/keysmith/src/chains/file/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import _ from 'lodash'
import * as signer from '@zondax/filecoin-signing-tools'

import { IChainContext } from '../_share_/context'
import { WalletCipher } from '../../safeV2/v2'
import { Me3Wallet } from '../../types'
import { getWalletName } from '../_share_/functions'

export class FileContext implements IChainContext {
readonly series: string

constructor(_series: string) {
this.series = _series
}

createWallet(chains: any[], mnemonic: string, pkCipher: WalletCipher): Promise<Me3Wallet[]> {
const wallets = _.chain(chains)
.map(c => {
const accountIdx = 0
const path = `${c.path}${accountIdx}`

const key = signer.keyDerive(
mnemonic,
path,
_.toLower(c.name) === 'fil' ? 'mainnet' : 'testnet'
)

return {
walletAddress: key.address,
secret: pkCipher(key.private_base64),
walletName: getWalletName(c.description),
chainName: c.name,
} as Me3Wallet
})
.compact()
.value()

return Promise.resolve(wallets)
}

signTx(mainChain, wallet: Me3Wallet, tx: any, pkDecipher: WalletCipher): Promise<string> {
// TODO: File tx signning
return Promise.resolve('')
}
}
7 changes: 7 additions & 0 deletions packages/keysmith/src/chains/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export * from './_share_/context'
export * from './bitcoin/context'
export * from './cosmos/context'
export * from './evm/context'
export * from './file/context'
export * from './solana/context'
export * from './substrate/context'
64 changes: 64 additions & 0 deletions packages/keysmith/src/chains/solana/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import _ from 'lodash'
import * as bip39 from 'bip39'
import * as bs58 from 'bs58'
import { clusterApiUrl, Connection, Keypair, PublicKey, Transaction } from '@solana/web3.js'

import { IChainContext } from '../_share_/context'
import { Me3Wallet } from '../../types'
import { WalletCipher } from '../../safeV2/v2'
import { getWalletName } from '../_share_/functions'

const RpcEndpoint = {
'sol': 'mainnet-beta',
'soldev': 'devnet',
'soltest': 'testnet',
}
export class SolanaContext implements IChainContext {
readonly series: string

constructor(_series: string) {
this.series = _series
}

async createWallet(
chains: any[],
mnemonic: string,
pkCipher: WalletCipher
): Promise<Me3Wallet[]> {
const seed = await bip39.mnemonicToSeed(mnemonic)
const keypair = Keypair.fromSeed(seed.subarray(0, 32))
const wallets = chains.map(c => {
return {
chainName: c.name,
walletName: getWalletName(c.name),
walletAddress: keypair.publicKey.toBase58(),
secret: pkCipher(bs58.encode(keypair.secretKey)),
} as Me3Wallet
})
return wallets
}

async signTx(
mainChain,
wallet: Me3Wallet,
tx: Transaction,
pkDecipher: (pk: string) => string
): Promise<string> {
if (_.isEmpty(tx.recentBlockhash) || _.isNil(tx.lastValidBlockHeight)) {
const rpc = mainChain.node ?? clusterApiUrl(RpcEndpoint[_.toLower(wallet.chainName)])
const solConn = new Connection(rpc)
const lastHash = await solConn.getLatestBlockhash('confirmed')
tx.recentBlockhash = lastHash.blockhash
tx.lastValidBlockHeight = lastHash.lastValidBlockHeight
}

const rawPK = pkDecipher(wallet.secret)
const rawPKBytes = bs58.decode(rawPK)
tx.sign({
publicKey: new PublicKey(wallet.walletAddress),
secretKey: rawPKBytes,
})
const bytes = tx.serialize()
return bs58.encode(bytes)
}
}
47 changes: 47 additions & 0 deletions packages/keysmith/src/chains/substrate/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {
cryptoIsReady,
cryptoWaitReady,
encodeAddress,
mnemonicToMiniSecret,
sr25519PairFromSeed,
} from '@polkadot/util-crypto'

import { IChainContext } from '../_share_/context'
import { Me3Wallet } from '../../types'
import { WalletCipher } from '../../safeV2/v2'
import { u8aToHex } from '@polkadot/util'
import { getWalletName } from '../_share_/functions'

export class SubstrateContext implements IChainContext {
readonly series: string

constructor(_series: string) {
this.series = _series
if (!cryptoIsReady()) {
cryptoWaitReady().then(console.log)
}
}
createWallet(chains: any[], mnemonic: string, pkCipher: WalletCipher): Promise<Me3Wallet[]> {
const mini = mnemonicToMiniSecret(mnemonic)
const { publicKey, secretKey } = sr25519PairFromSeed(mini)
const walletAddress = encodeAddress(publicKey)
const secret = pkCipher(u8aToHex(secretKey))

const wallets = chains.map(c => {
return {
chainName: c.name,
walletName: getWalletName(c.description),
walletAddress,
secret,
} as Me3Wallet
})

return Promise.resolve(wallets)
}

signTx(mainChain, wallet: Me3Wallet, tx: any, pkDecipher: WalletCipher): Promise<string> {
// TODO
return Promise.resolve('')
}

}
Loading