Skip to content

Commit

Permalink
feat: add support to unified full viewing keys instruction (Zondax#127)
Browse files Browse the repository at this point in the history
* feat: add support to unified full view keys method

* chore: add snapshots

* feat: add test case for non expert mode
  • Loading branch information
emmanuelm41 authored and pacu committed Dec 26, 2024
1 parent 20c5c7f commit 14b2c5e
Show file tree
Hide file tree
Showing 48 changed files with 151 additions and 1 deletion.
1 change: 1 addition & 0 deletions js/src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const INS = {
GET_ADDR_SECP256K1: 0x01,
SIGN_SECP256K1: 0x02,
SIGN_SAPLING: 0x12,
GET_UNIFIED_ADDR_SECP256K1: 0x13,

GET_DIV_LIST: 0x09,
GET_ADDR_SAPLING_DIV: 0x10,
Expand Down
44 changes: 43 additions & 1 deletion js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ import {
TRANSPARENT_PK_LEN,
} from './consts'
import {
AddressResponse, DfvkResponse,
AddressResponse,
DfvkResponse,
DiversifierListResponse,
ExtractSpendResponse,
FvkResponse,
Expand All @@ -45,6 +46,7 @@ import {
SignResponse,
SpendSignatureResponse,
TransaparentSignatureResponse,
UfvkResponse,
} from './types'
import { signSendChunkv1 } from './utils'

Expand All @@ -70,6 +72,7 @@ export default class ZCashApp extends GenericApp {
////////////////////////////////////////////
////////////////////////////////////////////


async getAddressTransparent(path: string, showInScreen = true): Promise<AddressResponse> {
try {
const sentToDevice = serializePath(path)
Expand Down Expand Up @@ -328,6 +331,45 @@ export default class ZCashApp extends GenericApp {
}
}

async getUfvk(zip32Account: number): Promise<UfvkResponse> {
try {
const serializedZip32Acc = Buffer.alloc(4)
serializedZip32Acc.writeUInt32LE(zip32Account + 0x80000000, 0)
let responseBuffer = await this.transport.send(CLA, INS.GET_DFVK_SAPLING, 0, 0, serializedZip32Acc, [0x9000])
let response = processResponse(responseBuffer)

console.log(response.length())

const akRaw = response.readBytes(SAPLING_AK_LEN)
const nkRaw = response.readBytes(SAPLING_NK_LEN)
const ovkRaw = response.readBytes(SAPLING_OVK_LEN)
const dkRaw = response.readBytes(SAPLING_DK_LEN)

const serializedUnifiedTransparentAcc = serializePath(`m/44'/133'/${zip32Account}'`, [3])
responseBuffer = await this.transport.send(CLA, INS.GET_UNIFIED_ADDR_SECP256K1, 0, 0, serializedUnifiedTransparentAcc, [0x9000])
response = processResponse(responseBuffer)

console.log(response.length())

const pkRaw = response.readBytes(TRANSPARENT_PK_LEN)

return {
sapling: {
akRaw,
nkRaw,
ovkRaw,
dkRaw
},
transparent: {
pkRaw
},
orchard: null
}
} catch (error) {
throw processErrorResponse(error)
}
}

////////////////////////////////////
////////////////////////////////////
////////////////////////////////////
Expand Down
13 changes: 13 additions & 0 deletions js/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ export type DfvkResponse = {
dkRaw: Buffer
}

export type UfvkResponse = {
sapling: {
akRaw: Buffer
nkRaw: Buffer
ovkRaw: Buffer
dkRaw: Buffer
},
transparent: {
pkRaw: Buffer
},
orchard: {} | null
}

export type DiversifierListResponse = {
diversifiers: Buffer[]
}
Expand Down
Binary file added tests_zemu/snapshots/fl-get-ufvk-expert/00000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/fl-get-ufvk-expert/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/fl-get-ufvk-expert/00002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/fl-get-ufvk/00000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/fl-get-ufvk/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/fl-get-ufvk/00002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/fl-get-ufvk/00003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/s-get-ufvk-expert/00000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/s-get-ufvk-expert/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/s-get-ufvk-expert/00002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/s-get-ufvk-expert/00003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/s-get-ufvk-expert/00004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/s-get-ufvk/00000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/s-get-ufvk/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/s-get-ufvk/00002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-get-ufvk-expert/00000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-get-ufvk-expert/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-get-ufvk-expert/00002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-get-ufvk-expert/00003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-get-ufvk-expert/00004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-get-ufvk-expert/00005.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-get-ufvk/00000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-get-ufvk/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-get-ufvk/00002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-get-ufvk/00003.png
Binary file added tests_zemu/snapshots/st-get-ufvk-expert/00001.png
Binary file added tests_zemu/snapshots/st-get-ufvk-expert/00002.png
Binary file added tests_zemu/snapshots/st-get-ufvk-expert/00003.png
Binary file added tests_zemu/snapshots/st-get-ufvk/00000.png
Binary file added tests_zemu/snapshots/st-get-ufvk/00001.png
Binary file added tests_zemu/snapshots/st-get-ufvk/00002.png
Binary file added tests_zemu/snapshots/st-get-ufvk/00003.png
Binary file added tests_zemu/snapshots/x-get-ufvk-expert/00000.png
Binary file added tests_zemu/snapshots/x-get-ufvk-expert/00001.png
Binary file added tests_zemu/snapshots/x-get-ufvk-expert/00002.png
Binary file added tests_zemu/snapshots/x-get-ufvk-expert/00003.png
Binary file added tests_zemu/snapshots/x-get-ufvk-expert/00004.png
Binary file added tests_zemu/snapshots/x-get-ufvk-expert/00005.png
Binary file added tests_zemu/snapshots/x-get-ufvk/00000.png
Binary file added tests_zemu/snapshots/x-get-ufvk/00001.png
Binary file added tests_zemu/snapshots/x-get-ufvk/00002.png
Binary file added tests_zemu/snapshots/x-get-ufvk/00003.png
94 changes: 94 additions & 0 deletions tests_zemu/tests/keys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,52 @@ describe('Get_keys', function () {
await sim.close()
}
})

test.concurrent.each(models)('Get ufvk', async function (m) {
const sim = new Zemu(m.path)
try {
await sim.start({
...defaultOptions(m, true),
approveKeyword: isTouchDevice(m.name) ? 'Approve' : '',
approveAction: ButtonKind.ApproveTapButton
})

const app = new ZCashApp(sim.getTransport())

const account = 1000
const ufvkreq = app.getUfvk(account)

await sim.waitUntilScreenIsNot(sim.getMainMenuSnapshot())
await sim.compareSnapshotsAndApprove('.', `${m.prefix.toLowerCase()}-get-ufvk`)

const ufvk = await ufvkreq

console.log(ufvk)

const expected_akRaw = '0bbb1d4bfe70a4f4fc762e2f980ab7c600a060c28410ccd03972931fe310f2a5'
const akRaw = ufvk.sapling.akRaw?.toString('hex')
expect(akRaw).toEqual(expected_akRaw)

const expected_nkRaw = '9f552de44e5c38db16de3165aaa4627e352e00b6863dd627cc58df02a39deec7'
const nkRaw = ufvk.sapling.nkRaw?.toString('hex')
expect(nkRaw).toEqual(expected_nkRaw)

const expected_ovkRaw = '199be731acfa8bf5d525eade16451edf6e818f27db0164ff1f428bd8bf432f69'
const ovkRaw = ufvk.sapling.ovkRaw?.toString('hex')
expect(ovkRaw).toEqual(expected_ovkRaw)

const expected_dkRaw = '702c87a33c31670b463df27f43ad02942f68ef9071d5547b0ead7b5ae425b079'
const dkRaw = ufvk.sapling.dkRaw?.toString('hex')
expect(dkRaw).toEqual(expected_dkRaw)

const expected_pkRaw = '025b92d7edb1d5acaa9e4eafd9c220d0d4ca2d63fcfd464ca95b9329d91d98cb5b'
const pkRaw = ufvk.transparent.pkRaw?.toString('hex')
expect(pkRaw).toEqual(expected_pkRaw)

} finally {
await sim.close()
}
})
})


Expand Down Expand Up @@ -443,6 +489,54 @@ describe('Get_keys', function () {
await sim.close()
}
})

test.concurrent.each(models)('Get ufvk', async function (m) {
const sim = new Zemu(m.path)
try {
await sim.start({
...defaultOptions(m, true),
approveKeyword: isTouchDevice(m.name) ? 'Approve' : '',
approveAction: ButtonKind.ApproveTapButton
})

await sim.toggleExpertMode()

const app = new ZCashApp(sim.getTransport())

const account = 1000
const ufvkreq = app.getUfvk(account)

await sim.waitUntilScreenIsNot(sim.getMainMenuSnapshot())
await sim.compareSnapshotsAndApprove('.', `${m.prefix.toLowerCase()}-get-ufvk-expert`)

const ufvk = await ufvkreq

console.log(ufvk)

const expected_akRaw = '0bbb1d4bfe70a4f4fc762e2f980ab7c600a060c28410ccd03972931fe310f2a5'
const akRaw = ufvk.sapling.akRaw?.toString('hex')
expect(akRaw).toEqual(expected_akRaw)

const expected_nkRaw = '9f552de44e5c38db16de3165aaa4627e352e00b6863dd627cc58df02a39deec7'
const nkRaw = ufvk.sapling.nkRaw?.toString('hex')
expect(nkRaw).toEqual(expected_nkRaw)

const expected_ovkRaw = '199be731acfa8bf5d525eade16451edf6e818f27db0164ff1f428bd8bf432f69'
const ovkRaw = ufvk.sapling.ovkRaw?.toString('hex')
expect(ovkRaw).toEqual(expected_ovkRaw)

const expected_dkRaw = '702c87a33c31670b463df27f43ad02942f68ef9071d5547b0ead7b5ae425b079'
const dkRaw = ufvk.sapling.dkRaw?.toString('hex')
expect(dkRaw).toEqual(expected_dkRaw)

const expected_pkRaw = '025b92d7edb1d5acaa9e4eafd9c220d0d4ca2d63fcfd464ca95b9329d91d98cb5b'
const pkRaw = ufvk.transparent.pkRaw?.toString('hex')
expect(pkRaw).toEqual(expected_pkRaw)

} finally {
await sim.close()
}
})
})

})
Expand Down

0 comments on commit 14b2c5e

Please sign in to comment.