Skip to content

Commit

Permalink
Fixed all problems in github actions
Browse files Browse the repository at this point in the history
  • Loading branch information
andreyjamer committed Feb 16, 2024
1 parent 26ff200 commit a0af5fd
Show file tree
Hide file tree
Showing 11 changed files with 450 additions and 226 deletions.
1 change: 1 addition & 0 deletions build/nyc_output/c4f48750-7248-4487-a9d8-c1cd8609475d.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"parent":null,"pid":19597,"argv":["/home/andrey/.nvm/versions/node/v16.19.1/bin/node","/home/andrey/project/wallet-plugin-webauth/node_modules/.bin/mocha","-u","tdd","-r","ts-node/register","-r","tsconfig-paths/register","--extension","ts","-R","list","test/tests/common.ts"],"execArgv":[],"cwd":"/home/andrey/project/wallet-plugin-webauth","time":1708082831522,"ppid":19580,"coverageFilename":"/home/andrey/project/wallet-plugin-webauth/build/nyc_output/c4f48750-7248-4487-a9d8-c1cd8609475d.json","externalId":"","uuid":"c4f48750-7248-4487-a9d8-c1cd8609475d","files":[]}
1 change: 1 addition & 0 deletions build/nyc_output/processinfo/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"processes":{"c4f48750-7248-4487-a9d8-c1cd8609475d":{"parent":null,"children":[]}},"files":{},"externalIds":{}}
344 changes: 175 additions & 169 deletions src/browser.ts
Original file line number Diff line number Diff line change
@@ -1,203 +1,209 @@
import { Deferred } from './utils'
import {Deferred} from './utils'

const OPEN_SETTINGS = 'menubar=1,resizable=1,width=400,height=600'

function parseErrorMessage(error: any) {
let errorMessage: string

if (error.json && error.json.error) {
error = error.json.error
}

if (error.error) {
error = error.error
}

if (error.details) {
const { code, details, name, what } = error
if (name === 'eosio_assert_message_exception') {
errorMessage = details[0].message.replace('assertion failure with message: ', '')
} else if (details.length > 0) {
errorMessage = details.map((d) => d.message).join('\n')
} else {
errorMessage = what || String(error)
}
} else {
errorMessage = error.message || String(error)
}

return errorMessage
let errorMessage: string

if (error.json && error.json.error) {
error = error.json.error
}

if (error.error) {
error = error.error
}

if (error.details) {
const {details, name, what} = error
if (name === 'eosio_assert_message_exception') {
errorMessage = details[0].message.replace('assertion failure with message: ', '')
} else if (details.length > 0) {
errorMessage = details.map((d) => d.message).join('\n')
} else {
errorMessage = what || String(error)
}
} else {
errorMessage = error.message || String(error)
}

return errorMessage
}

// Need to keep outside class since it messes with reactivity like Vuex
let _childWindow: Window | null = null

