Skip to content

Commit

Permalink
Add review transaction method (#9)
Browse files Browse the repository at this point in the history
* feat: Handle serialized transaction instead of its hash

App method to review transactions which returns the computed transaction hash upon user approval

sign takes a hash

* Remove optional
  • Loading branch information
neithanmo authored Sep 18, 2024
1 parent a9857ea commit 1606a90
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export const KEY_LENGTH = 32
export const REDJUBJUB_SIGNATURE_LEN = 64
export const ED25519_SIGNATURE_LEN = 64
export const IDENTITY_LEN = VERSION + KEY_LENGTH + KEY_LENGTH + ED25519_SIGNATURE_LEN
export const TX_HASH_LEN = 32
13 changes: 13 additions & 0 deletions src/deserialize.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { TX_HASH_LEN } from './consts'

export const deserializeDkgRound1 = (data?: Buffer) => {
if (!data) throw new Error('unexpected empty data')

Expand Down Expand Up @@ -35,3 +37,14 @@ export const deserializeDkgRound2 = (data?: Buffer) => {
publicPackage,
}
}

export const deserializeReviewTx = (data?: Buffer) => {
if (!data) throw new Error('unexpected empty data')

// We expect a hash of 32 bytes
const hash = data.subarray(0, TX_HASH_LEN)

return {
hash,
}
}
21 changes: 20 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import GenericApp, { ConstructorParams, LedgerError, Transport, processErrorResp
import { ResponsePayload } from '@zondax/ledger-js/dist/payload'

import { P2_VALUES } from './consts'
import { deserializeDkgRound1, deserializeDkgRound2 } from './deserialize'
import { deserializeDkgRound1, deserializeDkgRound2, deserializeReviewTx } from './deserialize'
import { processGetIdentityResponse, processGetKeysResponse } from './helper'
import { serializeDkgGetCommitments, serializeDkgRound1, serializeDkgRound2, serializeDkgRound3Min, serializeDkgSign } from './serialize'
import {
Expand All @@ -32,6 +32,7 @@ import {
ResponseDkgRound2,
ResponseDkgSign,
ResponseIdentity,
ResponseReviewTransaction,
ResponseSign,
} from './types'

Expand Down Expand Up @@ -63,6 +64,7 @@ export default class IronfishApp extends GenericApp {
DKG_BACKUP_KEYS: 0x19,
DKG_RESTORE_KEYS: 0x1a,
GET_RESULT: 0x1b,
REVIEW_TX: 0x1c,
},
p1Values: {
ONLY_RETRIEVE: 0x00,
Expand Down Expand Up @@ -248,6 +250,23 @@ export default class IronfishApp extends GenericApp {
}
}

async reviewTransaction(tx: string): Promise<ResponseReviewTransaction> {
try {
const blob = Buffer.from(tx, 'hex')
const chunks = this.prepareChunks(DUMMY_PATH, blob)

let rawResponse: any
for (let i = 0; i < chunks.length; i += 1) {
rawResponse = await this.sendGenericChunk(this.INS.REVIEW_TX, P2_VALUES.DEFAULT, 1 + i, chunks.length, chunks[i])
}

let result = await this.getResult(rawResponse)
return deserializeReviewTx(result)
} catch (e) {
throw processErrorResponse(e)
}
}

async getResult(rawResponse: ResponsePayload): Promise<Buffer> {
let data = Buffer.alloc(0)

Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface IronfishIns extends INSGeneric {
DKG_BACKUP_KEYS: 0x19
DKG_RESTORE_KEYS: 0x1a
GET_RESULT: 0x1b
REVIEW_TX: 0x1c
}

export type KeyResponse = ResponseAddress | ResponseViewKey | ResponseProofGenKey
Expand Down Expand Up @@ -70,3 +71,6 @@ export interface ResponseDkgGetPublicPackage {
export interface ResponseDkgBackupKeys {
encryptedKeys: Buffer
}
export interface ResponseReviewTransaction {
hash: Buffer
}

0 comments on commit 1606a90

Please sign in to comment.