Skip to content

Commit

Permalink
Merge pull request #52 from randlabs/feature/new-connect-params
Browse files Browse the repository at this point in the history
Added new interfaces
  • Loading branch information
Ignacio-87 committed Jan 5, 2022
2 parents 4119283 + f75c6a4 commit a8dd546
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 13 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# MyAlgo Connect Change log

## [1.1.2] - 10-01-2022

### Added

- Added new param in the constructor `disableLedgerNano`
- Added two new params for connect method `openManager` and `shouldSelectOneAccount`

### Updated

- Removed belterjs from dependencies
- Improved popup handlers

### Known issue

- MyAlgo Connect package still has the `Buffer is not defined` error (#27).

## [1.1.1] - 01-09-2021

### Added
Expand Down
6 changes: 4 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,12 @@ export interface Accounts {
export interface Options {
timeout?: number;
bridgeUrl?: string;
disableLedgerNano?: boolean;
}

export interface ConnectionSettings {
shouldSelectOneAccount: boolean;
shouldSelectOneAccount?: boolean;
openManager?: boolean;
}

export default class MyAlgoConnect {
Expand All @@ -171,7 +173,7 @@ export default class MyAlgoConnect {
/**
* @async
* @description Receives user's accounts from MyAlgo.
* @param {ConnectionSettings} Connection settings
* @param {ConnectionSettings} [settings] Connection settings
* @returns Returns an array of Algorand addresses.
*/
connect(settings?: ConnectionSettings): Promise<Accounts[]>;
Expand Down
51 changes: 41 additions & 10 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,17 @@ const bridge = new Messaging();
* @type {object}
* @property {string} [bridgeUrl] Override wallet.myalgo.com default frame url.
* @property {number} [timeout] Number of msec to wait the popup response, default value: 1600000 msec.
* @property {boolean} [disableLedgerNano] It will disable ledger nano accounts and returns only mnemonic accounts.
*/

/**
* @description Connect method settings
* @typedef ConnectionSettings
* @type {object}
* @property {boolean} [shouldSelectOneAccount] Only returns one account
* @property {boolean} [openManager] Open account manager
*/

/**
* @description Algorand account address
* @typedef Address
Expand Down Expand Up @@ -233,6 +242,12 @@ class MyAlgoConnect {
*/
this.options = { waitForReply: true, timeout: this.timeout };

/**
* @access private
* @description Disable ledger nano
* @type {boolean}
*/
this.disableLedgerNano = (options && options.disableLedgerNano) ? options.disableLedgerNano : false;
}

/**
Expand All @@ -242,15 +257,14 @@ class MyAlgoConnect {
* @param {ConnectionSettings} settings Connect settings
* @returns {Promise<string[]>} Returns allowed accounts by the user.
*/
async connect(settings = { shouldSelectOneAccount: false }) {
async connect(settings = { shouldSelectOneAccount: false, openManager: false }) {

if (this.currentConnectPopup) {
if (this.currentConnectPopup.closed) {
this.currentConnectPopup = null;
}
else {
this.currentConnectPopup.focus();
throw new Error(Errors.WINDOW_IS_OPENED);
this.focusWindow(this.currentConnectPopup);
}
}

Expand All @@ -261,7 +275,7 @@ class MyAlgoConnect {

const res = await this.bridge.sendMessage(
this.currentConnectPopup,
{ method: "unlock", params: { shouldSelectOneAccount: settings.shouldSelectOneAccount } },
{ method: "unlock", params: Object.assign(settings, { disableLedgerNano: this.disableLedgerNano }) },
this.url, this.options
);

Expand Down Expand Up @@ -297,8 +311,7 @@ class MyAlgoConnect {
this.currentSigntxPopup = null;
}
else {
this.currentSigntxPopup.focus();
throw new Error(Errors.WINDOW_IS_OPENED);
this.focusWindow(this.currentSigntxPopup);
}
}

Expand All @@ -314,8 +327,10 @@ class MyAlgoConnect {

// Send transaction info
const res = await this.bridge.sendMessage(
this.currentSigntxPopup,
{ method: "transaction", params: { txn } },
this.currentSigntxPopup, {
method: "transaction",
params: { txn, settings: { disableLedgerNano: this.disableLedgerNano } }
},
this.url, this.options
);

Expand Down Expand Up @@ -360,8 +375,7 @@ class MyAlgoConnect {
this.currentSignLogicSigPopup = null;
}
else {
this.currentSignLogicSigPopup.focus();
throw new Error(Errors.WINDOW_IS_OPENED);
this.focusWindow(this.currentSignLogicSigPopup);
}
}

Expand Down Expand Up @@ -429,6 +443,23 @@ class MyAlgoConnect {
window.close();
}
}

/**
* @access private
* @description Focus current popup
* @param {Window} window Window object
* @returns {void}
* @throws {"Windows is opened"}
*/
focusWindow(window) {
if (window && window.focus) {
window.focus();
throw new Error(Errors.WINDOW_IS_OPENED);
}
else {
throw new Error(Errors.INVALID_WINDOW);
}
}
}

module.exports = MyAlgoConnect;
3 changes: 2 additions & 1 deletion lib/utils/errors.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
WINDOW_NOT_LOADED: "Window not loaded",
WINDOW_IS_OPENED: "Windows is opened",
WINDOW_NOT_OPENED: "Can not open popup window"
WINDOW_NOT_OPENED: "Can not open popup window",
INVALID_WINDOW: "Invalid window",
};

0 comments on commit a8dd546

Please sign in to comment.