@airgap/beacon-sdk
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -1,8 +0,8 @@ | ||
import { InternalEvent } from './events'; | ||
import { BeaconEvent } from './events'; | ||
import { BeaconMessageType } from '.'; | ||
export declare const messageEvents: { | ||
[key in BeaconMessageType]: { | ||
success: InternalEvent; | ||
error: InternalEvent; | ||
success: BeaconEvent; | ||
error: BeaconEvent; | ||
}; | ||
}; |
@@ -7,34 +7,34 @@ "use strict"; | ||
[_1.BeaconMessageType.PermissionRequest]: { | ||
success: events_1.InternalEvent.PERMISSION_REQUEST_SENT, | ||
error: events_1.InternalEvent.PERMISSION_REQUEST_ERROR | ||
success: events_1.BeaconEvent.PERMISSION_REQUEST_SENT, | ||
error: events_1.BeaconEvent.PERMISSION_REQUEST_ERROR | ||
}, | ||
[_1.BeaconMessageType.PermissionResponse]: { | ||
success: events_1.InternalEvent.UNKNOWN, | ||
error: events_1.InternalEvent.UNKNOWN | ||
success: events_1.BeaconEvent.UNKNOWN, | ||
error: events_1.BeaconEvent.UNKNOWN | ||
}, | ||
[_1.BeaconMessageType.OperationRequest]: { | ||
success: events_1.InternalEvent.OPERATION_REQUEST_SENT, | ||
error: events_1.InternalEvent.OPERATION_REQUEST_ERROR | ||
success: events_1.BeaconEvent.OPERATION_REQUEST_SENT, | ||
error: events_1.BeaconEvent.OPERATION_REQUEST_ERROR | ||
}, | ||
[_1.BeaconMessageType.OperationResponse]: { | ||
success: events_1.InternalEvent.UNKNOWN, | ||
error: events_1.InternalEvent.UNKNOWN | ||
success: events_1.BeaconEvent.UNKNOWN, | ||
error: events_1.BeaconEvent.UNKNOWN | ||
}, | ||
[_1.BeaconMessageType.SignPayloadRequest]: { | ||
success: events_1.InternalEvent.SIGN_REQUEST_SENT, | ||
error: events_1.InternalEvent.SIGN_REQUEST_ERROR | ||
success: events_1.BeaconEvent.SIGN_REQUEST_SENT, | ||
error: events_1.BeaconEvent.SIGN_REQUEST_ERROR | ||
}, | ||
[_1.BeaconMessageType.SignPayloadResponse]: { | ||
success: events_1.InternalEvent.UNKNOWN, | ||
error: events_1.InternalEvent.UNKNOWN | ||
success: events_1.BeaconEvent.UNKNOWN, | ||
error: events_1.BeaconEvent.UNKNOWN | ||
}, | ||
[_1.BeaconMessageType.BroadcastRequest]: { | ||
success: events_1.InternalEvent.BROADCAST_REQUEST_SENT, | ||
error: events_1.InternalEvent.BROADCAST_REQUEST_ERROR | ||
success: events_1.BeaconEvent.BROADCAST_REQUEST_SENT, | ||
error: events_1.BeaconEvent.BROADCAST_REQUEST_ERROR | ||
}, | ||
[_1.BeaconMessageType.BroadcastResponse]: { | ||
success: events_1.InternalEvent.UNKNOWN, | ||
error: events_1.InternalEvent.UNKNOWN | ||
success: events_1.BeaconEvent.UNKNOWN, | ||
error: events_1.BeaconEvent.UNKNOWN | ||
} | ||
}; | ||
//# sourceMappingURL=beacon-message-events.js.map |
@@ -5,4 +5,6 @@ import * as sodium from 'libsodium-wrappers'; | ||
import { Transport, TransportType, Storage, AccountInfo, Network, BeaconMessage } from '../..'; | ||
import { BeaconEventHandler } from '../../events'; | ||
import { ClientOptions } from './ClientOptions'; | ||
export declare abstract class Client { | ||
protected readonly events: BeaconEventHandler; | ||
protected requestCounter: number[]; | ||
@@ -9,0 +11,0 @@ protected readonly rateLimit: number; |
@@ -8,5 +8,8 @@ "use strict"; | ||
const __1 = require("../.."); | ||
// const logger = new Logger('BaseClient') | ||
const events_1 = require("../../events"); | ||
const Logger_1 = require("../../utils/Logger"); | ||
const logger = new Logger_1.Logger('BaseClient'); | ||
class Client { | ||
constructor(config) { | ||
this.events = new events_1.BeaconEventHandler(); | ||
this.requestCounter = []; | ||
@@ -19,2 +22,7 @@ this.rateLimit = 2; | ||
this.storage = config.storage; | ||
if (config.eventHandlers) { | ||
this.events.overrideDefaults(config.eventHandlers).catch((overrideError) => { | ||
logger.error('constructor', overrideError); | ||
}); | ||
} | ||
this.handleResponse = (_event, _connectionInfo) => { | ||
@@ -53,3 +61,3 @@ throw new Error('not overwritten'); | ||
else if (await __1.P2PTransport.isAvailable()) { | ||
this.transport = new __1.P2PTransport(this.name, await this.keyPair, this.storage, isDapp); // Establish our own connection with the wallet | ||
this.transport = new __1.P2PTransport(this.name, await this.keyPair, this.storage, this.events, isDapp); // Establish our own connection with the wallet | ||
} | ||
@@ -56,0 +64,0 @@ else { |
import { Storage } from '../..'; | ||
import { BeaconEvent, BeaconEventHandlerFunction } from '../../events'; | ||
export interface ClientOptions { | ||
name: string; | ||
storage: Storage; | ||
eventHandlers?: { | ||
[key in BeaconEvent]: { | ||
handler: BeaconEventHandlerFunction; | ||
}; | ||
}; | ||
} |
import { ExposedPromise } from '../../utils/exposed-promise'; | ||
import { InternalEvent } from '../../events'; | ||
import { BeaconEvent, BeaconEventHandlerFunction } from '../../events'; | ||
import { ConnectionContext } from '../../types/ConnectionContext'; | ||
@@ -7,3 +7,2 @@ import { AccountInfo, Client, TransportType, Transport, BeaconMessageType, BeaconErrorMessage, BeaconMessage, RequestPermissionInput, RequestSignPayloadInput, RequestOperationInput, RequestBroadcastInput, AppMetadata, PermissionResponseOutput, SignPayloadResponseOutput, OperationResponseOutput, BroadcastResponseOutput } from '../..'; | ||
export declare class DAppClient extends Client { | ||
private readonly events; | ||
private readonly openRequests; | ||
@@ -23,3 +22,3 @@ private readonly iconUrl?; | ||
connect(): Promise<boolean>; | ||
subscribeToEvent(internalEvent: InternalEvent, eventCallback: (data?: unknown) => void): Promise<void>; | ||
subscribeToEvent(internalEvent: BeaconEvent, eventCallback: BeaconEventHandlerFunction): Promise<void>; | ||
checkPermissions(type: BeaconMessageType): Promise<boolean>; | ||
@@ -26,0 +25,0 @@ requestPermissions(input?: RequestPermissionInput): Promise<PermissionResponseOutput>; |
@@ -18,3 +18,2 @@ "use strict"; | ||
}); | ||
this.events = new events_1.InternalEventHandler(); | ||
this.openRequests = new Map(); | ||
@@ -27,4 +26,5 @@ this.iconUrl = config.iconUrl; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
if (event.errorType) { | ||
openRequest.reject(event); | ||
const errorMessage = event; | ||
if (errorMessage.errorType) { | ||
openRequest.reject(errorMessage); | ||
} | ||
@@ -74,3 +74,3 @@ else { | ||
await this.storage.set(__1.StorageKey.ACTIVE_ACCOUNT, account.accountIdentifier); | ||
await this.events.emit(events_1.InternalEvent.ACTIVE_ACCOUNT_SET, account); | ||
await this.events.emit(events_1.BeaconEvent.ACTIVE_ACCOUNT_SET, account); | ||
return; | ||
@@ -151,6 +151,9 @@ } | ||
} | ||
if (!this.activeAccount) { | ||
throw new Error('No active account!'); | ||
} | ||
const request = { | ||
type: __1.BeaconMessageType.SignPayloadRequest, | ||
payload: input.payload, | ||
sourceAddress: input.sourceAddress || '' | ||
sourceAddress: input.sourceAddress || this.activeAccount.address || '' | ||
}; | ||
@@ -168,6 +171,10 @@ const response = await this.makeRequest(request).catch(async (requestError) => { | ||
} | ||
if (!this.activeAccount) { | ||
throw new Error('No active account!'); | ||
} | ||
const request = { | ||
type: __1.BeaconMessageType.OperationRequest, | ||
network: input.network || { type: __1.NetworkType.MAINNET }, | ||
operationDetails: input.operationDetails // TODO: Fix type | ||
operationDetails: input.operationDetails, | ||
sourceAddress: this.activeAccount.address || '' | ||
}; | ||
@@ -218,3 +225,3 @@ const response = await this.makeRequest(request).catch(async (requestError) => { | ||
this.events | ||
.emit(events_1.InternalEvent.LOCAL_RATE_LIMIT_REACHED) | ||
.emit(events_1.BeaconEvent.LOCAL_RATE_LIMIT_REACHED) | ||
.catch((emitError) => console.warn(emitError)); | ||
@@ -224,3 +231,3 @@ throw new Error('rate limit reached'); | ||
if (!(await this.checkPermissions(requestInput.type))) { | ||
this.events.emit(events_1.InternalEvent.NO_PERMISSIONS).catch((emitError) => console.warn(emitError)); | ||
this.events.emit(events_1.BeaconEvent.NO_PERMISSIONS).catch((emitError) => console.warn(emitError)); | ||
throw new Error('No permissions to send this request to wallet!'); | ||
@@ -227,0 +234,0 @@ } |
import { Storage } from '../..'; | ||
import { BeaconEvent, BeaconEventHandlerFunction } from '../../events'; | ||
export interface DAppClientOptions { | ||
@@ -6,2 +7,7 @@ name: string; | ||
storage?: Storage; | ||
eventHandlers?: { | ||
[key in BeaconEvent]: { | ||
handler: BeaconEventHandlerFunction; | ||
}; | ||
}; | ||
} |
@@ -1,2 +0,3 @@ | ||
export declare enum InternalEvent { | ||
import { P2PPairInfo, AccountInfo } from '.'; | ||
export declare enum BeaconEvent { | ||
PERMISSION_REQUEST_SENT = "PERMISSION_REQUEST_SENT", | ||
@@ -13,10 +14,36 @@ PERMISSION_REQUEST_ERROR = "PERMISSION_REQUEST_ERROR", | ||
ACTIVE_ACCOUNT_SET = "ACTIVE_ACCOUNT_SET", | ||
P2P_CHANNEL_CONNECT_SUCCESS = "P2P_CHANNEL_CONNECT_SUCCESS", | ||
P2P_LISTEN_FOR_CHANNEL_OPEN = "P2P_LISTEN_FOR_CHANNEL_OPEN", | ||
UNKNOWN = "UNKNOWN" | ||
} | ||
export declare class InternalEventHandler { | ||
private readonly defaultCallbacks; | ||
export interface BeaconEventType { | ||
[BeaconEvent.PERMISSION_REQUEST_SENT]: undefined; | ||
[BeaconEvent.PERMISSION_REQUEST_ERROR]: undefined; | ||
[BeaconEvent.OPERATION_REQUEST_SENT]: undefined; | ||
[BeaconEvent.OPERATION_REQUEST_ERROR]: undefined; | ||
[BeaconEvent.SIGN_REQUEST_SENT]: undefined; | ||
[BeaconEvent.SIGN_REQUEST_ERROR]: undefined; | ||
[BeaconEvent.BROADCAST_REQUEST_SENT]: undefined; | ||
[BeaconEvent.BROADCAST_REQUEST_ERROR]: undefined; | ||
[BeaconEvent.PERMISSION_REQUEST_SENT]: undefined; | ||
[BeaconEvent.LOCAL_RATE_LIMIT_REACHED]: undefined; | ||
[BeaconEvent.NO_PERMISSIONS]: undefined; | ||
[BeaconEvent.ACTIVE_ACCOUNT_SET]: AccountInfo; | ||
[BeaconEvent.P2P_CHANNEL_CONNECT_SUCCESS]: undefined; | ||
[BeaconEvent.P2P_LISTEN_FOR_CHANNEL_OPEN]: P2PPairInfo; | ||
[BeaconEvent.UNKNOWN]: undefined; | ||
} | ||
export declare type BeaconEventHandlerFunction<T = unknown> = (data: T) => Promise<void>; | ||
export declare const defaultEventCallbacks: { | ||
[key in BeaconEvent]: BeaconEventHandlerFunction<BeaconEventType[key]>; | ||
}; | ||
export declare class BeaconEventHandler { | ||
private readonly callbackMap; | ||
constructor(); | ||
on(event: InternalEvent, eventCallback: (data?: unknown) => void): Promise<void>; | ||
emit(event: InternalEvent, data?: unknown): Promise<void>; | ||
on<K extends BeaconEvent>(event: K, eventCallback: BeaconEventHandlerFunction<BeaconEventType[K]>): Promise<void>; | ||
emit<K extends BeaconEvent>(event: K, data?: BeaconEventType[K]): Promise<void>; | ||
overrideDefaults(eventsToOverride: { | ||
[key in BeaconEvent]: { | ||
handler: BeaconEventHandlerFunction; | ||
}; | ||
}): Promise<void>; | ||
} |
@@ -5,21 +5,21 @@ "use strict"; | ||
const Alert_1 = require("./alert/Alert"); | ||
var InternalEvent; | ||
(function (InternalEvent) { | ||
InternalEvent["PERMISSION_REQUEST_SENT"] = "PERMISSION_REQUEST_SENT"; | ||
InternalEvent["PERMISSION_REQUEST_ERROR"] = "PERMISSION_REQUEST_ERROR"; | ||
InternalEvent["OPERATION_REQUEST_SENT"] = "OPERATION_REQUEST_SENT"; | ||
InternalEvent["OPERATION_REQUEST_ERROR"] = "OPERATION_REQUEST_ERROR"; | ||
InternalEvent["SIGN_REQUEST_SENT"] = "SIGN_REQUEST_SENT"; | ||
InternalEvent["SIGN_REQUEST_ERROR"] = "SIGN_REQUEST_ERROR"; | ||
InternalEvent["BROADCAST_REQUEST_SENT"] = "BROADCAST_REQUEST_SENT"; | ||
InternalEvent["BROADCAST_REQUEST_ERROR"] = "BROADCAST_REQUEST_ERROR"; | ||
InternalEvent["LOCAL_RATE_LIMIT_REACHED"] = "LOCAL_RATE_LIMIT_REACHED"; | ||
InternalEvent["NO_PERMISSIONS"] = "NO_PERMISSIONS"; | ||
InternalEvent["ACTIVE_ACCOUNT_SET"] = "ACTIVE_ACCOUNT_SET"; | ||
InternalEvent["UNKNOWN"] = "UNKNOWN"; | ||
})(InternalEvent = exports.InternalEvent || (exports.InternalEvent = {})); | ||
const listenerFallback = async (data) => { | ||
console.error(`no default listener for event ${event}`, data); | ||
}; | ||
const showSentToast = () => { | ||
const qr_1 = require("./utils/qr"); | ||
var BeaconEvent; | ||
(function (BeaconEvent) { | ||
BeaconEvent["PERMISSION_REQUEST_SENT"] = "PERMISSION_REQUEST_SENT"; | ||
BeaconEvent["PERMISSION_REQUEST_ERROR"] = "PERMISSION_REQUEST_ERROR"; | ||
BeaconEvent["OPERATION_REQUEST_SENT"] = "OPERATION_REQUEST_SENT"; | ||
BeaconEvent["OPERATION_REQUEST_ERROR"] = "OPERATION_REQUEST_ERROR"; | ||
BeaconEvent["SIGN_REQUEST_SENT"] = "SIGN_REQUEST_SENT"; | ||
BeaconEvent["SIGN_REQUEST_ERROR"] = "SIGN_REQUEST_ERROR"; | ||
BeaconEvent["BROADCAST_REQUEST_SENT"] = "BROADCAST_REQUEST_SENT"; | ||
BeaconEvent["BROADCAST_REQUEST_ERROR"] = "BROADCAST_REQUEST_ERROR"; | ||
BeaconEvent["LOCAL_RATE_LIMIT_REACHED"] = "LOCAL_RATE_LIMIT_REACHED"; | ||
BeaconEvent["NO_PERMISSIONS"] = "NO_PERMISSIONS"; | ||
BeaconEvent["ACTIVE_ACCOUNT_SET"] = "ACTIVE_ACCOUNT_SET"; | ||
BeaconEvent["P2P_CHANNEL_CONNECT_SUCCESS"] = "P2P_CHANNEL_CONNECT_SUCCESS"; | ||
BeaconEvent["P2P_LISTEN_FOR_CHANNEL_OPEN"] = "P2P_LISTEN_FOR_CHANNEL_OPEN"; | ||
BeaconEvent["UNKNOWN"] = "UNKNOWN"; | ||
})(BeaconEvent = exports.BeaconEvent || (exports.BeaconEvent = {})); | ||
const showSentToast = async () => { | ||
Toast_1.openToast({ body: 'Request sent', timer: 3000 }).catch((toastError) => console.error(toastError)); | ||
@@ -33,61 +33,62 @@ }; | ||
}; | ||
class InternalEventHandler { | ||
const showRateLimitReached = async () => { | ||
Toast_1.openToast({ | ||
body: 'Rate limit reached. Please slow down', | ||
timer: 3000 | ||
}).catch((toastError) => console.error(toastError)); | ||
}; | ||
const showOkAlert = async () => { | ||
await Alert_1.openAlert({ | ||
title: 'Success', | ||
confirmButtonText: 'Ok!', | ||
timer: 1500 | ||
}); | ||
}; | ||
const showQrCode = async (data) => { | ||
const alertConfig = { | ||
title: 'Pairing Request', | ||
confirmButtonText: 'Ok!', | ||
body: qr_1.getQrData(JSON.stringify(data)), | ||
successCallback: () => { | ||
console.log('CALLBACK'); | ||
} | ||
}; | ||
await Alert_1.openAlert(alertConfig); | ||
}; | ||
const emptyHandler = async (data) => { | ||
console.error(`no default listener for event ${event}`, data); | ||
}; | ||
exports.defaultEventCallbacks = { | ||
[BeaconEvent.PERMISSION_REQUEST_SENT]: showSentToast, | ||
[BeaconEvent.PERMISSION_REQUEST_ERROR]: showNoPermissionAlert, | ||
[BeaconEvent.OPERATION_REQUEST_SENT]: showSentToast, | ||
[BeaconEvent.OPERATION_REQUEST_ERROR]: showNoPermissionAlert, | ||
[BeaconEvent.SIGN_REQUEST_SENT]: showSentToast, | ||
[BeaconEvent.SIGN_REQUEST_ERROR]: showNoPermissionAlert, | ||
[BeaconEvent.BROADCAST_REQUEST_SENT]: showSentToast, | ||
[BeaconEvent.BROADCAST_REQUEST_ERROR]: showNoPermissionAlert, | ||
[BeaconEvent.LOCAL_RATE_LIMIT_REACHED]: showRateLimitReached, | ||
[BeaconEvent.NO_PERMISSIONS]: showNoPermissionAlert, | ||
[BeaconEvent.ACTIVE_ACCOUNT_SET]: emptyHandler, | ||
[BeaconEvent.P2P_CHANNEL_CONNECT_SUCCESS]: showOkAlert, | ||
[BeaconEvent.P2P_LISTEN_FOR_CHANNEL_OPEN]: showQrCode, | ||
[BeaconEvent.UNKNOWN]: emptyHandler | ||
}; | ||
class BeaconEventHandler { | ||
constructor() { | ||
this.defaultCallbacks = { | ||
[InternalEvent.PERMISSION_REQUEST_SENT]: async (_data) => { | ||
showSentToast(); | ||
}, | ||
[InternalEvent.PERMISSION_REQUEST_ERROR]: async (_data) => { | ||
await showNoPermissionAlert(); | ||
}, | ||
[InternalEvent.OPERATION_REQUEST_SENT]: async (_data) => { | ||
showSentToast(); | ||
}, | ||
[InternalEvent.OPERATION_REQUEST_ERROR]: async (_data) => { | ||
await showNoPermissionAlert(); | ||
}, | ||
[InternalEvent.SIGN_REQUEST_SENT]: async (_data) => { | ||
showSentToast(); | ||
}, | ||
[InternalEvent.SIGN_REQUEST_ERROR]: async (_data) => { | ||
await showNoPermissionAlert(); | ||
}, | ||
[InternalEvent.BROADCAST_REQUEST_SENT]: async (_data) => { | ||
showSentToast(); | ||
}, | ||
[InternalEvent.BROADCAST_REQUEST_ERROR]: async (_data) => { | ||
await showNoPermissionAlert(); | ||
}, | ||
[InternalEvent.LOCAL_RATE_LIMIT_REACHED]: async (_data) => { | ||
Toast_1.openToast({ | ||
body: 'Rate limit reached. Please slow down', | ||
timer: 3000 | ||
}).catch((toastError) => console.error(toastError)); | ||
}, | ||
[InternalEvent.NO_PERMISSIONS]: async (_data) => { | ||
Toast_1.openToast({ | ||
body: 'No permissions!', | ||
timer: 3000 | ||
}).catch((toastError) => console.error(toastError)); | ||
}, | ||
[InternalEvent.ACTIVE_ACCOUNT_SET]: async (data) => { | ||
console.log('internal event: active account set', data); | ||
}, | ||
[InternalEvent.UNKNOWN]: async (_data) => { | ||
/* Do nothing */ | ||
} | ||
}; | ||
this.callbackMap = { | ||
[InternalEvent.PERMISSION_REQUEST_SENT]: [], | ||
[InternalEvent.PERMISSION_REQUEST_ERROR]: [], | ||
[InternalEvent.OPERATION_REQUEST_SENT]: [], | ||
[InternalEvent.OPERATION_REQUEST_ERROR]: [], | ||
[InternalEvent.SIGN_REQUEST_SENT]: [], | ||
[InternalEvent.SIGN_REQUEST_ERROR]: [], | ||
[InternalEvent.BROADCAST_REQUEST_SENT]: [], | ||
[InternalEvent.BROADCAST_REQUEST_ERROR]: [], | ||
[InternalEvent.LOCAL_RATE_LIMIT_REACHED]: [], | ||
[InternalEvent.NO_PERMISSIONS]: [], | ||
[InternalEvent.ACTIVE_ACCOUNT_SET]: [], | ||
[InternalEvent.UNKNOWN]: [] | ||
[BeaconEvent.PERMISSION_REQUEST_SENT]: [exports.defaultEventCallbacks.PERMISSION_REQUEST_SENT], | ||
[BeaconEvent.PERMISSION_REQUEST_ERROR]: [exports.defaultEventCallbacks.PERMISSION_REQUEST_ERROR], | ||
[BeaconEvent.OPERATION_REQUEST_SENT]: [exports.defaultEventCallbacks.OPERATION_REQUEST_SENT], | ||
[BeaconEvent.OPERATION_REQUEST_ERROR]: [exports.defaultEventCallbacks.OPERATION_REQUEST_ERROR], | ||
[BeaconEvent.SIGN_REQUEST_SENT]: [exports.defaultEventCallbacks.SIGN_REQUEST_SENT], | ||
[BeaconEvent.SIGN_REQUEST_ERROR]: [exports.defaultEventCallbacks.SIGN_REQUEST_ERROR], | ||
[BeaconEvent.BROADCAST_REQUEST_SENT]: [exports.defaultEventCallbacks.BROADCAST_REQUEST_SENT], | ||
[BeaconEvent.BROADCAST_REQUEST_ERROR]: [exports.defaultEventCallbacks.BROADCAST_REQUEST_ERROR], | ||
[BeaconEvent.LOCAL_RATE_LIMIT_REACHED]: [exports.defaultEventCallbacks.LOCAL_RATE_LIMIT_REACHED], | ||
[BeaconEvent.NO_PERMISSIONS]: [exports.defaultEventCallbacks.NO_PERMISSIONS], | ||
[BeaconEvent.ACTIVE_ACCOUNT_SET]: [exports.defaultEventCallbacks.ACTIVE_ACCOUNT_SET], | ||
[BeaconEvent.P2P_CHANNEL_CONNECT_SUCCESS]: [exports.defaultEventCallbacks.P2P_CHANNEL_CONNECT_SUCCESS], | ||
[BeaconEvent.P2P_LISTEN_FOR_CHANNEL_OPEN]: [exports.defaultEventCallbacks.P2P_LISTEN_FOR_CHANNEL_OPEN], | ||
[BeaconEvent.UNKNOWN]: [exports.defaultEventCallbacks.UNKNOWN] | ||
}; | ||
@@ -104,12 +105,14 @@ } | ||
listeners.forEach((listener) => { | ||
listener(data); | ||
listener(data).catch((listenerError) => console.error(`error handling event ${event}`, listenerError)); | ||
}); | ||
} | ||
else { | ||
const listener = this.defaultCallbacks[event] || listenerFallback; | ||
await listener(data); | ||
} | ||
} | ||
async overrideDefaults(eventsToOverride) { | ||
Object.keys(eventsToOverride).forEach((untypedEvent) => { | ||
const event = untypedEvent; | ||
this.callbackMap[event] = [eventsToOverride[event].handler]; | ||
}); | ||
} | ||
} | ||
exports.InternalEventHandler = InternalEventHandler; | ||
exports.BeaconEventHandler = BeaconEventHandler; | ||
//# sourceMappingURL=events.js.map |
@@ -17,3 +17,2 @@ import * as sodium from 'libsodium-wrappers'; | ||
}; | ||
getHandshakeQR(type?: 'data' | 'svg' | 'ascii'): string; | ||
getRelayServer(publicKeyHash?: string, nonce?: string): string; | ||
@@ -20,0 +19,0 @@ start(): Promise<void>; |
@@ -5,3 +5,2 @@ "use strict"; | ||
const matrixsdk = require("matrix-js-sdk"); | ||
const qrcode = require("qrcode-generator"); | ||
const crypto_1 = require("./utils/crypto"); | ||
@@ -31,43 +30,2 @@ class P2PCommunicationClient { | ||
} | ||
getHandshakeQR(type) { | ||
const typeNumber = 0; | ||
const errorCorrectionLevel = 'L'; | ||
const qr = qrcode(typeNumber, errorCorrectionLevel); | ||
const data = JSON.stringify(this.getHandshakeInfo()); | ||
console.log(data); | ||
try { | ||
qr.addData(data); | ||
qr.make(); | ||
if (type === 'svg') { | ||
return qr.createSvgTag(); | ||
} | ||
else if (type === 'ascii') { | ||
const length = qr.getModuleCount(); | ||
const black = '\x1B[40m \x1B[0m'; | ||
const white = '\x1B[47m \x1B[0m'; | ||
const whiteLine = new Array(length + 3).join(white); | ||
const blackLine = new Array(length + 3).join(black); | ||
let ascii = ''; | ||
ascii += `${blackLine}\n`; | ||
ascii += `${whiteLine}\n`; | ||
for (let x = 0; x < length; x++) { | ||
ascii += white; | ||
for (let y = 0; y < length; y++) { | ||
ascii += qr.isDark(x, y) ? black : white; | ||
} | ||
ascii += `${white}\n`; | ||
} | ||
ascii += whiteLine; | ||
ascii += blackLine; | ||
return ascii; | ||
} | ||
else { | ||
return qr.createDataURL(); | ||
} | ||
} | ||
catch (qrError) { | ||
console.error('error', qrError); | ||
throw qrError; | ||
} | ||
} | ||
getRelayServer(publicKeyHash, nonce = '') { | ||
@@ -74,0 +32,0 @@ if (!this.keyPair) { |
@@ -29,4 +29,4 @@ "use strict"; | ||
const connectionContext = { | ||
origin: __1.Origin.EXTENSION, | ||
id: sender.id ? sender.id : '', | ||
origin: __1.Origin.WEBSITE, | ||
id: sender.url ? sender.url : '', | ||
extras: { sender, sendResponse } | ||
@@ -33,0 +33,0 @@ }; |
import * as sodium from 'libsodium-wrappers'; | ||
import { Storage, Transport, TransportType } from '..'; | ||
import { BeaconEventHandler } from '../events'; | ||
export declare class P2PTransport extends Transport { | ||
readonly type: TransportType; | ||
private readonly events; | ||
private readonly isDapp; | ||
@@ -9,3 +11,3 @@ private readonly storage; | ||
private client; | ||
constructor(name: string, keyPair: sodium.KeyPair, storage: Storage, isDapp: boolean); | ||
constructor(name: string, keyPair: sodium.KeyPair, storage: Storage, events: BeaconEventHandler, isDapp: boolean); | ||
static isAvailable(): Promise<boolean>; | ||
@@ -12,0 +14,0 @@ connect(): Promise<void>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const Alert_1 = require("../alert/Alert"); | ||
const Logger_1 = require("../utils/Logger"); | ||
const __1 = require(".."); | ||
const events_1 = require("../events"); | ||
const logger = new Logger_1.Logger('Transport'); | ||
class P2PTransport extends __1.Transport { | ||
constructor(name, keyPair, storage, isDapp) { | ||
constructor(name, keyPair, storage, events, isDapp) { | ||
super(name); | ||
@@ -14,2 +14,3 @@ this.type = __1.TransportType.P2P; | ||
this.storage = storage; | ||
this.events = events; | ||
this.isDapp = isDapp; | ||
@@ -54,19 +55,10 @@ } | ||
} | ||
Alert_1.closeAlert(); | ||
Alert_1.openAlert({ | ||
title: 'Success', | ||
confirmButtonText: 'Ok!', | ||
timer: 1500 | ||
}); | ||
this.events | ||
.emit(events_1.BeaconEvent.P2P_CHANNEL_CONNECT_SUCCESS) | ||
.catch((emitError) => console.warn(emitError)); | ||
resolve(); | ||
}); | ||
const alertConfig = { | ||
title: 'Pairing Request', | ||
confirmButtonText: 'Ok!', | ||
body: [this.client.getHandshakeQR('svg')].join(''), | ||
successCallback: () => { | ||
console.log('CALLBACK'); | ||
} | ||
}; | ||
Alert_1.openAlert(alertConfig); | ||
this.events | ||
.emit(events_1.BeaconEvent.P2P_LISTEN_FOR_CHANNEL_OPEN, this.client.getHandshakeInfo()) | ||
.catch((emitError) => console.warn(emitError)); | ||
}); | ||
@@ -73,0 +65,0 @@ } |
@@ -0,3 +1,3 @@ | ||
import { TransportType } from '..'; | ||
import { Transport } from './Transport'; | ||
import { TransportType } from '..'; | ||
export declare class PostMessageTransport extends Transport { | ||
@@ -4,0 +4,0 @@ readonly type: TransportType; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const MockWindow_1 = require("../MockWindow"); | ||
const __1 = require(".."); | ||
const Origin_1 = require("../types/Origin"); | ||
const Transport_1 = require("./Transport"); | ||
const __1 = require(".."); | ||
class PostMessageTransport extends Transport_1.Transport { | ||
@@ -40,4 +41,7 @@ constructor(name) { | ||
const data = message.data; | ||
if (data.target === __1.ExtensionMessageTarget.PAGE) { | ||
this.notifyListeners(data.payload, {}).catch((error) => { | ||
if (data.message.target === __1.ExtensionMessageTarget.PAGE) { | ||
this.notifyListeners(data.message.payload, { | ||
origin: Origin_1.Origin.EXTENSION, | ||
id: data.sender.id || '' | ||
}).catch((error) => { | ||
throw error; | ||
@@ -44,0 +48,0 @@ }); |
@@ -6,2 +6,3 @@ import { TezosBaseOperation, BeaconBaseMessage, BeaconMessageType, Network } from '../../..'; | ||
operationDetails: TezosBaseOperation[]; | ||
sourceAddress: string; | ||
} |
@@ -5,3 +5,3 @@ import { BeaconBaseMessage, BeaconMessageType } from '../../..'; | ||
payload: string; | ||
sourceAddress?: string; | ||
sourceAddress: string; | ||
} |
export declare enum NetworkType { | ||
MAINNET = "mainnet", | ||
CARTHAGENET = "carthagenet", | ||
CUSTOM = "custom" | ||
} |
@@ -6,4 +6,5 @@ "use strict"; | ||
NetworkType["MAINNET"] = "mainnet"; | ||
NetworkType["CARTHAGENET"] = "carthagenet"; | ||
NetworkType["CUSTOM"] = "custom"; | ||
})(NetworkType = exports.NetworkType || (exports.NetworkType = {})); | ||
//# sourceMappingURL=NetworkType.js.map |
import { ExtensionMessageTarget } from '..'; | ||
export interface ExtensionMessage<T> { | ||
export interface ExtensionMessage<T, U = unknown> { | ||
target: ExtensionMessageTarget; | ||
sender?: U; | ||
payload: T; | ||
} |
export declare enum Origin { | ||
WEBSITE = "website", | ||
EXTENSION = "extension", | ||
P2P = "p2p" | ||
} |
@@ -5,2 +5,3 @@ "use strict"; | ||
(function (Origin) { | ||
Origin["WEBSITE"] = "website"; | ||
Origin["EXTENSION"] = "extension"; | ||
@@ -7,0 +8,0 @@ Origin["P2P"] = "p2p"; |
{ | ||
"name": "@airgap/beacon-sdk", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "The beacon-sdk is setup in a way to allow for p2p communication between wallets and dapps", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
238044
311
3564