Skip to content

Commit

Permalink
enhancement: loading scatter protocol dynamically
Browse files Browse the repository at this point in the history
chore: updated session kit
  • Loading branch information
dafuga committed Sep 25, 2024
1 parent b8be24a commit cd02542
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 36 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-scatter",
"license": "BSD-3-Clause",
"main": "lib/wallet-plugin-scatter.js",
"module": "lib/wallet-plugin-scatter.m.js",
"types": "lib/wallet-plugin-scatter.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.3.1"
"@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()],
},
]
45 changes: 22 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
WalletPluginMetadata,
WalletPluginSignResponse,
} from '@wharfkit/session'
import {handleLogin, handleLogout, handleSignatureRequest} from '@wharfkit/protocol-scatter'

export class WalletPluginScatter extends AbstractWalletPlugin implements WalletPlugin {
id = 'scatter'
Expand Down Expand Up @@ -43,22 +42,28 @@ export class WalletPluginScatter extends AbstractWalletPlugin implements WalletP
download: 'https://github.com/GetScatter/ScatterDesktop/releases',
})

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
}

/**
* Performs the wallet logic required to login and return the chain and permission level to use.
*
* @param context LoginContext
* @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): Promise<WalletPluginLoginResponse> {
const scatterProtocol = await this.loadScatterProtocol()
return scatterProtocol.handleLogin(context)
}

/**
Expand All @@ -68,16 +73,9 @@ export class WalletPluginScatter extends AbstractWalletPlugin implements WalletP
* @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 scatterProtocol = await this.loadScatterProtocol()
return scatterProtocol.handleLogout(context)
}

/**
Expand All @@ -87,10 +85,11 @@ export class WalletPluginScatter extends AbstractWalletPlugin implements WalletP
* @param resolved ResolvedSigningRequest
* @returns Promise<Signature>
*/
sign(
async sign(
resolved: ResolvedSigningRequest,
context: TransactContext
): Promise<WalletPluginSignResponse> {
return handleSignatureRequest(resolved, context)
const scatterProtocol = await this.loadScatterProtocol()
return scatterProtocol.handleSignatureRequest(resolved, context)
}
}
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 cd02542

Please sign in to comment.