1+ import { getSdk } from "@namada/sdk/web" ;
2+ import sdkInit from "@namada/sdk/web-init" ;
13import clsx from "clsx" ;
24import { ReactNode , useCallback , useEffect , useState } from "react" ;
35
@@ -84,8 +86,9 @@ export const ConfirmSignLedgerTx: React.FC<Props> = ({ details }) => {
8486 const signMaspTx = async (
8587 ledger : Ledger ,
8688 bytes : Uint8Array ,
87- path : string
88- ) : Promise < { sbar : Uint8Array ; rbar : Uint8Array } > => {
89+ path : string ,
90+ shieldedHash ?: string
91+ ) : Promise < { sbar : Uint8Array ; rbar : Uint8Array } [ ] > => {
8992 const signMaspSpendsResponse = await ledger . namadaApp . signMaspSpends (
9093 path ,
9194 Buffer . from ( bytes )
@@ -97,14 +100,36 @@ export const ConfirmSignLedgerTx: React.FC<Props> = ({ details }) => {
97100 ) ;
98101 }
99102
100- const spendSignatureResponse = await ledger . namadaApp . getSpendSignature ( ) ;
101- if ( spendSignatureResponse . returnCode !== LedgerError . NoErrors ) {
102- throw new Error (
103- `Getting spends signature error encountered: ${ signMaspSpendsResponse . errorMessage } `
104- ) ;
103+ if ( ! shieldedHash ) {
104+ throw new Error ( "Shielded hash is required for MASP transactions" ) ;
105105 }
106106
107- return spendSignatureResponse ;
107+ const { cryptoMemory } = await sdkInit ( ) ;
108+ // TODO: Find a better way to init the sdk, token has to be any valid token
109+ const sdk = getSdk (
110+ cryptoMemory ,
111+ "" ,
112+ "" ,
113+ "" ,
114+ "tnam1q9gr66cvu4hrzm0sd5kmlnjje82gs3xlfg3v6nu7"
115+ ) ;
116+ const descriptors = await sdk
117+ . getMasp ( )
118+ . getDescriptorMap ( bytes , fromBase64 ( shieldedHash ) ) ;
119+
120+ const responses = [ ] ;
121+ // TODO: this probably means that we do not really need descriptor map, but just to iterate over the sapling inputs
122+ for ( const _ of descriptors ) {
123+ const spendSignatureResponse = await ledger . namadaApp . getSpendSignature ( ) ;
124+ if ( spendSignatureResponse . returnCode !== LedgerError . NoErrors ) {
125+ throw new Error (
126+ `Getting spends signature error encountered: ${ signMaspSpendsResponse . errorMessage } `
127+ ) ;
128+ }
129+ responses . push ( spendSignatureResponse ) ;
130+ }
131+
132+ return responses ;
108133 } ;
109134
110135 const signLedgerTx = async (
@@ -130,15 +155,20 @@ export const ConfirmSignLedgerTx: React.FC<Props> = ({ details }) => {
130155 ledger : Ledger ,
131156 tx : string ,
132157 zip32Path : string ,
133- signatures : string [ ]
158+ signatures : string [ ] ,
159+ shieldedHash ?: string
134160 ) => {
135- const { sbar , rbar } = await signMaspTx (
161+ const responses = await signMaspTx (
136162 ledger ,
137163 fromBase64 ( tx ) ,
138- zip32Path
164+ zip32Path ,
165+ shieldedHash
139166 ) ;
140- const signature = toBase64 ( new Uint8Array ( [ ...rbar , ...sbar ] ) ) ;
141- signatures . push ( signature ) ;
167+
168+ responses . forEach ( ( { sbar, rbar } ) => {
169+ const signature = toBase64 ( new Uint8Array ( [ ...rbar , ...sbar ] ) ) ;
170+ signatures . push ( signature ) ;
171+ } ) ;
142172 } ,
143173 [ ]
144174 ) ;
@@ -197,10 +227,12 @@ export const ConfirmSignLedgerTx: React.FC<Props> = ({ details }) => {
197227 if ( ! accountDetails ) {
198228 throw new Error ( `Failed to query account details for ${ signer } ` ) ;
199229 }
230+ const [ transparentAccount , shieldedAccount ] = accountDetails ;
231+
200232 const path = {
201- account : accountDetails . path . account ,
202- change : accountDetails . path . change || 0 ,
203- index : accountDetails . path . index || 0 ,
233+ account : transparentAccount . path . account ,
234+ change : transparentAccount . path . change || 0 ,
235+ index : transparentAccount . path . index || 0 ,
204236 } ;
205237
206238 const pendingTxs = await requester . sendMessage (
@@ -247,7 +279,7 @@ export const ConfirmSignLedgerTx: React.FC<Props> = ({ details }) => {
247279 transferTypes . includes ( "Unshielding" ) ||
248280 transferTypes . includes ( "IbcUnshieldTransfer" ) ;
249281
250- for await ( const tx of pendingTxs ) {
282+ for await ( const { tx , shieldedHash } of pendingTxs ) {
251283 if ( txCount > 1 ) {
252284 setStepTwoDescription (
253285 < p >
@@ -259,11 +291,22 @@ export const ConfirmSignLedgerTx: React.FC<Props> = ({ details }) => {
259291 }
260292
261293 if ( fromMasp ) {
294+ if ( ! shieldedAccount ) {
295+ throw new Error (
296+ `Shielded account details for ${ signer } not found!`
297+ ) ;
298+ }
262299 const zip32Path = makeSaplingPath ( chains . namada . bip44 . coinType , {
263- account : path . account ,
300+ account : shieldedAccount . path . account ,
264301 } ) ;
265302 // Adds new signature to the collection
266- await handleMaspSignTx ( ledger , tx , zip32Path , maspSignatures ) ;
303+ await handleMaspSignTx (
304+ ledger ,
305+ tx ,
306+ zip32Path ,
307+ maspSignatures ,
308+ shieldedHash
309+ ) ;
267310 } else {
268311 const bip44Path = makeBip44Path ( chains . namada . bip44 . coinType , path ) ;
269312 // Adds new signature to the collection
0 commit comments