Skip to content

Commit

Permalink
touch up ledger class
Browse files Browse the repository at this point in the history
  • Loading branch information
jurevans committed Jun 1, 2023
1 parent 6a409cf commit 0274838
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 46 deletions.
4 changes: 3 additions & 1 deletion apps/extension/src/Setup/Ledger/Ledger.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ const Ledger: React.FC<Props> = ({ requester: _ }) => {

const queryLedger = async (ledger: LedgerApp): Promise<void> => {
const pk = await ledger.getPublicKey();
const { appName, appVersion } = ledger.status();
const {
info: { appName, appVersion },
} = await ledger.status();

setAppInfo({ name: appName, version: appVersion });
setPublicKey(pk);
Expand Down
69 changes: 29 additions & 40 deletions apps/extension/src/background/ledger/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ export const initLedgerHIDTransport = async (): Promise<Transport> => {
export const DEFAULT_LEDGER_BIP44_PATH = `m/44'/${bip44CoinType}'/0'/0/0`;

export class Ledger {
public version: ResponseVersion | undefined;
public info: ResponseAppInfo | undefined;

constructor(public readonly namadaApp: NamadaApp | undefined = undefined) {}

/**
Expand All @@ -38,52 +35,25 @@ export class Ledger {
const namadaApp = new NamadaApp(initializedTransport);
const ledger = new Ledger(namadaApp);

ledger.version = await namadaApp.getVersion();
ledger.info = await namadaApp.getAppInfo();

return ledger;
}

/**
* Return status and version info of initialized NamadaApp
*/
public status(): {
appName: string;
appVersion: string;
errorMessage: string;
returnCode: number;
deviceLocked: boolean;
major: number;
minor: number;
patch: number;
targetId: string;
testMode: boolean;
} {
if (!this.version || !this.info) {
public async status(): Promise<{
version: ResponseVersion;
info: ResponseAppInfo;
}> {
if (!this.namadaApp) {
throw new Error("NamadaApp is not initialized!");
}

const { appName, appVersion, errorMessage, returnCode } = this.info;
const {
major,
minor,
patch,
targetId,
deviceLocked = false,
testMode,
} = this.version;
const version = await this.namadaApp.getVersion();
const info = await this.namadaApp.getAppInfo();

return {
appName,
appVersion,
errorMessage,
returnCode,
deviceLocked,
major,
minor,
patch,
targetId,
testMode,
version,
info,
};
}

Expand Down Expand Up @@ -120,8 +90,17 @@ export class Ledger {

/**
* Sign tx bytes with the key associated with provided (or default) path
*
* @async
* @param {ArrayBuffer} tx - tx data blob to sign
* @param {string} bip44Path (optional) - Bip44 path for signing account
*
* @returns {ResponseSign}
*/
public async sign(tx: ArrayBuffer, bip44Path: string): Promise<ResponseSign> {
public async sign(
tx: ArrayBuffer,
bip44Path?: string
): Promise<ResponseSign> {
if (!this.namadaApp) {
throw new Error("NamadaApp is not initialized!");
}
Expand All @@ -130,4 +109,14 @@ export class Ledger {

return await this.namadaApp.sign(path, Buffer.from(tx));
}

/**
* Query status to determine if device is locked
*/
public async isLocked(): Promise<boolean> {
const {
version: { deviceLocked },
} = await this.status();
return deviceLocked;
}
}
39 changes: 34 additions & 5 deletions apps/extension/src/background/ledger/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ export class LedgerService {
async submitTransfer(
txMsg: string,
address: string,
txId: string
msgId: string
): Promise<void> {
const msgId = txId;

const tabs = await syncTabs(
this.connectedTabsStore,
this.requester,
Expand All @@ -66,10 +64,41 @@ export class LedgerService {
txMsgBuffer
);

// TODO: We need actual tx data to sign:
// TODO: We need actual tx data to sign. txMsg contains all transfer data we need to construct
// a tx, however, this needs to be constructed properly for Ledger to parse and sign (e.g.,
// header, data, code):
const txData = new Uint8Array();
const signature = await ledger.sign(txData, path);
console.log("TODO:", { signature, tabs, msgId, txDetails });

console.log("TODO:", {
msgId,
txDetails,
tabs,
});

const { headerSignature, dataSignature, codeSignature } = signature;
// TODO: Testing signatures
console.log(
"header",
headerSignature.hash,
headerSignature.salt,
headerSignature.pubkey,
headerSignature.signature
);
console.log(
"data",
dataSignature.hash,
dataSignature.salt,
dataSignature.pubkey,
dataSignature.signature
);
console.log(
"code",
codeSignature.hash,
codeSignature.salt,
codeSignature.pubkey,
codeSignature.signature
);
return;
}

Expand Down

0 comments on commit 0274838

Please sign in to comment.