Skip to content

Commit

Permalink
feat: add support to unified full view keys method
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuelm41 committed Nov 8, 2024
1 parent 09632db commit ac77614
Show file tree
Hide file tree
Showing 4 changed files with 105 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
48 changes: 48 additions & 0 deletions tests_zemu/tests/keys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,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 ac77614

Please sign in to comment.