Skip to content

Commit

Permalink
DEXW-5017: wavesLedger instance (#97)
Browse files Browse the repository at this point in the history
* DEXW-5017: wavesLedger instance

* 6.1.4

* 6.1.5

Co-authored-by: okanishcheva <[email protected]>
Co-authored-by: vba2000 <[email protected]>
  • Loading branch information
3 people authored May 4, 2022
1 parent 77eee4a commit 8f34200
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@waves/signature-adapter",
"version": "6.1.3",
"version": "6.1.5",
"types": "dist/index.d.ts",
"main": "dist/index.js",
"license": "MIT",
Expand Down
27 changes: 17 additions & 10 deletions src/adapters/LedgerAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class LedgerAdapter extends Adapter {
private static _ledger: WavesLedger;
//@ts-ignore
private static _hasConnectionPromise;

private static _wavesLedgerOptions: IWavesLedger;

//@ts-ignore
constructor(user) {
Expand Down Expand Up @@ -52,20 +52,20 @@ export class LedgerAdapter extends Adapter {
}

public getAdapterVersion() {
return LedgerAdapter._ledger.getVersion();
return LedgerAdapter.getLedgerInstance().getVersion();
}

public signRequest(bytes: Uint8Array): Promise<string> {
return this._isMyLedger()
.then(() => LedgerAdapter._ledger.signRequest(this._currentUser.id, { dataBuffer: bytes }));
.then(() => LedgerAdapter.getLedgerInstance().signRequest(this._currentUser.id, { dataBuffer: bytes }));
}

public signTransaction(bytes: Uint8Array, precision: Record<string, number>, signData: any): Promise<string> {
if (bytes[0] === 15) {
return this.signData(bytes);
}
return this._isMyLedger()
.then(() => LedgerAdapter._ledger.signTransaction(
.then(() => LedgerAdapter.getLedgerInstance().signTransaction(
this._currentUser.id, {
amount2Precision: precision.amount2Precision,
amountPrecision: precision.amountPrecision,
Expand All @@ -78,7 +78,7 @@ export class LedgerAdapter extends Adapter {

public signOrder(bytes: Uint8Array, precision: Record<string, number>, data: any): Promise<string> {
return this._isMyLedger()
.then(() => LedgerAdapter._ledger.signOrder(this._currentUser.id, {
.then(() => LedgerAdapter.getLedgerInstance().signOrder(this._currentUser.id, {
dataBuffer: bytes,
amountPrecision: precision.amountPrecision,
feePrecision: precision.feePrecision,
Expand All @@ -88,7 +88,7 @@ export class LedgerAdapter extends Adapter {

public signData(bytes: Uint8Array): Promise<string> {
return this._isMyLedger()
.then(() => LedgerAdapter._ledger.signSomeData(this._currentUser.id, { dataBuffer: bytes }));
.then(() => LedgerAdapter.getLedgerInstance().signSomeData(this._currentUser.id, { dataBuffer: bytes }));
}

public getEncodedSeed() {
Expand Down Expand Up @@ -126,7 +126,7 @@ export class LedgerAdapter extends Adapter {
}

protected _isMyLedger() {
const promise = LedgerAdapter._ledger.getUserDataById(this._currentUser.id)
const promise = LedgerAdapter.getLedgerInstance().getUserDataById(this._currentUser.id)
//@ts-ignore
.then(user => {
if (user.address !== this._currentUser.address) {
Expand All @@ -143,17 +143,17 @@ export class LedgerAdapter extends Adapter {
}

public static getUserList(from: number = 1, to: number = 1) {
return LedgerAdapter._ledger.getPaginationUsersData(from, to) as any;
return LedgerAdapter.getLedgerInstance().getPaginationUsersData(from, to) as any;
}

public static initOptions(options: IWavesLedger) {
Adapter.initOptions(options);
this._ledger = new WavesLedger( options );
LedgerAdapter._wavesLedgerOptions = options;
}

public static isAvailable() {
if (!LedgerAdapter._hasConnectionPromise) {
LedgerAdapter._hasConnectionPromise = LedgerAdapter._ledger.probeDevice();
LedgerAdapter._hasConnectionPromise = LedgerAdapter.getLedgerInstance().probeDevice();
}

return LedgerAdapter._hasConnectionPromise.then(() => {
Expand All @@ -165,6 +165,13 @@ export class LedgerAdapter extends Adapter {
return false;
});
}

protected static getLedgerInstance(): WavesLedger {
if (!LedgerAdapter._ledger) {
LedgerAdapter._ledger = new WavesLedger(LedgerAdapter._wavesLedgerOptions);
}
return LedgerAdapter._ledger;
}
}

interface IWavesLedger {
Expand Down

0 comments on commit 8f34200

Please sign in to comment.