Skip to content

Commit

Permalink
Merge pull request #4 from wharfkit/allowing-snap-origin-arg
Browse files Browse the repository at this point in the history
Allowing snap origin arg on plugin initialization
  • Loading branch information
dafuga authored Aug 22, 2024
2 parents 08ec3bb + 2de978b commit 433d7df
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 83 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [14, 16, 18]
node-version: [18, 20]
name: Node.js v${{ matrix.node-version }}
steps:
- name: Setup Node.js
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@wharfkit/wallet-plugin-metamask",
"description": "A MetaMask plugin for use with @wharfkit/session.",
"version": "1.0.0-rc3",
"version": "1.0.0-rc6",
"homepage": "https://github.com/wharfkit/wallet-plugin-metamask",
"license": "BSD-3-Clause",
"main": "lib/wallet-plugin-metamask.js",
Expand All @@ -20,7 +20,7 @@
"tslib": "^2.1.0"
},
"peerDependencies": {
"@wharfkit/session": "^1.1.0"
"@wharfkit/session": "^1.4.0"
},
"devDependencies": {
"@rollup/plugin-alias": "^3.1.4",
Expand All @@ -36,7 +36,7 @@
"@typescript-eslint/eslint-plugin": "^5.20.0",
"@typescript-eslint/parser": "^5.20.0",
"@wharfkit/mock-data": "^1.2.0",
"@wharfkit/session": "^1.4.0-rc3",
"@wharfkit/session": "^1.4.0",
"chai": "^4.3.4",
"eslint": "^8.13.0",
"eslint-config-prettier": "^8.1.0",
Expand Down
12 changes: 12 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ export type GetSnapsResponse = Record<string, Snap>

export * from './metamask'

interface WalletPluginMetaMaskConfig {
snapOrigin?: string
}

const defaultSnapOrigin = 'local:http://localhost:8080'
// const defaultSnapOrigin = 'npm:@greymass/test-snap'

export class WalletPluginMetaMask extends AbstractWalletPlugin implements WalletPlugin {
public id = 'wallet-plugin-metamask'
public snapOrigin: string

readonly config: WalletPluginConfig = {
requiresChainSelect: true,
requiresPermissionSelect: true,
Expand All @@ -39,6 +45,12 @@ export class WalletPluginMetaMask extends AbstractWalletPlugin implements Wallet
download: '',
})

constructor(walletPluginMetaMaskConfig?: WalletPluginMetaMaskConfig) {
super()

this.snapOrigin = walletPluginMetaMaskConfig?.snapOrigin || defaultSnapOrigin
}

login(context: LoginContext): Cancelable<WalletPluginLoginResponse> {
const promise = this.metamaskLogin(context)
return cancelable(promise, (canceled) => {
Expand Down
17 changes: 0 additions & 17 deletions test/data/3de7f859450540bb5aa2948cf30b14815e73623e.json

This file was deleted.

33 changes: 33 additions & 0 deletions test/data/a041de03f2a7ee6c133465c8c6b2b286704a5d8f.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"request": {
"path": "https://jungle4.greymass.com/v1/chain/get_info",
"params": {
"method": "GET",
"headers": {}
}
},
"status": 200,
"json": {
"server_version": "17b89a28",
"chain_id": "73e4385a2708e6d7048834fbc1079f2fabb17b3c125b146af438971e90716c4d",
"head_block_num": 156088678,
"last_irreversible_block_num": 156088343,
"last_irreversible_block_id": "094db817b49cfc17bf85a65bc86c3fe70b2bb18030a0ca863c4b2abb09c0fb8c",
"head_block_id": "094db96623b26ac406bd612b3160e80cda4963332a39e4d23005deeb094e34e1",
"head_block_time": "2024-08-16T19:26:05.000",
"head_block_producer": "eosriobrazil",
"virtual_block_cpu_limit": 200000000,
"virtual_block_net_limit": 1048576000,
"block_cpu_limit": 200000,
"block_net_limit": 1048576,
"server_version_string": "v1.0.0-beta2.1",
"fork_db_head_block_num": 156088678,
"fork_db_head_block_id": "094db96623b26ac406bd612b3160e80cda4963332a39e4d23005deeb094e34e1",
"server_full_version_string": "v1.0.0-beta2.1-17b89a28b525826db2986bc20106a6e8202b22a9",
"total_cpu_weight": "120573498838233",
"total_net_weight": "117542107821577",
"earliest_available_block_num": 155899613,
"last_irreversible_block_time": "2024-08-16T19:23:17.500"
},
"text": "{\"server_version\":\"17b89a28\",\"chain_id\":\"73e4385a2708e6d7048834fbc1079f2fabb17b3c125b146af438971e90716c4d\",\"head_block_num\":156088678,\"last_irreversible_block_num\":156088343,\"last_irreversible_block_id\":\"094db817b49cfc17bf85a65bc86c3fe70b2bb18030a0ca863c4b2abb09c0fb8c\",\"head_block_id\":\"094db96623b26ac406bd612b3160e80cda4963332a39e4d23005deeb094e34e1\",\"head_block_time\":\"2024-08-16T19:26:05.000\",\"head_block_producer\":\"eosriobrazil\",\"virtual_block_cpu_limit\":200000000,\"virtual_block_net_limit\":1048576000,\"block_cpu_limit\":200000,\"block_net_limit\":1048576,\"server_version_string\":\"v1.0.0-beta2.1\",\"fork_db_head_block_num\":156088678,\"fork_db_head_block_id\":\"094db96623b26ac406bd612b3160e80cda4963332a39e4d23005deeb094e34e1\",\"server_full_version_string\":\"v1.0.0-beta2.1-17b89a28b525826db2986bc20106a6e8202b22a9\",\"total_cpu_weight\":\"120573498838233\",\"total_net_weight\":\"117542107821577\",\"earliest_available_block_num\":155899613,\"last_irreversible_block_time\":\"2024-08-16T19:23:17.500\"}"
}
32 changes: 0 additions & 32 deletions test/data/a91ac04b00106a44611383931aef281f37cd304e.json

This file was deleted.

18 changes: 18 additions & 0 deletions test/data/ed314e8a151304a30043198b7b9e9df34a14aaeb.json

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions test/tests/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ import {
mockSessionKitOptions,
} from '@wharfkit/mock-data'

import {WalletPluginTEMPLATE} from '$lib'
import {WalletPluginMetaMask} from '$lib'
import {setupEthereumMock} from './mocks/ethereum'

suite('wallet plugin', function () {
setup(function () {
setupEthereumMock()
})

test('login and sign', async function () {
const kit = new SessionKit(
{
...mockSessionKitArgs,
walletPlugins: [new WalletPluginTEMPLATE()],
walletPlugins: [new WalletPluginMetaMask()],
},
mockSessionKitOptions
)
Expand Down
42 changes: 42 additions & 0 deletions test/tests/mocks/ethereum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/// <reference path="../../../global.d.ts" />

import {MetaMaskInpageProvider} from '@metamask/providers'

class MockMetaMaskInpageProvider implements Partial<MetaMaskInpageProvider> {
request(args: {method: string; params?: any}) {
switch (args.method) {
case 'wallet_getSnaps':
return Promise.resolve({})
case 'wallet_requestSnaps':
return Promise.resolve({
'local:http://localhost:8080': {
id: 'local:http://localhost:8080',
version: '1.0.0',
},
})
case 'wallet_invokeSnap':
if (args.params.request.method === 'antelope_getPublicKey') {
return Promise.resolve(
'PUB_K1_6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5BoDq63'
)
}
if (args.params.request.method === 'antelope_signTransaction') {
return Promise.resolve(
'SIG_K1_KfCdjsrTnx5cBpbA5cUdHZAsRYsnC9uKzuS1shFeqfMCfdZwX4PBm9pfHwGRT6ffz3eavhtkyNci5GoFozQAx8P8PBnDmj'
)
}
return Promise.resolve(null)
case 'web3_clientVersion':
return Promise.resolve(['MetaMask/v10.8.1'])
default:
return Promise.resolve(null)
}
}
}

const mockProvider = new MockMetaMaskInpageProvider()

export function setupEthereumMock() {
global.window = global.window || {}
global.window.ethereum = mockProvider as any
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
"strict": true,
"target": "es2020"
},
"include": ["src/**/*"]
"include": ["src/**/*", "global.d.ts"]
}
54 changes: 27 additions & 27 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,18 @@
pako "^2.1.0"
tslib "^2.0.3"

"@wharfkit/antelope@^1.0.11", "@wharfkit/antelope@^1.0.5":
version "1.0.11"
resolved "https://registry.yarnpkg.com/@wharfkit/antelope/-/antelope-1.0.11.tgz#a830b3726b33d4faf4867add244013bf5e51fea3"
integrity sha512-4oSM5b9IzQxP6hC5H6QGBCylTYKs+cZ8fg6piM8qjj/g4Ok3YmYwx6sbIQwwQmdCUK9sBweKXrOHjNwpyafPBQ==
dependencies:
bn.js "^4.11.9"
brorand "^1.1.0"
elliptic "^6.5.4"
hash.js "^1.0.0"
pako "^2.1.0"
tslib "^2.0.3"

"@wharfkit/antelope@^1.0.2", "@wharfkit/antelope@^1.0.4", "@wharfkit/antelope@^1.0.7":
version "1.0.7"
resolved "https://registry.yarnpkg.com/@wharfkit/antelope/-/antelope-1.0.7.tgz#5ca010db963e061b2e8c47c14e55f86817718c2e"
Expand Down Expand Up @@ -769,13 +781,13 @@
tslib "^2.1.0"

"@wharfkit/mock-data@^1.2.0":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@wharfkit/mock-data/-/mock-data-1.2.0.tgz#fa5749f1332e413ecd4126dedf1d17724840df41"
integrity sha512-R779GKbzVeJFvO/VMLnjSua4YteQiuJ1WX/MjEeo2ftP0aPgaLNEoFKGa7veAvYEv/6mgEcS4Sm5gyFi1Enxhg==
version "1.3.0"
resolved "https://registry.yarnpkg.com/@wharfkit/mock-data/-/mock-data-1.3.0.tgz#7a9caa7c7fb2ad839bc12593de1b7a546b673ae6"
integrity sha512-LkhAkrUOvG6o+lPWb2Q6JCrs9++F2XowGK42PODh35Xu9an5H+THMGjpUhTC1sPiYkfjhkmBbAzMpZbHjkXj4w==
dependencies:
"@wharfkit/antelope" "^1.0.0"
"@wharfkit/session" "^1.1.0-rcfinal"
"@wharfkit/wallet-plugin-privatekey" "^1.0.0"
"@wharfkit/antelope" "^1.0.5"
"@wharfkit/session" "^1.2.7"
"@wharfkit/wallet-plugin-privatekey" "^1.1.0"
node-fetch "^2.6.1"
tslib "^2.1.0"

Expand All @@ -789,26 +801,14 @@
js-big-decimal "^2.0.7"
tslib "^2.1.0"

"@wharfkit/session@^1.1.0-rcfinal":
version "1.1.0-rcfinal"
resolved "https://registry.yarnpkg.com/@wharfkit/session/-/session-1.1.0-rcfinal.tgz#29a9f40dca8557e96d5e44c1498b09eb641f47e1"
integrity sha512-UijDlP2yE8WoprAXz8EpAyz1GyxWv9OloRGApTUKM/lMuSk1U2d7vefQzyXTn14f1npd7bfBzeTQ5piTerBHww==
dependencies:
"@wharfkit/abicache" "^1.2.0"
"@wharfkit/antelope" "^1.0.0"
"@wharfkit/common" "^1.2.0"
"@wharfkit/signing-request" "^3.1.0"
pako "^2.0.4"
tslib "^2.1.0"

"@wharfkit/session@^1.4.0-rc3":
version "1.4.0-rc3"
resolved "https://registry.yarnpkg.com/@wharfkit/session/-/session-1.4.0-rc3.tgz#1d1594c4ee3b81186773e996e24f41f4cc2821ff"
integrity sha512-3pWgNehl4auJz8MJ7NpdzndeTfZ95KLBngAMK2rwrFpaiiinywExiL6m+HTPEY95mXqNiWuMHyUW1Xrgn8/U9w==
"@wharfkit/session@^1.2.7", "@wharfkit/session@^1.4.0":
version "1.4.0"
resolved "https://registry.yarnpkg.com/@wharfkit/session/-/session-1.4.0.tgz#13a44a0d23d5ee689755985580f943f869352987"
integrity sha512-U+K9eWbYzFeDshYwgrhyNoF47kYToKFpTe6xFAwaJO+2w0kIkP0vtJFl7ygd06muh7Hu3K1BqrZ3j6+FyyuIyA==
dependencies:
"@wharfkit/abicache" "^1.2.1"
"@wharfkit/account" "^1.0.0"
"@wharfkit/antelope" "^1.0.2"
"@wharfkit/antelope" "^1.0.11"
"@wharfkit/common" "^1.2.0"
"@wharfkit/signing-request" "^3.1.0"
pako "^2.0.4"
Expand All @@ -832,10 +832,10 @@
bn.js "^4.11.9"
tslib "^2.1.0"

"@wharfkit/wallet-plugin-privatekey@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@wharfkit/wallet-plugin-privatekey/-/wallet-plugin-privatekey-1.0.0.tgz#2600cce1117ce9391c8078649e05ceaf93780f1d"
integrity sha512-V+/7T/cwoHM8fDaM3MZ1DFKrX2+NddBkkWJ8BIFfmEZnGR1W8Qr77t+piOP0/6UM2etmuZh98XLwZS33vORQ0A==
"@wharfkit/wallet-plugin-privatekey@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@wharfkit/wallet-plugin-privatekey/-/wallet-plugin-privatekey-1.1.0.tgz#5985bff61895c54d2afbef359cd42da4f3871c7d"
integrity sha512-45LPj7AOVDm4RugDEhy0fnQX/BcMffeJPjGUCUrLazJ2S0Sti8nNk4nqiJqyme84c/0gq7d65vvwlmVfGtPVEg==
dependencies:
tslib "^2.1.0"

Expand Down

0 comments on commit 433d7df

Please sign in to comment.