From 2dd805764d9448a0a881de4c8489f97fde045bb4 Mon Sep 17 00:00:00 2001 From: abenso Date: Fri, 20 Dec 2024 17:20:35 -0300 Subject: [PATCH] add spend auth signature --- src/app.ts | 35 ++++++++++++++++++++++++++++------- src/consts.ts | 3 +++ src/helper.ts | 16 ++++++++++++++-- src/types.ts | 12 ++++++++++++ 4 files changed, 57 insertions(+), 9 deletions(-) diff --git a/src/app.ts b/src/app.ts index 45a7a0e..16fedcd 100644 --- a/src/app.ts +++ b/src/app.ts @@ -17,17 +17,15 @@ import BaseApp, { BIP32Path, ConstructorParams, - LedgerError, - PAYLOAD_TYPE, - ResponsePayload, Transport, processErrorResponse, processResponse, } from '@zondax/ledger-js' -import { DEFAULT_PATH, P2_VALUES, PREHASH_LEN, RANDOMIZER_LEN, SIGRSLEN } from './consts' -import { processGetAddrResponse, processGetFvkResponse } from './helper' -import { AddressIndex, PenumbraIns, ResponseAddress, ResponseFvk, ResponseSign } from './types' +import { DEFAULT_PATH, P2_VALUES, RANDOMIZER_LEN, SPEND_AUTH_SIGNATURE_LEN } from './consts' +import { processGetAddrResponse, processGetFvkResponse, processSignResponse } from './helper' +import { AddressIndex, PenumbraIns, ResponseAddress, ResponseFvk, ResponseSign, ResponseSpendAuthSignatures, ResponseDelegatorVoteSignatures } from './types' +import { ByteStream } from '@zondax/ledger-js/dist/byteStream' // https://buf.build/penumbra-zone/penumbra/docs/main:penumbra.custody.v1#penumbra.custody.v1.ConfirmAddressRequest @@ -46,6 +44,8 @@ export class PenumbraApp extends BaseApp { SIGN: 0x02, FVK: 0x03, TX_METADATA: 0x04, + GET_SPEND_AUTH_SIGNATURES: 0x05, + GET_BINDING_SIGNATURES: 0x06, }, p1Values: { ONLY_RETRIEVE: 0x00, @@ -126,8 +126,29 @@ export class PenumbraApp extends BaseApp { for (let i = 1; i < chunks.length; i += 1) { signatureResponse = await this.signSendChunk(this.INS.SIGN, 1 + i, chunks.length, chunks[i]) } + + // | 64 bytes | 2 bytes | 2 bytes | + // | effect hash | spend auth signature qty | binding signature qty | + return processSignResponse(signatureResponse) + } catch (e) { + throw processErrorResponse(e) + } + } + + async getSpendAuthSignatures(index: number): Promise { + try { + const bs = new ByteStream() + bs.appendUint16(index) + const indexBuffer = bs.getCompleteBuffer() + // Use P2 to indicate the index of the signature + console.log("getSpendAuthSignatures!!!!!!!: ", indexBuffer, this.INS.GET_SPEND_AUTH_SIGNATURES) + const responseBuffer = await this.transport.send(this.CLA, this.INS.GET_SPEND_AUTH_SIGNATURES, this.P1_VALUES.ONLY_RETRIEVE, P2_VALUES.DEFAULT, indexBuffer) + + const payload = processResponse(responseBuffer) + const spendAuthSignature = payload.readBytes(SPEND_AUTH_SIGNATURE_LEN) + return { - signature: signatureResponse.readBytes(signatureResponse.length()), + spendAuth_signature: spendAuthSignature } } catch (e) { throw processErrorResponse(e) diff --git a/src/consts.ts b/src/consts.ts index 2ab4589..0a5dc7e 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -15,3 +15,6 @@ export const FVKLEN = AK_LEN + NK_LEN export const SIGRSLEN = 64 export const PREHASH_LEN = 32 export const RANDOMIZER_LEN = 12 +export const EFFECT_HASH_LEN = 64 +export const SPEND_AUTH_SIGNATURE_LEN = 64 +export const DELEGATOR_VOTE_SIGNATURE_LEN = 64 diff --git a/src/helper.ts b/src/helper.ts index 5442b43..3cdd336 100644 --- a/src/helper.ts +++ b/src/helper.ts @@ -1,7 +1,7 @@ import { ResponsePayload } from '@zondax/ledger-js/dist/payload' -import { ADDRLEN, AK_LEN, FVKLEN, NK_LEN } from './consts' -import { ResponseAddress, ResponseFvk } from './types' +import { ADDRLEN, AK_LEN, EFFECT_HASH_LEN, FVKLEN } from './consts' +import { ResponseAddress, ResponseFvk, ResponseSign } from './types' export function processGetAddrResponse(response: ResponsePayload): ResponseAddress { const address = response.readBytes(ADDRLEN) @@ -23,3 +23,15 @@ export function processGetFvkResponse(response: ResponsePayload): ResponseFvk { nk, } } + +export function processSignResponse(response: ResponsePayload): ResponseSign { + const signature = response.readBytes(EFFECT_HASH_LEN) + const spendAuth_signature_qty = response.readBytes(2).readUInt16LE(0) + const delegatorVote_signature_qty = response.readBytes(2).readUInt16LE(0) + + return { + signature, + spendAuth_signature_qty, + delegatorVote_signature_qty, + } +} diff --git a/src/types.ts b/src/types.ts index 54600b8..fa2a87b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -6,6 +6,8 @@ export interface PenumbraIns extends INSGeneric { SIGN: 0x02 FVK: 0x03 TX_METADATA: 0x04 + GET_SPEND_AUTH_SIGNATURES: 0x05 + GET_BINDING_SIGNATURES: 0x06 } export interface AddressIndex { @@ -29,4 +31,14 @@ export interface ResponseFvk { export interface ResponseSign { signature: Buffer + spendAuth_signature_qty: number + delegatorVote_signature_qty: number +} + +export interface ResponseSpendAuthSignatures { + spendAuth_signature: Buffer +} + +export interface ResponseDelegatorVoteSignatures { + delegatorVote_signature: Buffer }