Skip to main content

bn.getWeb3Provider

getWeb3Provider(): Web3Provider

description The api to get web3 provider

remark supported jssdk >= 4.0.0

example

 //eg1: get web3 provider and then request with method
bn.getWeb3Provider()
.request({
method: 'eth_requestAccounts'
})
.then(accounts => {
console.log('accounts', accounts)
})

Returns

Web3Provider


Class: Web3Provider

NameTypeDescription
idMappingdefault
rpcdefault
callbacksMap<number, JsonRpcCallback<JsonRpcCallbackResponse>>
wrapResultsMap<number, boolean>
isDebugundefined | boolean
addressstring[] = []
readyboolean = false
experimentalObject
chainId?stringThe chain ID of the currently connected Ethereum chain.; See chainId.network for more information.
_rpcHooksRPCHooks
isProcessingRequestAccountsboolean = false
ProviderRpcErrortypeof ProviderRpcError
setChainId(value?): void
setAddress(value?): void
setRpc(rpcUrl, rpcHooks?): void
request(payload): Promise<EthJsonRpc[T]["response"]["result"]>
isConnected(): booleandeprecated Listen to "connect" event instead.
enable(): Promise<Maybe<string[]>>deprecated Use request({ method: "eth_requestAccounts" }) instead.
send(payload): EthJsonRpc[T]["response"]deprecated Use request() method instead.
sendAsync(payload, callback): voiddeprecated Use request() method instead.
_request(payload, wrapResult?): Promise<undefined | null | string | boolean | object | ( (undefined | string | object)[] | ( (undefined | string)[] | JsonRpcSuccess<UnknownResponse> | JsonRpcSuccess<string[]> | JsonRpcSuccess<string> | JsonRpcSuccess<boolean> | JsonRpcSuccess<null> | JsonRpcSuccess<never>>
eth_accounts(): string[]
eth_coinbase(): undefined | string
net_version(): null | string
eth_chainId(): null | string
eth_sign(payload): void
personal_sign(payload): void
eth_signTypedData(payload, version): void
getAccounts(): Promise<string[]>
requestAccounts(): Promise<string[]>Metamask 行為描述
比照流程:https://github.com/MetaMask/metamask-extension/blob/develop/app/scripts/controllers/permissions/permissionsMethodMiddleware.js
eth_sendTransaction(payload): Promise<Maybe<string>>
populateTransaction(payload): Promise<EthTransaction>populate the transaction params
eth_sendRawTransaction(payload): void
wallet_watchAsset(payload): void
wallet_addEthereumChain(payload): Promise<Maybe<null>>
wallet_switchEthereumChain(payload): Promise<Maybe<null>>
postMessage(action, id, object): void
_isJsonRpc(result): result is JsonRpcSuccess<UnknownResponse>
sendResponse(id, result): void
sendError(id, error): void

setChainId

setChainId(value?): void

Parameters

NameType
value?string | number

Returns

void


setAddress

setAddress(value?): void

Parameters

NameTypeDefault value
valuestring[][]

Returns

void


setRpc

Private setRpc(rpcUrl, rpcHooks?): void

Parameters

NameType
rpcUrlundefined | string
rpcHooks?RPCHooks

Returns

void


request

request<T>(payload): Promise<EthJsonRpc[T]["response"]["result"]>

Type parameters

NameType
Textends keyof EthJsonRpc

Parameters

NameType
payloadEthJsonRpc[T]["request"]

Returns

Promise<EthJsonRpc[T]["response"]["result"]>


isConnected

isConnected(): boolean

deprecated Listen to "connect" event instead.

Returns

boolean


enable

enable(): Promise<Maybe<string[]>>

deprecated Use request({ method: "eth_requestAccounts" }) instead.

Returns

Promise<Maybe<string[]>>


send

send<T>(payload): EthJsonRpc[T]["response"]

deprecated Use request() method instead.

Type parameters

NameType
Textends keyof EthJsonRpc

Parameters

NameType
payloadEthJsonRpc[T]["request"]

Returns

EthJsonRpc[T]["response"]


sendAsync

sendAsync<T>(payload, callback): void

deprecated Use request() method instead.

Type parameters

NameType
Textends keyof EthJsonRpc

Parameters

NameType
payloadEthJsonRpc[T]["request"] | EthJsonRpc[T]["request"][]
callbackJsonRpcCallback<EthJsonRpc[T]["response"] | EthJsonRpc[T]["response"][]>

Returns

void


_request

Private _request(payload, wrapResult?): Promise<undefined | null | string | boolean | object | (undefined | string | object)[] | (undefined | string)[] | JsonRpcSuccess<UnknownResponse> | JsonRpcSuccess<string[]> | JsonRpcSuccess<string> | JsonRpcSuccess<boolean> | JsonRpcSuccess<null> | JsonRpcSuccess<never>>

Parameters

NameTypeDefault value
payloadJsonRpcRequest<"unknown", unknown> | JsonRpcRequest<"eth_accounts", undefined> | JsonRpcRequest<"eth_coinbase", undefined> | JsonRpcRequest<"net_version", undefined> | JsonRpcRequest<"eth_chainId", undefined> | JsonRpcRequest<"eth_sign", [string, string]> | JsonRpcRequest<"personal_sign", [string]> | JsonRpcRequest<"personal_ecRecover", [string, string]> | JsonRpcRequest<"eth_signTypedData" | "eth_signTypedData_v3" | "eth_signTypedData_v4", [string, string]> | JsonRpcRequest<"eth_sendTransaction", [EthTransaction]> | JsonRpcRequest<"eth_sendRawTransaction", [string]> | JsonRpcRequest<"eth_signTransaction", [EthTransaction]> | JsonRpcRequest<"eth_requestAccounts", undefined> | JsonRpcRequest<"eth_privateRequestAccounts", undefined> | JsonRpcRequest<"eth_getTransactionCount", [string, BlockTag]> | JsonRpcRequest<"eth_gasPrice", undefined> | JsonRpcRequest<"eth_estimateGas", [EthTransaction]> | JsonRpcRequest<"wallet_watchAsset", WatchAssetParameter> | JsonRpcRequest<"wallet_addEthereumChain", [Pick<AddEthereumChainParameter, "chainName" | "nativeCurrency" | "rpcUrls" | "blockExplorerUrls" | "iconUrls"> & { chainId: string }]> | JsonRpcRequest<"wallet_switchEthereumChain", [Omit<SwitchEthereumChainParameter, "chainId"> & { chainId: string }]> | JsonRpcRequest<"eth_newFilter", never> | JsonRpcRequest<"eth_newBlockFilter", never> | JsonRpcRequest<"eth_newPendingTransactionFilter", never> | JsonRpcRequest<"eth_uninstallFilter", never> | JsonRpcRequest<"eth_subscribe", never>undefined
wrapResultbooleantrue

Returns

Promise<undefined | null | string | boolean | object | (undefined | string | object)[] | (undefined | string)[] | JsonRpcSuccess<UnknownResponse> | JsonRpcSuccess<string[]> | JsonRpcSuccess<string> | JsonRpcSuccess<boolean> | JsonRpcSuccess<null> | JsonRpcSuccess<never>>


eth_accounts

Private eth_accounts(): string[]

Returns

string[]


eth_coinbase

Private eth_coinbase(): undefined | string

Returns

undefined | string


net_version

Private net_version(): null | string

Returns

null | string


eth_chainId

Private eth_chainId(): null | string

Returns

null | string


eth_sign

Private eth_sign(payload): void

Parameters

NameType
payloadJsonRpcRequest<"eth_sign", [string, string]>

Returns

void


personal_sign

Private personal_sign(payload): void

Parameters

NameType
payloadJsonRpcRequest<"personal_sign", [string]>

Returns

void


eth_signTypedData

Private eth_signTypedData(payload, version): void

Parameters

NameType
payloadJsonRpcRequest<"eth_signTypedData" | "eth_signTypedData_v3" | "eth_signTypedData_v4", [string, string]>
versionV3 | V4

Returns

void


getAccounts

Private getAccounts(): Promise<string[]>

Returns

Promise<string[]>


requestAccounts

Private requestAccounts(): Promise<string[]>

Metamask 行為描述 比照流程:https://github.com/MetaMask/metamask-extension/blob/develop/app/scripts/controllers/permissions/permissionsMethodMiddleware.js

Returns

Promise<string[]>


eth_sendTransaction

Private eth_sendTransaction(payload): Promise<Maybe<string>>

Parameters

NameType
payloadJsonRpcRequest<"eth_sendTransaction", [EthTransaction]>

Returns

Promise<Maybe<string>>


populateTransaction

Private populateTransaction(payload): Promise<EthTransaction>

populate the transaction params

Parameters

NameType
payloadJsonRpcRequest<"eth_sendTransaction", [EthTransaction]>

Returns

Promise<EthTransaction>


eth_sendRawTransaction

Private eth_sendRawTransaction(payload): void

Parameters

NameType
payloadJsonRpcRequest<"eth_sendRawTransaction", [string]>

Returns

void


wallet_watchAsset

Private wallet_watchAsset(payload): void

Parameters

NameType
payloadJsonRpcRequest<"wallet_watchAsset", WatchAssetParameter>

Returns

void


wallet_addEthereumChain

Private wallet_addEthereumChain(payload): Promise<Maybe<null>>

Parameters

NameType
payloadJsonRpcRequest<"wallet_addEthereumChain", [Pick<AddEthereumChainParameter, "chainName" | "nativeCurrency" | "rpcUrls" | "blockExplorerUrls" | "iconUrls"> & { chainId: string }]>

Returns

Promise<Maybe<null>>


wallet_switchEthereumChain

Private wallet_switchEthereumChain(payload): Promise<Maybe<null>>

Parameters

NameType
payloadJsonRpcRequest<"wallet_switchEthereumChain", [Omit<SwitchEthereumChainParameter, "chainId"> & { chainId: string }]>

Returns

Promise<Maybe<null>>


postMessage

Private postMessage<T>(action, id, object): void

Type parameters

NameType
Textends keyof PostMessagePayload

Parameters

NameType
actionT
idnumber
objectPostMessagePayload[T]["object"]

Returns

void


_isJsonRpc

Private _isJsonRpc(result): result is JsonRpcSuccess<UnknownResponse>

Parameters

NameType
resultJsonRpcSuccess<UnknownResponse> | Maybe<UnknownResponse>

Returns

result is JsonRpcSuccess<UnknownResponse>


sendResponse

Private sendResponse<T>(id, result): void

Type parameters

NameType
Textends keyof EthJsonRpc

Parameters

NameType
idnumber
resultEthJsonRpc[T]["response"] | EthJsonRpc[T]["response"]["result"]

Returns

void


sendError

Private sendError(id, error): void

Parameters

NameType
idnumber
errorstring | Error | ProviderRpcError

Returns

void

Interface: RPCHooks

NameTypeDescription
addEthereumChain(payload): Promise<void>
switchEthereumChain(payload): Promise<void>
postMessage(payload): void
getAccounts(): Promise<string[]>
sendTransaction(payload): Promise<Maybe<string>>
createAccount(): Promise<void>
isUnlocked(): Promise<boolean>
walletHubOpen(payload): Promise<void>
walletHubClose(): Promise<void>
walletHubOpenWallet(payload): Promise<void>
rpcCall(_payload: JsonRpcRequest<"unknown", unknown> | JsonRpcRequest<"eth_accounts", undefined> | JsonRpcRequest<"eth_coinbase", undefined> | JsonRpcRequest<"net_version", undefined> | JsonRpcRequest<"eth_chainId", undefined> | JsonRpcRequest<"eth_sign", [string, string]> | JsonRpcRequest<"personal_sign", [string]> | JsonRpcRequest<"personal_ecRecover", [string, string]> | JsonRpcRequest<"eth_signTypedData" | "eth_signTypedData_v3" | "eth_signTypedData_v4", [string, string]> | JsonRpcRequest<"eth_sendTransaction", [EthTransaction]> | JsonRpcRequest<"eth_sendRawTransaction", [string]> | JsonRpcRequest<"eth_signTransaction", [EthTransaction]> | JsonRpcRequest<"eth_requestAccounts", undefined> | JsonRpcRequest<"eth_privateRequestAccounts", undefined> | JsonRpcRequest<"eth_getTransactionCount", [string, BlockTag]> | JsonRpcRequest<"eth_gasPrice", undefined> | JsonRpcRequest<"eth_estimateGas", [EthTransaction]> | JsonRpcRequest<"wallet_watchAsset", WatchAssetParameter> | JsonRpcRequest<"wallet_addEthereumChain", [Pick<AddEthereumChainParameter, "chainName" | "nativeCurrency" | "rpcUrls" | "blockExplorerUrls" | "iconUrls"> & { chainId: string }]> | JsonRpcRequest<"wallet_switchEthereumChain", [Omit<SwitchEthereumChainParameter, "chainId"> & { chainId: string }]> | JsonRpcRequest<"eth_newFilter", never> | JsonRpcRequest<"eth_newBlockFilter", never> | JsonRpcRequest<"eth_newPendingTransactionFilter", never> | JsonRpcRequest<"eth_uninstallFilter", never> | JsonRpcRequest<"eth_subscribe", never>) => Promise<JsonRpcSuccess<UnknownResponse> | JsonRpcSuccess<string[]> | JsonRpcSuccess<string> | JsonRpcSuccess<boolean> | JsonRpcSuccess<null> | JsonRpcSuccess<never>>

addEthereumChain

addEthereumChain(payload): Promise<void>

Parameters

NameType
payloadAddEthereumChainParameter

Returns

Promise<void>


switchEthereumChain

switchEthereumChain(payload): Promise<void>

Parameters

NameType
payloadSwitchEthereumChainParameter

Returns

Promise<void>


postMessage

postMessage(payload): void

Parameters

NameType
payloadValueOf<PostMessagePayload>

Returns

void


getAccounts

getAccounts(): Promise<string[]>

Returns

Promise<string[]>


sendTransaction

sendTransaction(payload): Promise<Maybe<string>>

Parameters

NameType
payload[EthTransaction]

Returns

Promise<Maybe<string>>


createAccount

createAccount(): Promise<void>

Returns

Promise<void>


isUnlocked

isUnlocked(): Promise<boolean>

Returns

Promise<boolean>


walletHubOpen

walletHubOpen(payload): Promise<void>

Parameters

NameType
payloadWalletHubOpenPayload

Returns

Promise<void>


walletHubClose

walletHubClose(): Promise<void>

Returns

Promise<void>


walletHubOpenWallet

walletHubOpenWallet(payload): Promise<void>

Parameters

NameType
payloadWalletHubOpenWalletPayload

Returns

Promise<void>

EthJsonRpc

Ƭ EthJsonRpc: Object

Type declaration

NameType
unknownJsonRpc<"unknown", undefined | unknown, UnknownResponse>
eth_accountsJsonRpc<"eth_accounts", undefined, Address[]>
eth_coinbaseJsonRpc<"eth_coinbase", undefined, Address>
net_versionJsonRpc<"net_version", undefined, ChainId>
eth_chainIdJsonRpc<"eth_chainId", undefined, ChainIdHex>
eth_signJsonRpc<"eth_sign", [Address, Message], SignedSignature>
personal_signJsonRpc<"personal_sign", [Message], SignedSignature>
personal_ecRecoverJsonRpc<"personal_ecRecover", [Message, SignedSignature], Address>
eth_signTypedDataJsonRpc<"eth_signTypedData" | "eth_signTypedData_v3" | "eth_signTypedData_v4", [Address, string], SignedSignature>
eth_sendTransactionJsonRpc<"eth_sendTransaction", [EthTransaction], TransactionHash>
eth_sendRawTransactionJsonRpc<"eth_sendRawTransaction", [SignedTransaction], TransactionHash>
eth_signTransactionJsonRpc<"eth_signTransaction", [EthTransaction], SignedTransaction>
eth_requestAccountsJsonRpc<"eth_requestAccounts", undefined, Address[]>
eth_privateRequestAccountsJsonRpc<"eth_privateRequestAccounts", undefined, Address[]>
eth_getTransactionCountJsonRpc<"eth_getTransactionCount", [Address, BlockTag], string>
eth_gasPriceJsonRpc<"eth_gasPrice", undefined, string>
eth_estimateGasJsonRpc<"eth_estimateGas", [EthTransaction], string>
wallet_watchAssetJsonRpc<"wallet_watchAsset", WatchAssetParameter, boolean>
wallet_addEthereumChainJsonRpc<"wallet_addEthereumChain", [Pick<AddEthereumChainParameter, "chainName" | "nativeCurrency" | "rpcUrls" | "blockExplorerUrls" | "iconUrls"> & { chainId: ChainIdHex }], null>
wallet_switchEthereumChainJsonRpc<"wallet_switchEthereumChain", [Omit<SwitchEthereumChainParameter, "chainId"> & { chainId: ChainIdHex }], null>
eth_newFilterJsonRpc<"eth_newFilter", never, never>
eth_newBlockFilterJsonRpc<"eth_newBlockFilter", never, never>
eth_newPendingTransactionFilterJsonRpc<"eth_newPendingTransactionFilter", never, never>
eth_uninstallFilterJsonRpc<"eth_uninstallFilter", never, never>
eth_subscribeJsonRpc<"eth_subscribe", never, never>

Interface: JsonRpc<Method, Params, Result>

NameTypeDescription
requestJsonRpcRequest<Method, Params>
responseJsonRpcSuccess<Result>

UnknownResponse

Ƭ UnknownResponse: boolean | string | object | (string | object)[]


Address

Ƭ Address: string


ChainId

Ƭ ChainId: string


ChainIdHex

Ƭ ChainIdHex: string


Message

Ƭ Message: string


SignedSignature

Ƭ SignedSignature: string


Interface: EthTransaction

NameTypeDescription
fromstring
to?string
gas?string
gasPrice?string
value?string
datastring
nonce?string

TransactionHash

Ƭ TransactionHash: string


SignedTransaction

Ƭ SignedTransaction: string

BlockTag

Ƭ BlockTag: "pending" | "latest"


Interface: WatchAssetParameter

NameTypeDescription
type"ERC20"
optionsObject

Interface: AddEthereumChainParameter

NameTypeDescription
chainIdnumber
chainNamestring
nativeCurrencyObject
rpcUrlsstring[]
blockExplorerUrls?string[]
iconUrls?string[]

Interface: SwitchEthereumChainParameter

NameTypeDescription
chainIdnumber

JsonRpcCallback

Ƭ JsonRpcCallback<T>: (error: ProviderRpcError | Error | null, response: T | null) => void

Type parameters

NameType
TJsonRpcCallbackResponse

Type declaration

▸ (error, response): void

Parameters

NameType
errorProviderRpcError | Error | null
responseT | null

Returns

void


Class: ProviderRpcError

NameTypeDescription
codenumber
toString(): string

toString

toString(): string

Returns

string

JsonRpcCallbackResponse

Ƭ JsonRpcCallbackResponse: EthJsonRpcValue["response"][] | EthJsonRpcValue["response"] | EthJsonRpcValue["response"]["result"][] | EthJsonRpcValue["response"]["result"]


EthJsonRpcValue

Ƭ EthJsonRpcValue: Object

Type declaration

NameType
requestJsonRpcRequest<"unknown", unknown> | JsonRpcRequest<"eth_accounts", undefined> | JsonRpcRequest<"eth_coinbase", undefined> | JsonRpcRequest<"net_version", undefined> | JsonRpcRequest<"eth_chainId", undefined> | JsonRpcRequest<"eth_sign", [string, string]> | JsonRpcRequest<"personal_sign", [string]> | JsonRpcRequest<"personal_ecRecover", [string, string]> | JsonRpcRequest<"eth_signTypedData" | "eth_signTypedData_v3" | "eth_signTypedData_v4", [string, string]> | JsonRpcRequest<"eth_sendTransaction", [EthTransaction]> | JsonRpcRequest<"eth_sendRawTransaction", [string]> | JsonRpcRequest<"eth_signTransaction", [EthTransaction]> | JsonRpcRequest<"eth_requestAccounts", undefined> | JsonRpcRequest<"eth_privateRequestAccounts", undefined> | JsonRpcRequest<"eth_getTransactionCount", [string, BlockTag]> | JsonRpcRequest<"eth_gasPrice", undefined> | JsonRpcRequest<"eth_estimateGas", [EthTransaction]> | JsonRpcRequest<"wallet_watchAsset", WatchAssetParameter> | JsonRpcRequest<"wallet_addEthereumChain", [Pick<AddEthereumChainParameter, "chainName" | "nativeCurrency" | "rpcUrls" | "blockExplorerUrls" | "iconUrls"> & { chainId: string }]> | JsonRpcRequest<"wallet_switchEthereumChain", [Omit<SwitchEthereumChainParameter, "chainId"> & { chainId: string }]> | JsonRpcRequest<"eth_newFilter", never> | JsonRpcRequest<"eth_newBlockFilter", never> | JsonRpcRequest<"eth_newPendingTransactionFilter", never> | JsonRpcRequest<"eth_uninstallFilter", never> | JsonRpcRequest<"eth_subscribe", never>
responseJsonRpcSuccess<UnknownResponse> | JsonRpcSuccess<string[]> | JsonRpcSuccess<string> | JsonRpcSuccess<boolean> | JsonRpcSuccess<null> | JsonRpcSuccess<never>

Interface: JsonRpcRequest<Method, Params>

NameTypeDescription
jsonrpc"2.0"
methodMethod
idnumber
paramsParams extends undefined ? [] : Params

Interface: JsonRpcSuccess<T>

NameTypeDescription
resultMaybe<T>
jsonrpc"2.0"
idnumber

UnknownResponse

Ƭ UnknownResponse: boolean | string | object | (string | object)[]


BlockTag

Ƭ BlockTag: "pending" | "latest"


V3

V3: Object = "V3"


V4

V4: Object = "V4"


Class: ProviderRpcError

NameTypeDescription
codenumber
toString(): string

toString

toString(): string

Returns

string