mirror of
https://github.com/tornadocash/provider.git
synced 2024-11-22 01:36:51 +01:00
Merge pull request #1 from tornadocash/walletConnectFix
fix JSON RPC request
This commit is contained in:
commit
170d58e067
@ -1,5 +1,9 @@
|
||||
const Web3 = require('web3')
|
||||
const { hexToNumberString, toChecksumAddress, numberToHex } = require('web3-utils')
|
||||
const VERSIONS = ['old', 'new']
|
||||
|
||||
// TODO: create constants, utils
|
||||
// TODO: move _repeatUntilResult and _generateId to utils
|
||||
|
||||
export default (ctx, inject) => {
|
||||
const moduleOptions = <%= JSON.stringify(options) %>
|
||||
@ -13,11 +17,11 @@ export default (ctx, inject) => {
|
||||
this.web3 = new Web3(new Web3.providers.HttpProvider(options.config.rpcUrl))
|
||||
}
|
||||
|
||||
async initProvider(provider) {
|
||||
async initProvider(provider, { version }) {
|
||||
try {
|
||||
this.provider = provider
|
||||
|
||||
await this._checkVersion()
|
||||
await this._setVersion(version)
|
||||
return await this._initProvider()
|
||||
} catch (err) {
|
||||
throw new Error(`Provider method initProvider has error: ${err.message}`)
|
||||
@ -245,10 +249,11 @@ export default (ctx, inject) => {
|
||||
|
||||
this.provider.sendAsync(
|
||||
{
|
||||
from,
|
||||
method,
|
||||
params,
|
||||
jsonrpc: '2.0',
|
||||
from,
|
||||
id: this._generateId(),
|
||||
},
|
||||
callback,
|
||||
)
|
||||
@ -279,6 +284,23 @@ export default (ctx, inject) => {
|
||||
}
|
||||
}
|
||||
|
||||
async _setVersion(version) {
|
||||
try {
|
||||
if(version) {
|
||||
if (!VERSIONS.includes(version)) {
|
||||
throw new Error('Invalid version parameter.')
|
||||
}
|
||||
|
||||
this.version = version
|
||||
return
|
||||
}
|
||||
|
||||
return await this._checkVersion()
|
||||
} catch (err) {
|
||||
throw new Error(`Provider method _setVersion has error: ${err.message}`)
|
||||
}
|
||||
}
|
||||
|
||||
_checkVersion() {
|
||||
if (this.provider && this.provider.request) {
|
||||
this.version = 'new'
|
||||
@ -311,6 +333,12 @@ export default (ctx, inject) => {
|
||||
iteration()
|
||||
})
|
||||
}
|
||||
|
||||
_generateId() {
|
||||
const date = Date.now() * Math.pow(10, 3);
|
||||
const extra = Math.floor(Math.random() * Math.pow(10, 3));
|
||||
return date + extra;
|
||||
}
|
||||
}
|
||||
|
||||
const provider = new instance({ config: moduleOptions })
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "nuxt-web3-provider",
|
||||
"version": "0.1.1",
|
||||
"version": "0.1.2",
|
||||
"description": "Provider integration with Nuxt.js",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
8
types/index.d.ts
vendored
8
types/index.d.ts
vendored
@ -61,11 +61,17 @@ interface ProviderOptions {
|
||||
blockGasLimit?: number,
|
||||
}
|
||||
|
||||
type ProviderVersions = 'new' | 'old'
|
||||
|
||||
type Config = {
|
||||
version?: ProviderVersions
|
||||
}
|
||||
|
||||
interface ProviderInstance {
|
||||
readonly web3: typeof Web3
|
||||
readonly config: ProviderOptions
|
||||
|
||||
initProvider(provider: unknown): Promise<Address>
|
||||
initProvider(provider: unknown, config?: Config): Promise<Address>
|
||||
sendRequest(params: RequestParams): Promise<TransactionReceipt>
|
||||
contractRequest(params: ContractRequestParams): Promise<TransactionReceipt>
|
||||
getBalance(params: GetBalanceParams): Promise<number>
|
||||
|
Loading…
Reference in New Issue
Block a user