Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

One integration #170

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
ONE wallet: import/create with mnemonic
  • Loading branch information
yuriy committed Jul 29, 2020
commit 130c9826c298752b2dcc3634c9b0cee956551e69
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -64,7 +64,7 @@
"tezos-uri": "^1.0.3",
"uri-js": "^3.0.2",
"url-parse": "^1.4.1",
"@harmony-js/core": "^0.1.43"
"@harmony-js/core": "^0.1.45"
},
"devDependencies": {
"@babel/core": "^7.9.0",
2 changes: 1 addition & 1 deletion src/one/oneEngine.js
Original file line number Diff line number Diff line change
@@ -128,7 +128,7 @@ export class OneEngine extends CurrencyEngine {
async checkServerInfoInnerLoop() {
try {
const res: OneGetLastHeader = await this.multicastServers(
'getLatestHeader',
'hmyv2_latestHeader',
[]
)

31 changes: 23 additions & 8 deletions src/one/onePlugin.js
Original file line number Diff line number Diff line change
@@ -2,9 +2,10 @@

// import { bns } from 'biggystring'
import { Harmony } from '@harmony-js/core'
import { ChainID, ChainType, isPrivateKey } from '@harmony-js/utils'
// import or require settings
import { ChainID, ChainType } from '@harmony-js/utils'
import { bns } from 'biggystring'
import { generateMnemonic, validateMnemonic } from 'bip39'
import {
type EdgeCorePluginOptions,
type EdgeCurrencyEngine,
@@ -19,7 +20,7 @@ import {
import { CurrencyPlugin } from '../common/plugin.js'
import { getDenomInfo } from '../common/utils.js'
import { OneEngine } from './oneEngine.js'
import { GAS_LIMIT, GAS_PRICE, currencyInfo } from './oneInfo'
import { currencyInfo, GAS_LIMIT, GAS_PRICE } from './oneInfo'

export class OnePlugin extends CurrencyPlugin {
harmonyApi: Harmony
@@ -44,23 +45,37 @@ export class OnePlugin extends CurrencyPlugin {
})
}

async importPrivateKey(privateKey: string): { oneKey: string } | void {
const account = await this.harmonyApi.wallet.addByPrivateKey(privateKey)
async importPrivateKey(userInput: string): Promise<Object> {
if (/^(0x)?[0-9a-fA-F]{64}$/.test(userInput)) {
if (!isPrivateKey(userInput)) {
throw new Error('Invalid private key')
}

return { oneKey: userInput }
} else {
if (!validateMnemonic(userInput)) {
throw new Error('Invalid mnemonic')
}

if (account) {
return { oneKey: privateKey }
const account = await this.harmonyApi.wallet.addByMnemonic(userInput)

const privateKey = account.privateKey.replace('0x', '')

return { oneKey: privateKey, oneMnemonic: userInput }
}
}

async createPrivateKey(walletType: string): Promise<Object> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create and save mnemonic.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

const type = walletType.replace('wallet:', '')

if (type === 'one') {
const account = await this.harmonyApi.wallet.createAccount()
const mnemonic = generateMnemonic()

const account = await this.harmonyApi.wallet.addByMnemonic(mnemonic)

const privateKey = account.privateKey.replace('0x', '')

return { oneKey: privateKey }
return { oneKey: privateKey, oneMnemonic: mnemonic }
} else {
throw new Error('InvalidWalletType')
}