Skip to content

Commit

Permalink
add spend auth signature
Browse files Browse the repository at this point in the history
  • Loading branch information
abenso committed Dec 20, 2024
1 parent 32f9c7f commit 2dd8057
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 9 deletions.
35 changes: 28 additions & 7 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,
Expand Down Expand Up @@ -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<ResponseSpendAuthSignatures> {
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)
Expand Down
3 changes: 3 additions & 0 deletions src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 14 additions & 2 deletions src/helper.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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,
}
}
12 changes: 12 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

0 comments on commit 2dd8057

Please sign in to comment.