Skip to content

Commit 1dcbb25

Browse files
committed
Forward requests from provider to wallet provider
1 parent af7dd23 commit 1dcbb25

File tree

4 files changed

+186
-168
lines changed

4 files changed

+186
-168
lines changed

src/BlockcoreProvider.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,38 @@ export class BlockcoreProvider {
1717
// this.baseUrl = provider;
1818
// }
1919

20-
on(event: string, callback: unknown) {
21-
console.log(event, callback);
22-
// "accountsChanged"
23-
// "chainChanged"
24-
// "networkChanged"
25-
}
20+
// on(event: string, callback: unknown) {
21+
// console.log(event, callback);
22+
// // "accountsChanged"
23+
// // "chainChanged"
24+
// // "networkChanged"
25+
// }
2626

27-
getAccounts() {
28-
return null;
29-
}
27+
// getAccounts() {
28+
// return null;
29+
// }
3030

31-
chainId() {
32-
return null;
33-
}
31+
// chainId() {
32+
// return null;
33+
// }
3434

35-
sendTransaction() {
36-
return null;
37-
}
35+
// sendTransaction() {
36+
// return null;
37+
// }
3838

39-
signTransaction() {
40-
return null;
41-
}
39+
// signTransaction() {
40+
// return null;
41+
// }
4242

43-
sign() {
44-
return null;
45-
}
43+
// sign() {
44+
// return null;
45+
// }
4646

47-
async signTypedData(params?: unknown[] | object) {
48-
console.log('SIGN DATA WITH PARAMS:', params);
47+
// async signTypedData(params?: unknown[] | object) {
48+
// console.log('SIGN DATA WITH PARAMS:', params);
4949

50-
return {};
51-
}
50+
// return {};
51+
// }
5252

5353
// public setNetwork(network: string): void {
5454
// this.baseUrl = this.getNetworkUrl(network);

src/WebProvider.ts

Lines changed: 71 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -35,66 +35,84 @@ export class WebProvider implements EIP1193Provider {
3535
this.indexer.setNetwork(network);
3636
}
3737

38-
on(event: string, callback: unknown) {
39-
console.log(event, callback);
40-
// "accountsChanged"
41-
// "chainChanged"
42-
// "networkChanged"
43-
}
38+
// on(event: string, callback: unknown) {
39+
// console.log(event, callback);
40+
// // "accountsChanged"
41+
// // "chainChanged"
42+
// // "networkChanged"
43+
// }
4444

4545
// eip-1193: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1193.md
46+
/** Use this method to send a request directly to the wallet extension. */
4647
public async request(args: RequestArguments): Promise<unknown> {
47-
let param0 = null;
48+
const gthis = globalThis as any;
49+
const blockcore = gthis.blockcore;
4850

49-
if (Array.isArray(args.params)) {
50-
param0 = args.params[0];
51-
} else {
52-
param0 = args.params;
51+
if (!blockcore) {
52+
alert('The Blockcore provider is not available. Unable to continue.');
53+
return;
5354
}
5455

55-
// params: [from, JSON.stringify(msgParams)]
56-
57-
switch (args.method) {
58-
case 'wallet_requestPermissions': // eip-2255 - https://eips.ethereum.org/EIPS/eip-2255
59-
return [
60-
{
61-
invoker: 'ens://your-site.eth',
62-
parentCapability: 'eth_accounts',
63-
caveats: [
64-
{
65-
type: 'filterResponse',
66-
value: ['0x0c54fccd2e384b4bb6f2e405bf5cbc15a017aafb'],
67-
},
68-
],
69-
},
70-
];
71-
case 'wallet_getPermissions': // eip-2255 - https://eips.ethereum.org/EIPS/eip-2255
72-
return [
73-
{
74-
invoker: 'ens://your-site.eth',
75-
parentCapability: 'eth_accounts',
76-
caveats: [
77-
{
78-
type: 'filterResponse',
79-
value: ['0x0c54fccd2e384b4bb6f2e405bf5cbc15a017aafb'],
80-
},
81-
],
82-
},
83-
];
84-
case 'signTypedData':
85-
case 'signTypedData_v4':
86-
return this.provider.signTypedData(args.params);
87-
case 'requestPermissions': //
88-
return null;
89-
case 'getTransactionByHash':
90-
return this.indexer.getBlockTransactionsByHash(param0.transactionHash);
91-
case 'getBlockByHash':
92-
return this.indexer.getBlockByHash(param0.blockHash); // TODO: Add support for "includeTransactions".
93-
case 'getBlockByNumber':
94-
return this.indexer.getBlockByIndex(param0.blockNumber); // TODO: Add support for "includeTransactions".
95-
default:
96-
return null;
56+
let result;
57+
58+
try {
59+
result = await blockcore.request(args);
60+
} catch (err: any) {
61+
console.error(err);
62+
result = 'Error: ' + err.message;
9763
}
64+
65+
return result;
66+
67+
// let param0 = null;
68+
69+
// if (Array.isArray(args.params)) {
70+
// param0 = args.params[0];
71+
// } else {
72+
// param0 = args.params;
73+
// }
74+
75+
// switch (args.method) {
76+
// case 'wallet_requestPermissions': // eip-2255 - https://eips.ethereum.org/EIPS/eip-2255
77+
// return [
78+
// {
79+
// invoker: 'ens://your-site.eth',
80+
// parentCapability: 'eth_accounts',
81+
// caveats: [
82+
// {
83+
// type: 'filterResponse',
84+
// value: ['0x0c54fccd2e384b4bb6f2e405bf5cbc15a017aafb'],
85+
// },
86+
// ],
87+
// },
88+
// ];
89+
// case 'wallet_getPermissions': // eip-2255 - https://eips.ethereum.org/EIPS/eip-2255
90+
// return [
91+
// {
92+
// invoker: 'ens://your-site.eth',
93+
// parentCapability: 'eth_accounts',
94+
// caveats: [
95+
// {
96+
// type: 'filterResponse',
97+
// value: ['0x0c54fccd2e384b4bb6f2e405bf5cbc15a017aafb'],
98+
// },
99+
// ],
100+
// },
101+
// ];
102+
// case 'signTypedData':
103+
// case 'signTypedData_v4':
104+
// return this.provider.signTypedData(args.params);
105+
// case 'requestPermissions': //
106+
// return null;
107+
// case 'getTransactionByHash':
108+
// return this.indexer.getBlockTransactionsByHash(param0.transactionHash);
109+
// case 'getBlockByHash':
110+
// return this.indexer.getBlockByHash(param0.blockHash); // TODO: Add support for "includeTransactions".
111+
// case 'getBlockByNumber':
112+
// return this.indexer.getBlockByIndex(param0.blockNumber); // TODO: Add support for "includeTransactions".
113+
// default:
114+
// return null;
115+
// }
98116
}
99117

100118
// public setNetwork(network: string): void {

test/provider-blockcore.test.ts

Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -13,74 +13,74 @@ test('get CITY network', async (t) => {
1313
t.assert(network.isProofOfStake === true);
1414
});
1515

16-
test('perform signing request', async (t) => {
17-
let provider = new BlockcoreProvider();
16+
// test('perform signing request', async (t) => {
17+
// let provider = new BlockcoreProvider();
1818

19-
const msgParams = JSON.stringify({
20-
domain: {
21-
// Defining the chain aka Rinkeby testnet or Ethereum Main Net
22-
chainId: 1,
23-
// Give a user friendly name to the specific contract you are signing for.
24-
name: 'Ether Mail',
25-
// If name isn't enough add verifying contract to make sure you are establishing contracts with the proper entity
26-
verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
27-
// Just let's you know the latest version. Definitely make sure the field name is correct.
28-
version: '1',
29-
},
19+
// const msgParams = JSON.stringify({
20+
// domain: {
21+
// // Defining the chain aka Rinkeby testnet or Ethereum Main Net
22+
// chainId: 1,
23+
// // Give a user friendly name to the specific contract you are signing for.
24+
// name: 'Ether Mail',
25+
// // If name isn't enough add verifying contract to make sure you are establishing contracts with the proper entity
26+
// verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
27+
// // Just let's you know the latest version. Definitely make sure the field name is correct.
28+
// version: '1',
29+
// },
3030

31-
// Defining the message signing data content.
32-
message: {
33-
/*
34-
- Anything you want. Just a JSON Blob that encodes the data you want to send
35-
- No required fields
36-
- This is DApp Specific
37-
- Be as explicit as possible when building out the message schema.
38-
*/
39-
contents: 'Hello, Bob!',
40-
attachedMoneyInEth: 4.2,
41-
from: {
42-
name: 'Cow',
43-
wallets: ['0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826', '0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF'],
44-
},
45-
to: [
46-
{
47-
name: 'Bob',
48-
wallets: ['0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB', '0xB0BdaBea57B0BDABeA57b0bdABEA57b0BDabEa57', '0xB0B0b0b0b0b0B000000000000000000000000000'],
49-
},
50-
],
51-
},
52-
// Refers to the keys of the *types* object below.
53-
primaryType: 'Mail',
54-
types: {
55-
// TODO: Clarify if EIP712Domain refers to the domain the contract is hosted on
56-
EIP712Domain: [
57-
{ name: 'name', type: 'string' },
58-
{ name: 'version', type: 'string' },
59-
{ name: 'chainId', type: 'uint256' },
60-
{ name: 'verifyingContract', type: 'address' },
61-
],
62-
// Not an EIP712Domain definition
63-
Group: [
64-
{ name: 'name', type: 'string' },
65-
{ name: 'members', type: 'Person[]' },
66-
],
67-
// Refer to PrimaryType
68-
Mail: [
69-
{ name: 'from', type: 'Person' },
70-
{ name: 'to', type: 'Person[]' },
71-
{ name: 'contents', type: 'string' },
72-
],
73-
// Not an EIP712Domain definition
74-
Person: [
75-
{ name: 'name', type: 'string' },
76-
{ name: 'wallets', type: 'address[]' },
77-
],
78-
},
79-
});
31+
// // Defining the message signing data content.
32+
// message: {
33+
// /*
34+
// - Anything you want. Just a JSON Blob that encodes the data you want to send
35+
// - No required fields
36+
// - This is DApp Specific
37+
// - Be as explicit as possible when building out the message schema.
38+
// */
39+
// contents: 'Hello, Bob!',
40+
// attachedMoneyInEth: 4.2,
41+
// from: {
42+
// name: 'Cow',
43+
// wallets: ['0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826', '0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF'],
44+
// },
45+
// to: [
46+
// {
47+
// name: 'Bob',
48+
// wallets: ['0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB', '0xB0BdaBea57B0BDABeA57b0bdABEA57b0BDabEa57', '0xB0B0b0b0b0b0B000000000000000000000000000'],
49+
// },
50+
// ],
51+
// },
52+
// // Refers to the keys of the *types* object below.
53+
// primaryType: 'Mail',
54+
// types: {
55+
// // TODO: Clarify if EIP712Domain refers to the domain the contract is hosted on
56+
// EIP712Domain: [
57+
// { name: 'name', type: 'string' },
58+
// { name: 'version', type: 'string' },
59+
// { name: 'chainId', type: 'uint256' },
60+
// { name: 'verifyingContract', type: 'address' },
61+
// ],
62+
// // Not an EIP712Domain definition
63+
// Group: [
64+
// { name: 'name', type: 'string' },
65+
// { name: 'members', type: 'Person[]' },
66+
// ],
67+
// // Refer to PrimaryType
68+
// Mail: [
69+
// { name: 'from', type: 'Person' },
70+
// { name: 'to', type: 'Person[]' },
71+
// { name: 'contents', type: 'string' },
72+
// ],
73+
// // Not an EIP712Domain definition
74+
// Person: [
75+
// { name: 'name', type: 'string' },
76+
// { name: 'wallets', type: 'address[]' },
77+
// ],
78+
// },
79+
// });
8080

81-
const account = {};
81+
// const account = {};
8282

83-
const result = await provider.signTypedData([account, msgParams]);
83+
// const result = await provider.signTypedData([account, msgParams]);
8484

85-
t.assert(result != null);
86-
});
85+
// t.assert(result != null);
86+
// });

test/provider-web.test.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,30 @@ const createInstance = async () => {
1212
return webProvider;
1313
};
1414

15-
test.serial('request: should get permissions', async (t) => {
16-
let webProvider = await createInstance();
17-
18-
const response = await webProvider.request({
19-
method: 'wallet_getPermissions',
20-
});
21-
22-
t.assert(response != null);
23-
});
24-
25-
test.serial('request: should request permissions', async (t) => {
26-
let webProvider = await createInstance();
27-
28-
const response = await webProvider.request({
29-
method: 'wallet_requestPermissions',
30-
params: [
31-
{
32-
eth_accounts: {},
33-
},
34-
],
35-
});
36-
37-
t.assert(response != null);
38-
});
15+
// test.serial('request: should get permissions', async (t) => {
16+
// let webProvider = await createInstance();
17+
18+
// const response = await webProvider.request({
19+
// method: 'wallet_getPermissions',
20+
// });
21+
22+
// t.assert(response != null);
23+
// });
24+
25+
// test.serial('request: should request permissions', async (t) => {
26+
// let webProvider = await createInstance();
27+
28+
// const response = await webProvider.request({
29+
// method: 'wallet_requestPermissions',
30+
// params: [
31+
// {
32+
// eth_accounts: {},
33+
// },
34+
// ],
35+
// });
36+
37+
// t.assert(response != null);
38+
// });
3939

4040
test.serial('should use indexer through the WebProvider', async (t) => {
4141
let webProvider = await createInstance();

0 commit comments

Comments
 (0)