phantasma-sdk-ts 0.2.0-rc.17 → 0.2.0-rc.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -8,6 +8,7 @@ const Hex_1 = require("../utils/Hex");
|
|
|
8
8
|
class PhantasmaLink {
|
|
9
9
|
//Construct The Link
|
|
10
10
|
constructor(dappID, logging = true) {
|
|
11
|
+
this.lastSocketErrorMessage = null;
|
|
11
12
|
this.requestID = 0;
|
|
12
13
|
//Message Logging
|
|
13
14
|
this.onMessage = (msg) => {
|
|
@@ -163,8 +164,9 @@ class PhantasmaLink {
|
|
|
163
164
|
onErrorCallback(message);
|
|
164
165
|
return;
|
|
165
166
|
}
|
|
166
|
-
|
|
167
|
-
|
|
167
|
+
const txLengthLimit = 65536;
|
|
168
|
+
if (txHex.length >= txLengthLimit) {
|
|
169
|
+
const message = `Error: Carbon transaction message is too big (${txHex.length} > ${txLengthLimit})!`;
|
|
168
170
|
this.onMessage(message);
|
|
169
171
|
onErrorCallback(message);
|
|
170
172
|
return;
|
|
@@ -367,6 +369,7 @@ class PhantasmaLink {
|
|
|
367
369
|
new PhantasmaLinkSocket()
|
|
368
370
|
: new WebSocket(path);
|
|
369
371
|
this.requestCallback = null;
|
|
372
|
+
this.lastSocketErrorMessage = null;
|
|
370
373
|
this.token = null;
|
|
371
374
|
this.account = null;
|
|
372
375
|
this.requestID = 0;
|
|
@@ -451,18 +454,24 @@ class PhantasmaLink {
|
|
|
451
454
|
};
|
|
452
455
|
//Cleanup After Socket Closes
|
|
453
456
|
this.socket.onclose = function (event) {
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
457
|
+
const reason = event.reason && event.reason.length > 0
|
|
458
|
+
? event.reason
|
|
459
|
+
: that.lastSocketErrorMessage || (event.wasClean ? 'Wallet connection closed' : 'Connection terminated unexpectedly');
|
|
460
|
+
that.lastSocketErrorMessage = null;
|
|
461
|
+
if (that.requestCallback) {
|
|
462
|
+
that.handleSocketFailure(reason);
|
|
463
|
+
}
|
|
464
|
+
else if (!event.wasClean) {
|
|
465
|
+
that.handleSocketFailure(reason);
|
|
459
466
|
}
|
|
460
467
|
};
|
|
461
468
|
//Error Callback When Socket Has Error
|
|
462
469
|
this.socket.onerror = function (error) {
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
470
|
+
const errMsg = error && typeof error.message === 'string' && error.message.length > 0
|
|
471
|
+
? error.message
|
|
472
|
+
: 'WebSocket error';
|
|
473
|
+
that.lastSocketErrorMessage = errMsg;
|
|
474
|
+
that.onMessage('Error: ' + errMsg);
|
|
466
475
|
};
|
|
467
476
|
}
|
|
468
477
|
//Message Logging Util
|
|
@@ -492,13 +501,40 @@ class PhantasmaLink {
|
|
|
492
501
|
//Build Request and Send To Wallet Via Socket
|
|
493
502
|
sendLinkRequest(request, callback) {
|
|
494
503
|
this.onMessage('Sending Phantasma Link request: ' + request);
|
|
504
|
+
this.requestCallback = callback;
|
|
505
|
+
const socket = this.socket;
|
|
506
|
+
const openState = typeof WebSocket !== 'undefined' && typeof WebSocket.OPEN === 'number' ? WebSocket.OPEN : 1;
|
|
507
|
+
const isSocketOpen = socket && socket.readyState === openState;
|
|
508
|
+
if (!socket || !isSocketOpen) {
|
|
509
|
+
this.handleSocketFailure('Wallet connection is closed. Please reconnect to your wallet.');
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
495
512
|
if (this.token != null) {
|
|
496
513
|
request = request + '/' + this.dapp + '/' + this.token;
|
|
497
514
|
}
|
|
498
515
|
this.requestID++; //Object Nonce Increase?
|
|
499
516
|
request = this.requestID + ',' + request;
|
|
500
|
-
|
|
501
|
-
|
|
517
|
+
try {
|
|
518
|
+
socket.send(request);
|
|
519
|
+
}
|
|
520
|
+
catch (err) {
|
|
521
|
+
const errMessage = err && typeof err.message === 'string' && err.message.length > 0
|
|
522
|
+
? err.message
|
|
523
|
+
: 'Failed to send request to wallet';
|
|
524
|
+
this.handleSocketFailure(errMessage);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
handleSocketFailure(message) {
|
|
528
|
+
const callback = this.requestCallback;
|
|
529
|
+
this.requestCallback = null;
|
|
530
|
+
const errorMessage = message || 'Connection lost with Phantasma Link wallet';
|
|
531
|
+
if (callback) {
|
|
532
|
+
callback({ success: false, error: errorMessage });
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
if (this.onError) {
|
|
536
|
+
this.onError(errorMessage);
|
|
537
|
+
}
|
|
502
538
|
}
|
|
503
539
|
//Disconnect The Wallet Connection Socket
|
|
504
540
|
disconnect(triggered) {
|
|
@@ -5,6 +5,7 @@ import { bytesToHex } from '../utils/Hex';
|
|
|
5
5
|
export class PhantasmaLink {
|
|
6
6
|
//Construct The Link
|
|
7
7
|
constructor(dappID, logging = true) {
|
|
8
|
+
this.lastSocketErrorMessage = null;
|
|
8
9
|
this.requestID = 0;
|
|
9
10
|
//Message Logging
|
|
10
11
|
this.onMessage = (msg) => {
|
|
@@ -160,8 +161,9 @@ export class PhantasmaLink {
|
|
|
160
161
|
onErrorCallback(message);
|
|
161
162
|
return;
|
|
162
163
|
}
|
|
163
|
-
|
|
164
|
-
|
|
164
|
+
const txLengthLimit = 65536;
|
|
165
|
+
if (txHex.length >= txLengthLimit) {
|
|
166
|
+
const message = `Error: Carbon transaction message is too big (${txHex.length} > ${txLengthLimit})!`;
|
|
165
167
|
this.onMessage(message);
|
|
166
168
|
onErrorCallback(message);
|
|
167
169
|
return;
|
|
@@ -364,6 +366,7 @@ export class PhantasmaLink {
|
|
|
364
366
|
new PhantasmaLinkSocket()
|
|
365
367
|
: new WebSocket(path);
|
|
366
368
|
this.requestCallback = null;
|
|
369
|
+
this.lastSocketErrorMessage = null;
|
|
367
370
|
this.token = null;
|
|
368
371
|
this.account = null;
|
|
369
372
|
this.requestID = 0;
|
|
@@ -448,18 +451,24 @@ export class PhantasmaLink {
|
|
|
448
451
|
};
|
|
449
452
|
//Cleanup After Socket Closes
|
|
450
453
|
this.socket.onclose = function (event) {
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
454
|
+
const reason = event.reason && event.reason.length > 0
|
|
455
|
+
? event.reason
|
|
456
|
+
: that.lastSocketErrorMessage || (event.wasClean ? 'Wallet connection closed' : 'Connection terminated unexpectedly');
|
|
457
|
+
that.lastSocketErrorMessage = null;
|
|
458
|
+
if (that.requestCallback) {
|
|
459
|
+
that.handleSocketFailure(reason);
|
|
460
|
+
}
|
|
461
|
+
else if (!event.wasClean) {
|
|
462
|
+
that.handleSocketFailure(reason);
|
|
456
463
|
}
|
|
457
464
|
};
|
|
458
465
|
//Error Callback When Socket Has Error
|
|
459
466
|
this.socket.onerror = function (error) {
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
467
|
+
const errMsg = error && typeof error.message === 'string' && error.message.length > 0
|
|
468
|
+
? error.message
|
|
469
|
+
: 'WebSocket error';
|
|
470
|
+
that.lastSocketErrorMessage = errMsg;
|
|
471
|
+
that.onMessage('Error: ' + errMsg);
|
|
463
472
|
};
|
|
464
473
|
}
|
|
465
474
|
//Message Logging Util
|
|
@@ -489,13 +498,40 @@ export class PhantasmaLink {
|
|
|
489
498
|
//Build Request and Send To Wallet Via Socket
|
|
490
499
|
sendLinkRequest(request, callback) {
|
|
491
500
|
this.onMessage('Sending Phantasma Link request: ' + request);
|
|
501
|
+
this.requestCallback = callback;
|
|
502
|
+
const socket = this.socket;
|
|
503
|
+
const openState = typeof WebSocket !== 'undefined' && typeof WebSocket.OPEN === 'number' ? WebSocket.OPEN : 1;
|
|
504
|
+
const isSocketOpen = socket && socket.readyState === openState;
|
|
505
|
+
if (!socket || !isSocketOpen) {
|
|
506
|
+
this.handleSocketFailure('Wallet connection is closed. Please reconnect to your wallet.');
|
|
507
|
+
return;
|
|
508
|
+
}
|
|
492
509
|
if (this.token != null) {
|
|
493
510
|
request = request + '/' + this.dapp + '/' + this.token;
|
|
494
511
|
}
|
|
495
512
|
this.requestID++; //Object Nonce Increase?
|
|
496
513
|
request = this.requestID + ',' + request;
|
|
497
|
-
|
|
498
|
-
|
|
514
|
+
try {
|
|
515
|
+
socket.send(request);
|
|
516
|
+
}
|
|
517
|
+
catch (err) {
|
|
518
|
+
const errMessage = err && typeof err.message === 'string' && err.message.length > 0
|
|
519
|
+
? err.message
|
|
520
|
+
: 'Failed to send request to wallet';
|
|
521
|
+
this.handleSocketFailure(errMessage);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
handleSocketFailure(message) {
|
|
525
|
+
const callback = this.requestCallback;
|
|
526
|
+
this.requestCallback = null;
|
|
527
|
+
const errorMessage = message || 'Connection lost with Phantasma Link wallet';
|
|
528
|
+
if (callback) {
|
|
529
|
+
callback({ success: false, error: errorMessage });
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
if (this.onError) {
|
|
533
|
+
this.onError(errorMessage);
|
|
534
|
+
}
|
|
499
535
|
}
|
|
500
536
|
//Disconnect The Wallet Connection Socket
|
|
501
537
|
disconnect(triggered) {
|
|
@@ -9,6 +9,7 @@ export declare class PhantasmaLink {
|
|
|
9
9
|
onError: (message: any) => void;
|
|
10
10
|
socket: any;
|
|
11
11
|
requestCallback: any;
|
|
12
|
+
private lastSocketErrorMessage;
|
|
12
13
|
token: any;
|
|
13
14
|
requestID: number;
|
|
14
15
|
account: IAccount;
|
|
@@ -38,6 +39,7 @@ export declare class PhantasmaLink {
|
|
|
38
39
|
set dappID(dapp: any);
|
|
39
40
|
get dappID(): any;
|
|
40
41
|
sendLinkRequest(request: string, callback: (T: any) => void): void;
|
|
42
|
+
private handleSocketFailure;
|
|
41
43
|
disconnect(triggered: string | boolean | undefined): void;
|
|
42
44
|
private serializeCarbonTx;
|
|
43
45
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"phantasmaLink.d.ts","sourceRoot":"","sources":["../../../../src/core/link/phantasmaLink.ts"],"names":[],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