-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathonePlugin.js
185 lines (146 loc) · 4.99 KB
/
onePlugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// @flow
// import { bns } from 'biggystring'
import { Harmony } from '@harmony-js/core'
import { ChainID, ChainType, isPrivateKey } from '@harmony-js/utils'
// import or require settings
import { bns } from 'biggystring'
import { generateMnemonic, validateMnemonic } from 'bip39'
import {
type EdgeCorePluginOptions,
type EdgeCurrencyEngine,
type EdgeCurrencyEngineOptions,
type EdgeCurrencyPlugin,
type EdgeEncodeUri,
type EdgeIo,
type EdgeParsedUri,
type EdgeWalletInfo
} from 'edge-core-js/types'
import { CurrencyPlugin } from '../common/plugin.js'
import { getDenomInfo } from '../common/utils.js'
import { OneEngine } from './oneEngine.js'
import { currencyInfo, GAS_LIMIT, GAS_PRICE } from './oneInfo'
export class OnePlugin extends CurrencyPlugin {
harmonyApi: Harmony
harmonyRpcNodes: Array<string>
// connectionPool: Object
constructor(io: EdgeIo) {
super(io, 'one', currencyInfo)
this.harmonyRpcNodes = []
for (const rpcNode of currencyInfo.defaultSettings.otherSettings
.oneServers) {
this.harmonyRpcNodes.push(rpcNode)
}
this.harmonyApi = new Harmony(this.harmonyRpcNodes[0], {
// chainType set to Harmony
chainType: ChainType.Harmony,
// chainType set to HmyLocal
chainId: ChainID.HmyMainnet
})
}
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')
}
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> {
const type = walletType.replace('wallet:', '')
if (type === 'one') {
const mnemonic = generateMnemonic()
const account = await this.harmonyApi.wallet.addByMnemonic(mnemonic)
const privateKey = account.privateKey.replace('0x', '')
return { oneKey: privateKey, oneMnemonic: mnemonic }
} else {
throw new Error('InvalidWalletType')
}
}
async derivePublicKey(walletInfo: EdgeWalletInfo): Promise<Object> {
const type = walletInfo.type.replace('wallet:', '')
if (type === 'one') {
const account = await this.harmonyApi.wallet.addByPrivateKey(
walletInfo.keys.oneKey
)
const address = this.harmonyApi.crypto.getAddress(account.address).bech32
return { publicKey: address }
} else {
throw new Error('InvalidWalletType')
}
}
async parseUri(uri: string): Promise<EdgeParsedUri> {
const networks = {
one: true
}
const { parsedUri, edgeParsedUri } = this.parseUriCommon(
currencyInfo,
uri,
networks
)
try {
this.harmonyApi.crypto.getAddress(edgeParsedUri.publicAddress)
} catch (e) {
throw new Error('InvalidPublicAddressError')
}
edgeParsedUri.uniqueIdentifier = parsedUri.query.dt || undefined
return edgeParsedUri
}
async encodeUri(obj: EdgeEncodeUri): Promise<string> {
try {
this.harmonyApi.crypto.getAddress(obj.publicAddress)
} catch (e) {
throw new Error('InvalidPublicAddressError')
}
let amount
if (typeof obj.nativeAmount === 'string') {
const currencyCode: string = 'ONE'
const nativeAmount: string = obj.nativeAmount
const denom = getDenomInfo(currencyInfo, currencyCode)
if (!denom) {
throw new Error('ONE InternalErrorInvalidCurrencyCode')
}
// amount = nativeAmount
amount = bns.div(nativeAmount, denom.multiplier, 18)
}
const encodedUri = this.encodeUriCommon(obj, 'one', amount)
return encodedUri
}
}
export function makeOnePlugin(opts: EdgeCorePluginOptions): EdgeCurrencyPlugin {
const { io } = opts
let toolsPromise: Promise<OnePlugin>
function makeCurrencyTools(): Promise<OnePlugin> {
if (toolsPromise != null) return toolsPromise
toolsPromise = Promise.resolve(new OnePlugin(io))
return toolsPromise
}
async function makeCurrencyEngine(
walletInfo: EdgeWalletInfo,
opts: EdgeCurrencyEngineOptions
): Promise<EdgeCurrencyEngine> {
const tools = await makeCurrencyTools()
const currencyEngine = new OneEngine(tools, walletInfo, opts)
await currencyEngine.loadEngine(tools, walletInfo, opts)
// This is just to make sure otherData is Flow type checked
currencyEngine.otherData = currencyEngine.walletLocalData.otherData
currencyEngine.otherData.gasPrice = GAS_PRICE
currencyEngine.otherData.gasLimit = GAS_LIMIT
if (!currencyEngine.otherData.recommendedFee) {
currencyEngine.otherData.recommendedFee = String(GAS_PRICE * GAS_LIMIT)
}
const out: EdgeCurrencyEngine = currencyEngine
return out
}
return {
currencyInfo,
makeCurrencyEngine,
makeCurrencyTools
}
}