Skip to content

Commit

Permalink
feat: Handle serialized transaction instead of its hash
Browse files Browse the repository at this point in the history
App method to review transactions which returns the computed transaction hash upon user approval

sign takes a hash
  • Loading branch information
neithanmo committed Sep 18, 2024
1 parent 319b019 commit 4582161
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
55 changes: 55 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
ResponseDkgRound3Min,
ResponseDkgSign,
ResponseIdentity,
ResponseReviewTransaction,
ResponseSign,
} from './types'

Expand Down Expand Up @@ -72,6 +73,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 @@ -540,6 +542,59 @@ export default class IronfishApp extends GenericApp {
}
}

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

try {
let response = Buffer.alloc(0)
let returnCode = 0
let errorCodeData = Buffer.alloc(0)
let errorMessage = ''
try {
response = await this.sendDkgChunk(this.INS.REVIEW_TX, 1, chunks.length, chunks[0])
// console.log("resp 0 " + response.toString("hex"))

errorCodeData = response.subarray(-2)
returnCode = errorCodeData[0] * 256 + errorCodeData[1]
errorMessage = errorCodeToString(returnCode)
} catch (e) {
// console.log(e)
}

for (let i = 1; i < chunks.length; i += 1) {
// eslint-disable-next-line no-await-in-loop
response = await this.sendDkgChunk(this.INS.REVIEW_TX, 1 + i, chunks.length, chunks[i])
// console.log("resp " + i + " " + response.toString("hex"))

errorCodeData = response.subarray(-2)
returnCode = errorCodeData[0] * 256 + errorCodeData[1]
errorMessage = errorCodeToString(returnCode)

// console.log("returnCode " + returnCode)
if (returnCode !== LedgerError.NoErrors) {
return {
returnCode,
errorMessage,
}
}
}

let { isError, responseResult, rawResponse } = this.checkResponseCode(response)
if (isError) return responseResult

let result = await this.getResult(rawResponse)

return {
returnCode: result.returnCode,
errorMessage: result.errorMessage,
hash: result.data,
}
} catch (e) {
return processErrorResponse(e)
}
}

async getResult(rawResponse: Buffer): Promise<{ data?: Buffer; returnCode: number; errorMessage: string }> {
let data = Buffer.alloc(0)

Expand Down
5 changes: 4 additions & 1 deletion src/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ export const serializeDkgSign = (pkRandomness: string, frostSigningPackage: stri
let frostSigningPackageLen = frostSigningPackage.length / 2
let txHashLen = 32

let blob = Buffer.alloc(2 + pkRandomnessLen + 2 + frostSigningPackageLen + txHashLen)
// Use 4-bytes for transaction length
let blob = Buffer.alloc(2 + pkRandomnessLen + 2 + frostSigningPackageLen + 4 + txHashLen)
console.log(`dkgSign msg size: ${blob.byteLength}`)

let pos = 0
Expand All @@ -170,6 +171,8 @@ export const serializeDkgSign = (pkRandomness: string, frostSigningPackage: stri
pos += 2
blob.fill(Buffer.from(frostSigningPackage, 'hex'), pos)
pos += frostSigningPackageLen
blob.writeUint32BE(txHashLen, pos)
pos += 4

blob.fill(Buffer.from(txHash, 'hex'), pos)

Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,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 @@ -74,3 +75,6 @@ export interface ResponseDkgGetPublicPackage extends ResponseBase {
export interface ResponseDkgBackupKeys extends ResponseBase {
encryptedKeys?: Buffer
}
export interface ResponseReviewTransaction extends ResponseBase {
hash?: Buffer
}

0 comments on commit 4582161

Please sign in to comment.