export class BrowserTransport {
deferredTransact: {
deferral: Deferred<any>
transaction: any,
params: any,
waitingForOpen: boolean
} | undefined
deferredLogin: Deferred<any> | undefined
scheme: string

public get childWindow() {
return _childWindow;
}

public set childWindow(window: Window | null) {
_childWindow = window
}

constructor(options: { scheme: string} = { scheme: 'proton' }) {
this.scheme = options.scheme

setInterval(() => this.closeChild(), 500)
if (typeof window !== 'undefined') {
window.addEventListener('message', (event) => this.onEvent(event), false)
}
}

childUrl(path: string) {
const base = this.scheme === 'proton-dev'
? 'https://testnet.webauth.com'
: 'https://webauth.com'
return `${base}${path}`
}

closeChild(force = false) {
if (this.childWindow) {
if (force) {
this.childWindow.close()
}

if (force || this.childWindow.closed) {
this.childWindow = null
}
}
}
deferredTransact:
| {
deferral: Deferred<any>
transaction: any
params: any
waitingForOpen: boolean
}
| undefined
deferredLogin: Deferred<any> | undefined
scheme: string

async login() {
if (this.deferredTransact) {
this.closeChild(true)
this.deferredTransact.deferral.reject('Trying to login')
this.deferredTransact = undefined
public get childWindow() {
return _childWindow
}

this.childWindow = window.open(this.childUrl('/login'), '_blank', OPEN_SETTINGS)
this.deferredLogin = new Deferred()

try {
const auth: {
actor: string,
permission: string
} = await this.deferredLogin.promise
return {
inBrowser: auth
}
} catch (e) {
console.error(e)
throw e
public set childWindow(window: Window | null) {
_childWindow = window
}
}

async transact(args: any /*TransactArgs*/, options?: any /*TransactOptions*/): Promise<any> {
if (this.deferredLogin) {
this.closeChild(true)
this.deferredLogin.reject('Trying to login')
this.deferredLogin = undefined
}
constructor(options: {scheme: string} = {scheme: 'proton'}) {
this.scheme = options.scheme

this.deferredTransact = {
deferral: new Deferred(),
transaction: args.transaction || { actions: args.actions },
params: options,
waitingForOpen: true
setInterval(() => this.closeChild(), 500)
if (typeof window !== 'undefined') {
window.addEventListener('message', (event) => this.onEvent(event), false)
}
}

this.childWindow = window.open(this.childUrl('/auth'), '_blank', OPEN_SETTINGS)

try {
const res = await this.deferredTransact.deferral.promise
return res
} catch (error) {
throw error
}
}

async onEvent(e: MessageEvent) {
if (
e.origin.indexOf('https://webauth.com') !== -1 &&
e.origin.indexOf('https://testnet.webauth.com') !== -1
) {
return
childUrl(path: string) {
const base =
this.scheme === 'proton-dev' ? 'https://testnet.webauth.com' : 'https://webauth.com'
return `${base}${path}`
}

let eventData
try {
eventData = JSON.parse(e.data)
} catch (e) {
return
}
closeChild(force = false) {
if (this.childWindow) {
if (force) {
this.childWindow.close()
}

try {
const { type, data, error } = eventData
if (!type) {
return
}

// Ready to receive transaction
if (type === 'isReady') {
if (this.deferredTransact && this.deferredTransact.waitingForOpen) {
this.deferredTransact.waitingForOpen = false

this.childWindow!.postMessage(JSON.stringify({
type: 'transaction',
data: {
transaction: this.deferredTransact.transaction,
params: this.deferredTransact.params
if (force || this.childWindow.closed) {
this.childWindow = null
}
}), '*')
}
}
// Close child
else if (type === 'close') {
this.closeChild(true)
}

async login() {
if (this.deferredTransact) {
this.deferredTransact.deferral.reject('Closed')
} else if (this.deferredLogin) {
this.deferredLogin.reject('Closed')
this.closeChild(true)
this.deferredTransact.deferral.reject('Trying to login')
this.deferredTransact = undefined
}
}
// TX Success
else if (type === 'transactionSuccess') {
this.closeChild(true)
console.log('transactionSuccess', error, data);

if (this.deferredTransact) {
if (error) {
const errorMessage = parseErrorMessage(error)
this.deferredTransact.deferral.reject(errorMessage)
} else {
this.deferredTransact.deferral.resolve(data)
}
this.childWindow = window.open(this.childUrl('/login'), '_blank', OPEN_SETTINGS)
this.deferredLogin = new Deferred()

this.deferredTransact = undefined
try {
const auth: {
actor: string
permission: string
} = await this.deferredLogin.promise
return {
inBrowser: auth,
}
} catch (e) {
// eslint-disable-next-line no-console
console.error(e)
throw e
}
}
// Login success
else if (type === 'loginSuccess') {
this.closeChild(true)
}

async transact(args: any /*TransactArgs*/, options?: any /*TransactOptions*/): Promise<any> {
if (this.deferredLogin) {
this.deferredLogin.resolve(data)
this.deferredLogin = undefined
this.closeChild(true)
this.deferredLogin.reject('Trying to login')
this.deferredLogin = undefined
}

this.deferredTransact = {
deferral: new Deferred(),
transaction: args.transaction || {actions: args.actions},
params: options,
waitingForOpen: true,
}

this.childWindow = window.open(this.childUrl('/auth'), '_blank', OPEN_SETTINGS)

// eslint-disable-next-line no-useless-catch
try {
const res = await this.deferredTransact.deferral.promise
return res
} catch (error) {
throw error
}
}

async onEvent(e: MessageEvent) {
if (
e.origin.indexOf('https://webauth.com') !== -1 &&
e.origin.indexOf('https://testnet.webauth.com') !== -1
) {
return
}

let eventData
try {
eventData = JSON.parse(e.data)
} catch (e) {
return
}

try {
const {type, data, error} = eventData
if (!type) {
return
}

// Ready to receive transaction
if (type === 'isReady') {
if (this.deferredTransact && this.deferredTransact.waitingForOpen) {
this.deferredTransact.waitingForOpen = false

this.childWindow!.postMessage(
JSON.stringify({
type: 'transaction',
data: {
transaction: this.deferredTransact.transaction,
params: this.deferredTransact.params,
},
}),
'*'
)
}
}
// Close child
else if (type === 'close') {
this.closeChild(true)

if (this.deferredTransact) {
this.deferredTransact.deferral.reject('Closed')
} else if (this.deferredLogin) {
this.deferredLogin.reject('Closed')
}
}
// TX Success
else if (type === 'transactionSuccess') {
this.closeChild(true)

if (this.deferredTransact) {
if (error) {
const errorMessage = parseErrorMessage(error)
this.deferredTransact.deferral.reject(errorMessage)
} else {
this.deferredTransact.deferral.resolve(data)
}

this.deferredTransact = undefined
}
}
// Login success
else if (type === 'loginSuccess') {
this.closeChild(true)

if (this.deferredLogin) {
this.deferredLogin.resolve(data)
this.deferredLogin = undefined
}
}
} catch (e) {
// eslint-disable-next-line no-console
console.error(e)
}
}
} catch (e) {
console.error(e)
}
}
}
Loading

0 comments on commit a0af5fd

Please sign in to comment.