Skip to content

Commit

Permalink
Merge pull request #4 from XPRNetwork/deps-update
Browse files Browse the repository at this point in the history
Updated dependencies for the plugin
  • Loading branch information
metallicusdev authored Feb 16, 2024
2 parents 68b247d + dab88fe commit c9522df
Show file tree
Hide file tree
Showing 6 changed files with 407 additions and 323 deletions.
3 changes: 2 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Copyright (c) 2023 Greymass Inc. All Rights Reserved.
Copyright (c) 2024 Greymass Inc. All Rights Reserved.
Copyright (c) 2024 Metallicus. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Expand Down
346 changes: 194 additions & 152 deletions examples/yarn.lock

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@
},
"dependencies": {
"@greymass/buoy": "^1.0.3",
"@wharfkit/protocol-esr": "^1.2.1",
"@wharfkit/antelope": "^1.0.5",
"@wharfkit/protocol-esr": "^1.3.0",
"isomorphic-ws": "^5.0.0",
"ws": "^8.14.2"
},
"resolutions": {
"@wharfkit/antelope": "^1.0.5"
},
"peerDependencies": {
"@wharfkit/session": "^1.2.5"
"@wharfkit/session": "^1.2.7"
},
"devDependencies": {
"@rollup/plugin-alias": "^3.1.4",
Expand All @@ -40,8 +44,8 @@
"@types/node": "^18.7.18",
"@typescript-eslint/eslint-plugin": "^5.20.0",
"@typescript-eslint/parser": "^5.20.0",
"@wharfkit/mock-data": "^1.2.0",
"@wharfkit/session": "^1.2.5",
"@wharfkit/mock-data": "^1.3.0",
"@wharfkit/session": "^1.2.7",
"chai": "^4.3.4",
"eslint": "^8.13.0",
"eslint-config-prettier": "^8.1.0",
Expand Down
20 changes: 13 additions & 7 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ import fs from 'fs'
import dts from 'rollup-plugin-dts'
import typescript from '@rollup/plugin-typescript'
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json'
import replace from '@rollup/plugin-replace'

import pkg from './package.json'
import json from '@rollup/plugin-json'

const replaceVersion = replace({
preventAssignment: true,
__ver: pkg.version,
})

const name = pkg.name
const license = fs.readFileSync('LICENSE').toString('utf-8').trim()
Expand All @@ -18,7 +24,7 @@ const banner = `
*/
`.trim()

const external = Object.keys(pkg.peerDependencies)
const external = [...Object.keys(pkg.dependencies), ...Object.keys(pkg.peerDependencies)]

/** @type {import('rollup').RollupOptions} */
export default [
Expand All @@ -31,9 +37,9 @@ export default [
sourcemap: true,
exports: 'named',
},
plugins: [typescript({target: 'es6'}), commonjs({
plugins: [replaceVersion, typescript({target: 'es6'}), commonjs({
defaultIsModuleExports: false,
}),json()],
}), json()],
external,
},
{
Expand All @@ -44,14 +50,14 @@ export default [
format: 'esm',
sourcemap: true,
},
plugins: [typescript({target: 'es2020'}), commonjs({
plugins: [replaceVersion, typescript({target: 'es2020'}), commonjs({
defaultIsModuleExports: false,
}),json()],
}), json()],
external,
},
{
input: 'src/index.ts',
output: {banner, file: pkg.types, format: 'esm'},
plugins: [dts()],
plugins: [replaceVersion, dts()],
},
]
65 changes: 36 additions & 29 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ import {
} from '@wharfkit/session'
import {
extractSignaturesFromCallback,
generateReturnUrl,
isAppleHandheld,
isCallback,
LinkInfo,
sealMessage,
setTransactionCallback,
verifyLoginCallbackResponse,
waitForCallback,
generateReturnUrl,
isAppleHandheld,
} from '@wharfkit/protocol-esr'

