Skip to content

Commit

Permalink
Remove duplicated code by using our generic ledger js package to hand…
Browse files Browse the repository at this point in the history
…le error response function and improve FVK type definition

fix subarray offset
  • Loading branch information
neithanmo committed Nov 20, 2024
1 parent 0a5d766 commit b9c0286
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 33 deletions.
23 changes: 16 additions & 7 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************* */
import BaseApp, { BIP32Path, ConstructorParams, LedgerError, PAYLOAD_TYPE, Transport, processErrorResponse } from '@zondax/ledger-js'
import BaseApp, {
BIP32Path,
ConstructorParams,
LedgerError,
PAYLOAD_TYPE,
Transport,
processErrorResponse,
processResponse,
} from '@zondax/ledger-js'

import { DEFAULT_PATH, P2_VALUES, PREHASH_LEN, RANDOMIZER_LEN, SIGRSLEN } from './consts'
import { processGetAddrResponse, processGetFvkResponse } from './helper'
Expand Down Expand Up @@ -53,7 +61,8 @@ export class PenumbraApp extends BaseApp {
try {
const responseBuffer = await this.transport.send(this.CLA, this.INS.GET_ADDR, this.P1_VALUES.ONLY_RETRIEVE, P2_VALUES.DEFAULT, data)

const response = processGetAddrResponse(responseBuffer)
const payload = processResponse(responseBuffer)
const response = processGetAddrResponse(payload)

return {
address: response.address,
Expand All @@ -75,7 +84,8 @@ export class PenumbraApp extends BaseApp {
data
)

const response = processGetAddrResponse(responseBuffer)
const payload = processResponse(responseBuffer)
const response = processGetAddrResponse(payload)

return {
address: response.address,
Expand All @@ -92,11 +102,10 @@ export class PenumbraApp extends BaseApp {
try {
const responseBuffer = await this.transport.send(this.CLA, this.INS.FVK, this.P1_VALUES.ONLY_RETRIEVE, P2_VALUES.DEFAULT, data)

const response = processGetFvkResponse(responseBuffer)
const payload = processResponse(responseBuffer)
const response = processGetFvkResponse(payload)

return {
fvk: response.fvk,
} as ResponseFvk
return response
} catch (e) {
throw processErrorResponse(e)
}
Expand Down
6 changes: 5 additions & 1 deletion src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ export const P2_VALUES = {

export const PKLEN = 65
export const ADDRLEN = 80
export const FVKLEN = 64
// Verification key length
export const AK_LEN = 32
// Nullifier key length
export const NK_LEN = 32
export const FVKLEN = AK_LEN + NK_LEN
export const SIGRSLEN = 64
export const PREHASH_LEN = 32
export const RANDOMIZER_LEN = 12
29 changes: 14 additions & 15 deletions src/helper.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
import { ADDRLEN, FVKLEN } from "./consts";
import { ResponseAddress, ResponseFvk } from "./types";
import { ResponsePayload } from '@zondax/ledger-js/dist/payload'

export function processGetAddrResponse(response: Buffer): ResponseAddress {
const errorCodeData = response.subarray(-2);
const returnCode = errorCodeData[0] * 256 + errorCodeData[1];
import { ADDRLEN, AK_LEN, FVKLEN, NK_LEN } from './consts'
import { ResponseAddress, ResponseFvk } from './types'

const address = Buffer.from(response.subarray(0, ADDRLEN));
response = response.subarray(ADDRLEN);
export function processGetAddrResponse(response: ResponsePayload): ResponseAddress {
const address = response.readBytes(ADDRLEN)

return {
address,
};
}
}

export function processGetFvkResponse(response: Buffer): ResponseFvk {
const errorCodeData = response.subarray(-2);
const returnCode = errorCodeData[0] * 256 + errorCodeData[1];
export function processGetFvkResponse(response: ResponsePayload): ResponseFvk {
const keys = response.readBytes(FVKLEN)

const fvk = Buffer.from(response.subarray(0, FVKLEN));
response = response.subarray(FVKLEN);
// Extract ak and nullifier_key
const ak = Buffer.from(keys.subarray(0, AK_LEN))
const nk = Buffer.from(keys.subarray(32, FVKLEN))

return {
fvk,
};
ak,
nk,
}
}
26 changes: 16 additions & 10 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
import { INSGeneric } from "@zondax/ledger-js";
import { INSGeneric } from '@zondax/ledger-js'

export interface PenumbraIns extends INSGeneric {
GET_VERSION: 0x00;
GET_ADDR: 0x01;
SIGN: 0x02;
FVK: 0x03;
GET_VERSION: 0x00
GET_ADDR: 0x01
SIGN: 0x02
FVK: 0x03
}

export interface AddressIndex {
account: number;
randomizer?: Buffer;
account: number
randomizer?: Buffer
}

export interface ResponseAddress {
address?: Buffer;
address?: Buffer
}

// The full viewing key consists of two components:
//
// - ak
// - nk
//
export interface ResponseFvk {
fvk?: Buffer;
ak: Buffer
nk: Buffer
}

export interface ResponseSign {
signature: Buffer;
signature: Buffer
}

0 comments on commit b9c0286

Please sign in to comment.