Skip to content

Commit

Permalink
refactor front end api
Browse files Browse the repository at this point in the history
  • Loading branch information
arobsn committed Jul 3, 2024
1 parent df58273 commit 7cec338
Showing 1 changed file with 47 additions and 54 deletions.
101 changes: 47 additions & 54 deletions src/erg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ export const CLA = 0xe0;
* Ergo's Ledger hardware wallet API
*/
export class ErgoLedgerApp {
private _device: Device;
private _authToken: number;
private _useAuthToken: boolean;
private _logging: boolean;
#device: Device;
#authToken: number;
#useAuthToken: boolean;
#logging: boolean;

public get authToken(): number | undefined {
return this._useAuthToken ? this._authToken : undefined;
get authToken(): number | undefined {
return this.#useAuthToken ? this.#authToken : undefined;
}

public get transport(): Transport {
return this._device.transport;
get transport(): Transport {
return this.#device.transport;
}

constructor(transport: Transport);
Expand All @@ -60,19 +60,19 @@ export class ErgoLedgerApp {
scrambleKey
);

this._device = new Device(transport, CLA);
this._authToken = !authToken ? this.newAuthToken() : authToken;
this._useAuthToken = true;
this._logging = false;
this.#device = new Device(transport, CLA);
this.#authToken = !authToken ? this.#newAuthToken() : authToken;
this.#useAuthToken = true;
this.#logging = false;
}

/**
* Use authorization token to keep session opened.
* @param use
* @returns
*/
public useAuthToken(use = true): ErgoLedgerApp {
this._useAuthToken = use;
useAuthToken(use = true): ErgoLedgerApp {
this.#useAuthToken = use;
return this;
}

Expand All @@ -81,16 +81,16 @@ export class ErgoLedgerApp {
* @param enable
* @returns
*/
public enableDebugMode(enable = true): ErgoLedgerApp {
this._logging = enable;
enableDebugMode(enable = true): ErgoLedgerApp {
this.#logging = enable;
return this;
}

private newAuthToken(): number {
#newAuthToken(): number {
let newToken = 0;
do {
newToken = Math.floor(Math.random() * 0xffffffff) + 1;
} while (newToken === this._authToken);
} while (newToken === this.#authToken);

return newToken;
}
Expand All @@ -99,73 +99,70 @@ export class ErgoLedgerApp {
* Get application version.
* @returns a Promise with the Ledger Application version.
*/
public async getAppVersion(): Promise<Version> {
this._debug("getAppVersion");
return getAppVersion(this._device);
async getAppVersion(): Promise<Version> {
this.#debug("getAppVersion");
return getAppVersion(this.#device);
}

/**
* Get application name.
* @returns a Promise with the Ledger Application name.
*/
public async getAppName(): Promise<AppName> {
this._debug("getAppName");
return getAppName(this._device);
async getAppName(): Promise<AppName> {
this.#debug("getAppName");
return getAppName(this.#device);
}

/**
* Get the extended public key.
* @param path BIP32 path.
* @returns a Promise with the **chain code** and the **public key** for provided BIP32 path.
*/
public async getExtendedPublicKey(path: string): Promise<ExtendedPublicKey> {
this._debug("getExtendedPublicKey", path);
return getExtendedPublicKey(this._device, path, this.authToken);
async getExtendedPublicKey(path: string): Promise<ExtendedPublicKey> {
this.#debug("getExtendedPublicKey", path);
return getExtendedPublicKey(this.#device, path, this.authToken);
}

/**
* Derive the address for a given Bip44 path.
* @param path Bip44 path.
* @returns a Promise with the derived address in hex format.
*/
public async deriveAddress(
path: string,
network = Network.Mainnet
): Promise<DerivedAddress> {
this._debug("deriveAddress", path);
return deriveAddress(this._device, network, path, this.authToken);
async deriveAddress(path: string, network = Network.Mainnet): Promise<DerivedAddress> {
this.#debug("deriveAddress", path);
return deriveAddress(this.#device, network, path, this.authToken);
}

/**
* Derive and show the address on device screen for a given Bip44 path.
* @param path Bip44 path.
* @returns a Promise with true if the user accepts or throws an exception if it get rejected.
*/
public async showAddress(path: string, network = Network.Mainnet): Promise<boolean> {
this._debug("showAddress", path);
return showAddress(this._device, network, path, this.authToken);
async showAddress(path: string, network = Network.Mainnet): Promise<boolean> {
this.#debug("showAddress", path);
return showAddress(this.#device, network, path, this.authToken);
}

public async attestInput(box: UnsignedBox): Promise<AttestedBox> {
this._debug("attestInput", box);
return this._attestInput(box);
async attestInput(box: UnsignedBox): Promise<AttestedBox> {
this.#debug("attestInput", box);
return this.#attestInput(box);
}

private async _attestInput(box: UnsignedBox): Promise<AttestedBox> {
return attestInput(this._device, box, this.authToken);
async #attestInput(box: UnsignedBox): Promise<AttestedBox> {
return attestInput(this.#device, box, this.authToken);
}

public async signTx(
async signTx(
tx: UnsignedTransaction,
network = Network.Mainnet
): Promise<Uint8Array[]> {
this._debug("signTx", { tx, network });
this.#debug("signTx", { tx, network });

if (!tx.inputs || tx.inputs.length === 0) {
throw new DeviceError(RETURN_CODE.BAD_INPUT_COUNT);
}

const attestedInputs = await this._attestInputs(tx.inputs);
const attestedInputs = await this.#attestInputs(tx.inputs);
const signPaths = uniq(tx.inputs.map((i) => i.signPath));
const attestedTx: AttestedTransaction = {
inputs: attestedInputs,
Expand All @@ -178,7 +175,7 @@ export class ErgoLedgerApp {
const signatures: SignTransactionResponse = {};
for (const path of signPaths) {
signatures[path] = await signTx(
this._device,
this.#device,
attestedTx,
path,
network,
Expand All @@ -187,28 +184,24 @@ export class ErgoLedgerApp {
}

const signBytes: Uint8Array[] = [];
for (const input of tx.inputs) {
signBytes.push(signatures[input.signPath]);
}
for (const input of tx.inputs) signBytes.push(signatures[input.signPath]);

return signBytes;
}

private async _attestInputs(inputs: UnsignedBox[]): Promise<AttestedBox[]> {
async #attestInputs(inputs: UnsignedBox[]): Promise<AttestedBox[]> {
const attestedBoxes: AttestedBox[] = [];
for (const box of inputs) {
const attestedBox = await this._attestInput(box);
const attestedBox = await this.#attestInput(box);
attestedBox.setExtension(box.extension);
attestedBoxes.push(attestedBox);
}

return attestedBoxes;
}

private _debug(caller: string, message: unknown = "") {
if (!this._logging) {
return;
}
#debug(caller: string, message: unknown = "") {
if (!this.#logging) return;

console.debug(
`[ledger-ergo-js][${caller}]${message ? ": " : ""}${message ? JSON.stringify(message) : ""}`
Expand Down

0 comments on commit 7cec338

Please sign in to comment.