import WebSocket from 'isomorphic-ws'
Expand Down Expand Up @@ -127,6 +127,7 @@ export class WalletPluginWebAuth extends AbstractWalletPlugin {
throw new Error('No UI available')
}

// Retrieve translation helper from the UI, passing the app ID
const t = context.ui.getTranslate(this.id)

const browserLogin = new Deferred<inBrowserPayload>()
Expand Down Expand Up @@ -361,6 +362,14 @@ export class WalletPluginWebAuth extends AbstractWalletPlugin {
// Create a new signing request based on the existing resolved request
const modifiedRequest = await context.createRequest({transaction: resolved.transaction})

// Set the expiration on the request LinkInfo
modifiedRequest.setInfoKey(
'link',
LinkInfo.from({
expiration,
})
)

// Add the callback to the request
const callback = setTransactionCallback(modifiedRequest, this.buoyUrl)

Expand All @@ -374,16 +383,14 @@ export class WalletPluginWebAuth extends AbstractWalletPlugin {
const returnUrl = generateReturnUrl()
sameDeviceRequest.setInfoKey('same_device', true)
sameDeviceRequest.setInfoKey('return_path', returnUrl)

if (this.data.sameDevice) {
if (this.data.sameDevice) {
if (this.data.launchUrl) {
window.location.href = this.data.launchUrl
} else if (isAppleHandheld()) {
window.location.href = `${this.scheme}://link`
}
if (this.data.launchUrl) {
window.location.href = this.data.launchUrl
} else if (isAppleHandheld()) {
window.location.href = `${this.scheme}://link`
}
}
}

const signManually = () => {
context.ui?.prompt({
Expand Down Expand Up @@ -460,30 +467,27 @@ export class WalletPluginWebAuth extends AbstractWalletPlugin {
// Clear the timeout if the UI throws (which generally means it closed)
promptPromise.catch(() => clearTimeout(timer))

// Set the expiration on the request LinkInfo
modifiedRequest.setInfoKey(
'link',
LinkInfo.from({
expiration,
})
)

// Wait for the callback from the wallet
const callbackPromise = waitForCallback(callback, this.buoyWs, t)

// Assemble and send the payload to the wallet
const service = new URL(this.data.channelUrl).origin
const channel = new URL(this.data.channelUrl).pathname.substring(1)
const sealedMessage = sealMessage(
(this.data.sameDevice ? sameDeviceRequest : modifiedRequest).encode(true, false, `${this.scheme}:`),
PrivateKey.from(this.data.privateKey),
PublicKey.from(this.data.signerKey)
)
if (this.data.channelUrl) {
const service = new URL(this.data.channelUrl).origin
const channel = new URL(this.data.channelUrl).pathname.substring(1)
const sealedMessage = sealMessage(
(this.data.sameDevice ? sameDeviceRequest : modifiedRequest).encode(true, false, `${this.scheme}:`),
PrivateKey.from(this.data.privateKey),
PublicKey.from(this.data.signerKey)
)

send(Serializer.encode({object: sealedMessage}).array, {
service,
channel,
})
send(Serializer.encode({object: sealedMessage}).array, {
service,
channel,
})
} else {
// If no channel is defined, fallback to the same device request and trigger immediately
window.location.href = sameDeviceRequest.encode()
}

// Wait for either the callback or the prompt to resolve
const callbackResponse = await Promise.race([callbackPromise, promptPromise]).finally(
Expand All @@ -503,13 +507,16 @@ export class WalletPluginWebAuth extends AbstractWalletPlugin {
callbackResponse,
context.esrOptions
)

// Return the new request and the signatures from the wallet
return {
signatures: extractSignaturesFromCallback(callbackResponse),
resolved: resolvedRequest,
}
}

const errorString = t('error.not_completed', {default: 'The request was not completed.'})

promptPromise.cancel(errorString)
}

Expand Down
Loading

0 comments on commit c9522df

Please sign in to comment.