Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public async onConnectionPayload (payload: JsonRpc) {
const { id } = payload
if (typeof id !== 'undefined') {
if (this.promises[id]) {
if (isJsonRpcResponseError(payload)) {
this.promises[id].reject(payload.error)
} else if (isJsonRpcResponseSuccess(payload)) {
this.promises[id].resolve(payload.result)
}
delete this.promises[id]
}
} else if (isJsonRpcSubscription(payload)) {
if (payload.method && payload.method.indexOf('_subscription') > -1) {
// Emit subscription result
this.emit(payload.params.subscription, payload.params.result)
this.emit(payload.method, payload.params) // Latest EIP-1193
this.emit('data', payload) // Backwards Compatibility
}
}
}
public async checkConnection () {
public async onConnectionPayload (payload: JsonRpc) {
const { id } = payload
if (typeof id !== 'undefined') {
if (this.promises[id]) {
if (isJsonRpcResponseError(payload)) {
this.promises[id].reject(payload.error)
} else if (isJsonRpcResponseSuccess(payload)) {
this.promises[id].resolve(payload.result)
}
delete this.promises[id]
}
} else if (isJsonRpcSubscription(payload)) {
if (payload.method && payload.method.indexOf('_subscription') > -1) {
// Emit subscription result
this.emit(payload.params.subscription, payload.params.result)
this.emit(payload.method, payload.params) // Latest EIP-1193
this.emit('data', payload) // Backwards Compatibility
}
}
}
public async checkConnection () {
public async onConnectionPayload (payload: JsonRpc) {
const { id } = payload
if (typeof id !== 'undefined') {
if (this.promises[id]) {
if (isJsonRpcResponseError(payload)) {
this.promises[id].reject(payload.error)
} else if (isJsonRpcResponseSuccess(payload)) {
this.promises[id].resolve(payload.result)
}
delete this.promises[id]
}
} else if (isJsonRpcSubscription(payload)) {
if (payload.method && payload.method.indexOf('_subscription') > -1) {
// Emit subscription result
this.emit(payload.params.subscription, payload.params.result)
this.emit(payload.method, payload.params) // Latest EIP-1193
this.emit('data', payload) // Backwards Compatibility
}
}
}
public async checkConnection () {
public async onConnectionPayload (payload: JsonRpc) {
const { id } = payload
if (typeof id !== 'undefined') {
if (this.promises[id]) {
if (isJsonRpcResponseError(payload)) {
this.promises[id].reject(payload.error)
} else if (isJsonRpcResponseSuccess(payload)) {
this.promises[id].resolve(payload.result)
}
delete this.promises[id]
}
} else if (isJsonRpcSubscription(payload)) {
if (payload.method && payload.method.indexOf('_subscription') > -1) {
// Emit subscription result
this.emit(payload.params.subscription, payload.params.result)
this.emit(payload.method, payload.params) // Latest EIP-1193
this.emit('data', payload) // Backwards Compatibility
}
}
}
public async checkConnection () {
public sendAsync (payload: JsonRpc, cb: any) {
// Backwards Compatibility
if (!cb || typeof cb !== 'function') {
return cb(
new Error('Invalid or undefined callback provided to sendAsync')
)
}
if (!payload) {
return cb(new Error('Invalid Payload'))
}
// sendAsync can be called with an array for batch requests used by web3.js 0.x
// this is not part of EIP-1193's backwards compatibility but we still want to support it
if (payload instanceof Array) {
return this.sendAsyncBatch(payload, cb)
} else if (isJsonRpcRequest(payload)) {
return this._send(payload.method, payload.params)
.then(result => {
cb(null, { id: payload.id, jsonrpc: payload.jsonrpc, result })
})
.catch(err => {
cb(err)
})
}
}
public sendAsyncBatch (requests: JsonRpc[], cb: any) {
public sendAsync (payload: JsonRpc, cb: any) {
// Backwards Compatibility
if (!cb || typeof cb !== 'function') {
return cb(
new Error('Invalid or undefined callback provided to sendAsync')
)
}
if (!payload) {
return cb(new Error('Invalid Payload'))
}
// sendAsync can be called with an array for batch requests used by web3.js 0.x
// this is not part of EIP-1193's backwards compatibility but we still want to support it
if (payload instanceof Array) {
return this.sendAsyncBatch(payload, cb)
} else if (isJsonRpcRequest(payload)) {
return this._send(payload.method, payload.params)
.then(result => {
cb(null, { id: payload.id, jsonrpc: payload.jsonrpc, result })
})
.catch(err => {
cb(err)
})
}
}
public sendAsyncBatch (requests: JsonRpc[], cb: any) {
transaction,
assets,
nativeCurrency,
timestampInMs
);
}
if (payload.method === SIGN) {
const message = get(payload, 'params[1]');
const result = getMessageDisplayDetails(message, timestampInMs);
return result;
}
if (payload.method === PERSONAL_SIGN) {
let message = get(payload, 'params[0]');
try {
if (isHexString(message)) {
message = convertHexToUtf8(message);
}
} catch (error) {
// TODO error handling
}
return getMessageDisplayDetails(message, timestampInMs);
}
if (payload.method === SIGN_TYPED || payload.method === SIGN_TYPED_V3) {
const request = get(payload, 'params[1]', null);
const jsonRequest = JSON.stringify(request.message);
return getMessageDisplayDetails(jsonRequest, timestampInMs);
}
return {};
};
public testSignPersonalMessage = async () => {
const { walletConnector, address } = this.state;
if (!walletConnector) {
return;
}
// test message
const message = "My email is john@doe.com - 1537836206101";
// encode message (hex)
const hexMsg = convertUtf8ToHex(message);
// personal_sign params
const msgParams = [hexMsg, address];
try {
// open modal
this.toggleModal();
// toggle pending request indicator
this.setState({ pendingRequest: true });
// send message
const result = await walletConnector.signPersonalMessage(msgParams);
// verify signature
const signer = recoverPersonalSignature(result, message);
public testSignPersonalMessage = async () => {
const { web3, address } = this.state;
if (!web3) {
return;
}
// test message
const message = "My email is john@doe.com - 1537836206101";
// encode message (hex)
const hexMsg = convertUtf8ToHex(message);
try {
// open modal
this.toggleModal();
// toggle pending request indicator
this.setState({ pendingRequest: true });
// send message
const result = await web3.eth.personal.sign(hexMsg, address);
// verify signature
const signer = recoverPersonalSignature(result, message);
const verified = signer.toLowerCase() === address.toLowerCase();
// format displayed result
let params = [{ label: "Method", value: displayRequest.method }];
switch (displayRequest.method) {
case "eth_sendTransaction":
case "eth_signTransaction":
params = [
...params,
{ label: "From", value: displayRequest.params[0].from },
{ label: "To", value: displayRequest.params[0].to },
{
label: "Gas Limit",
value: displayRequest.params[0].gas
? convertHexToNumber(displayRequest.params[0].gas)
: displayRequest.params[0].gasLimit
? convertHexToNumber(displayRequest.params[0].gasLimit)
: ""
},
{
label: "Gas Price",
value: convertHexToNumber(displayRequest.params[0].gasPrice)
},
{
label: "Nonce",
value: convertHexToNumber(displayRequest.params[0].nonce)
},
{
label: "Value",
value: convertHexToNumber(displayRequest.params[0].value)
},
{ label: "Data", value: displayRequest.params[0].data }
];