Skip to content

Commit

Permalink
Merge pull request #5 from wharfkit/dynamically-loading-scatter
Browse files Browse the repository at this point in the history
Loading scatter protocol dynamically
  • Loading branch information
aaroncox authored Sep 26, 2024
2 parents ca34fe7 + 50f5f03 commit 33b2a53
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 94 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"version": "1.3.1",
"homepage": "https://github.com/wharfkit/wallet-plugin-tokenpocket",
"license": "BSD-3-Clause",
"main": "lib/wallet-plugin-tokenpocket.js",
"module": "lib/wallet-plugin-tokenpocket.m.js",
"types": "lib/wallet-plugin-tokenpocket.d.ts",
"main": "lib/cjs/index.js",
"module": "lib/esm/index.js",
"types": "lib/types/index.d.ts",
"sideEffects": false,
"files": [
"lib/*",
Expand All @@ -20,7 +20,7 @@
"tslib": "^2.1.0"
},
"peerDependencies": {
"@wharfkit/session": "^1.1.0"
"@wharfkit/session": "^1.4.0"
},
"devDependencies": {
"@babel/runtime": "^7.22.6",
Expand All @@ -34,7 +34,7 @@
"@types/node": "^18.7.18",
"@typescript-eslint/eslint-plugin": "^5.20.0",
"@typescript-eslint/parser": "^5.20.0",
"@wharfkit/session": "^1.3.1",
"@wharfkit/session": "^1.4.0",
"chai": "^4.3.4",
"crypto-js": "^4.1.1",
"eslint": "^8.13.0",
Expand Down
6 changes: 3 additions & 3 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default [
input: 'src/index.ts',
output: {
banner,
file: pkg.main,
dir: pkg.main.split('/').slice(0, -1).join('/'),
format: 'cjs',
sourcemap: true,
exports: 'named',
Expand All @@ -50,7 +50,7 @@ export default [
input: 'src/index.ts',
output: {
banner,
file: pkg.module,
dir: pkg.module.split('/').slice(0, -1).join('/'),
format: 'esm',
sourcemap: true,
},
Expand All @@ -67,7 +67,7 @@ export default [
},
{
input: 'src/index.ts',
output: {banner, file: pkg.types, format: 'esm'},
output: {banner, dir: pkg.types.split('/').slice(0, -1).join('/'), format: 'esm'},
plugins: [dts()],
},
]
51 changes: 23 additions & 28 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ import {
TransactContext,
WalletPlugin,
WalletPluginConfig,
WalletPluginLoginResponse,
WalletPluginMetadata,
WalletPluginSignResponse,
} from '@wharfkit/session'
import {handleLogin, handleLogout, handleSignatureRequest} from '@wharfkit/protocol-scatter'

export class WalletPluginTokenPocket extends AbstractWalletPlugin implements WalletPlugin {
id = 'tokenpocket'
Expand All @@ -32,6 +29,19 @@ export class WalletPluginTokenPocket extends AbstractWalletPlugin implements Wal
super()
}

private async loadScatterProtocol() {
let protocolScatter
if (typeof window !== 'undefined') {
protocolScatter = await import('@wharfkit/protocol-scatter')
}

if (!protocolScatter) {
throw new Error('Scatter protocol is not available in this environment')
}

return protocolScatter
}

/**
* The metadata for the wallet plugin to be displayed in the user interface.
*/
Expand All @@ -51,16 +61,9 @@ export class WalletPluginTokenPocket extends AbstractWalletPlugin implements Wal
* @param context LogoutContext
* @returns Promise<WalletPluginLoginResponse>
*/
login(context: LoginContext): Promise<WalletPluginLoginResponse> {
return new Promise((resolve, reject) => {
handleLogin(context)
.then((response) => {
resolve(response)
})
.catch((error) => {
reject(error)
})
})
async login(context: LoginContext) {
const protocolScatter = await this.loadScatterProtocol()
return protocolScatter.handleLogin(context)
}

/**
Expand All @@ -70,16 +73,9 @@ export class WalletPluginTokenPocket extends AbstractWalletPlugin implements Wal
* @returns Promise<void>
*/

logout(context: LogoutContext): Promise<void> {
return new Promise((resolve, reject) => {
handleLogout(context)
.then(() => {
resolve()
})
.catch((error) => {
reject(error)
})
})
async logout(context: LogoutContext): Promise<void> {
const protocolScatter = await this.loadScatterProtocol()
return protocolScatter.handleLogout(context)
}

/**
Expand All @@ -89,10 +85,9 @@ export class WalletPluginTokenPocket extends AbstractWalletPlugin implements Wal
* @param resolved ResolvedSigningRequest
* @returns Promise<Signature>
*/
sign(
resolved: ResolvedSigningRequest,
context: TransactContext
): Promise<WalletPluginSignResponse> {
return handleSignatureRequest(resolved, context)
async sign(resolved: ResolvedSigningRequest, context: TransactContext) {
const protocolScatter = await this.loadScatterProtocol()

return protocolScatter.handleSignatureRequest(resolved, context)
}
}
106 changes: 53 additions & 53 deletions test/tests/common.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
import {assert} from 'chai'
import {PermissionLevel, SessionKit} from '@wharfkit/session'
// import {assert} from 'chai'
// import {PermissionLevel, SessionKit} from '@wharfkit/session'

import {WalletPluginTokenPocket} from '$lib'
import {mockFetch} from '$test/utils/mock-fetch'
import {MockStorage} from '$test/utils/mock-storage'
import {MockUserInterface} from '$test/utils/mock-ui'
// import {WalletPluginTokenPocket} from '$lib'
// import {mockFetch} from '$test/utils/mock-fetch'
// import {MockStorage} from '$test/utils/mock-storage'
// import {MockUserInterface} from '$test/utils/mock-ui'

const mockChainDefinition = {
id: 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906',
url: 'https://eos.greymass.com',
}
// const mockChainDefinition = {
// id: 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906',
// url: 'https://eos.greymass.com',
// }

const mockPermissionLevel = PermissionLevel.from('wharfkit1111@test')
// const mockPermissionLevel = PermissionLevel.from('wharfkit1111@test')

const mockSessionKitOptions = {
appName: 'unittests',
chains: [mockChainDefinition],
fetch: mockFetch, // Required for unit tests
storage: new MockStorage(),
ui: new MockUserInterface(),
walletPlugins: [new WalletPluginTokenPocket()],
}
// const mockSessionKitOptions = {
// appName: 'unittests',
// chains: [mockChainDefinition],
// fetch: mockFetch, // Required for unit tests
// storage: new MockStorage(),
// ui: new MockUserInterface(),
// walletPlugins: [new WalletPluginTokenPocket()],
// }

suite('wallet plugin', function () {
this.timeout(120 * 1000)
this.slow(5 * 1000)
// suite('wallet plugin', function () {
// this.timeout(120 * 1000)
// this.slow(5 * 1000)

// test('login and sign', async function () {
// // test('login and sign', async function () {

// const kit = new SessionKit(mockSessionKitOptions)
// const { session } = await kit.login({
// chain: mockChainDefinition.id,
// permissionLevel: mockPermissionLevel,
// })
// assert.isTrue(session.chain.equals(mockChainDefinition))
// assert.isTrue(session.actor.equals(mockPermissionLevel.actor))
// assert.isTrue(session.permission.equals(mockPermissionLevel.permission))
// const result = await session.transact(
// {
// action: {
// authorization: [mockPermissionLevel],
// account: 'eosio.token',
// name: 'transfer',
// data: {
// from: mockPermissionLevel.actor,
// to: 'wharfkittest',
// quantity: '0.0001 EOS',
// memo: 'wharfkit/session wallet plugin template',
// },
// },
// },
// {
// broadcast: false,
// }
// )
// assert.isTrue(result.signer.equals(mockPermissionLevel))
// assert.equal(result.signatures.length, 1)
// })
})
// // const kit = new SessionKit(mockSessionKitOptions)
// // const { session } = await kit.login({
// // chain: mockChainDefinition.id,
// // permissionLevel: mockPermissionLevel,
// // })
// // assert.isTrue(session.chain.equals(mockChainDefinition))
// // assert.isTrue(session.actor.equals(mockPermissionLevel.actor))
// // assert.isTrue(session.permission.equals(mockPermissionLevel.permission))
// // const result = await session.transact(
// // {
// // action: {
// // authorization: [mockPermissionLevel],
// // account: 'eosio.token',
// // name: 'transfer',
// // data: {
// // from: mockPermissionLevel.actor,
// // to: 'wharfkittest',
// // quantity: '0.0001 EOS',
// // memo: 'wharfkit/session wallet plugin template',
// // },
// // },
// // },
// // {
// // broadcast: false,
// // }
// // )
// // assert.isTrue(result.signer.equals(mockPermissionLevel))
// // assert.equal(result.signatures.length, 1)
// // })
// })
22 changes: 17 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,18 @@
pako "^2.1.0"
tslib "^2.0.3"

"@wharfkit/antelope@^1.0.11":
version "1.0.12"
resolved "https://registry.yarnpkg.com/@wharfkit/antelope/-/antelope-1.0.12.tgz#e7b8e0e0805a7194fa62cacd3dfb662b18aa9962"
integrity sha512-Iuqog3rgnW+o8S+Etw5mMto14e1RILIDnRfZAvRS1DOVFgAma6Vv+rT9rJpJq7ldvTBdZu35BImkfwl0iDJCVw==
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":
version "1.0.7"
resolved "https://registry.yarnpkg.com/@wharfkit/antelope/-/antelope-1.0.7.tgz#5ca010db963e061b2e8c47c14e55f86817718c2e"
Expand Down Expand Up @@ -632,14 +644,14 @@
js-big-decimal "^2.0.7"
tslib "^2.1.0"

"@wharfkit/session@^1.3.1":
version "1.3.1"
resolved "https://registry.yarnpkg.com/@wharfkit/session/-/session-1.3.1.tgz#2cbfa14aafc21abba060e97dab24e9263c78ad6e"
integrity sha512-x+SV14arfhvuiDGyWAH2WIKW7v2WKQ1njc3EzU5ZzE6xVWRfKZ23mqtz83YemQ2h0562+QR712CzX5ZhAcdcpg==
"@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 Down

0 comments on commit 33b2a53

Please sign in to comment.