@@ -16,7 +16,14 @@ import { HID } from 'node-hid';
1616import { Hash32ByteBase16 } from '@cardano-sdk/crypto' ;
1717import { HexBlob } from '@cardano-sdk/util' ;
1818import { InitializeTxProps , InitializeTxResult } from '@cardano-sdk/tx-construction' ;
19+ import { LargeFirstSelector } from '@cardano-sdk/input-selection' ;
1920import { LedgerKeyAgent , LedgerTransportType } from '@cardano-sdk/hardware-ledger' ;
21+ import {
22+ MockChangeAddressResolver ,
23+ getPayToPubKeyHashScript ,
24+ getPaymentCredential ,
25+ getStakeCredential
26+ } from '../utils' ;
2027import { buildDRepAddressFromDRepKey } from '../../util' ;
2128import { firstValueFrom } from 'rxjs' ;
2229import { getDevices } from '@ledgerhq/hw-transport-node-hid-noevents' ;
@@ -40,14 +47,6 @@ const cleanupEstablishedConnections = async () => {
4047 LedgerKeyAgent . deviceConnections = [ ] ;
4148} ;
4249
43- const getStakeCredential = ( rewardAccount : Cardano . RewardAccount ) => {
44- const stakeKeyHash = Cardano . RewardAccount . toHash ( rewardAccount ) ;
45- return {
46- hash : stakeKeyHash ,
47- type : Cardano . CredentialType . KeyHash
48- } ;
49- } ;
50-
5150const signAndDecode = async ( signWith : Cardano . PaymentAddress | Cardano . RewardAccount , wallet : BaseWallet ) => {
5251 const dataSignature = await wallet . signData ( {
5352 payload : HexBlob ( 'abc123' ) ,
@@ -357,6 +356,86 @@ describe('LedgerKeyAgent', () => {
357356 expect ( signatures . size ) . toBe ( 3 ) ;
358357 } ) ;
359358
359+ describe ( 'Native Scripts' , ( ) => {
360+ it ( 'can sign transaction with native script - Payment credential' , async ( ) => {
361+ const selector = new LargeFirstSelector ( {
362+ changeAddressResolver : new MockChangeAddressResolver ( )
363+ } ) ;
364+ wallet . setInputSelector ( selector ) ;
365+ const txBuilder = wallet . createTxBuilder ( ) ;
366+ const firstAddress = ( await firstValueFrom ( wallet . addresses$ ) ) [ 0 ] . address ;
367+
368+ const builtTx = await txBuilder
369+ . addInput (
370+ [
371+ {
372+ address : Cardano . PaymentAddress ( 'addr_test1vzztre5epvtj5p72sh28nvrs3e6s4xxn95f66cvg0sqsk7qd3mah0' ) ,
373+ index : 0 ,
374+ txId : Cardano . TransactionId ( '0f3abbc8fc19c2e61bab6059bf8a466e6e754833a08a62a6c56fe0e78f19d9d5' )
375+ } ,
376+ outputs [ 0 ]
377+ ] ,
378+ {
379+ script : getPayToPubKeyHashScript (
380+ getPaymentCredential ( firstAddress ) . hash as unknown as Crypto . Ed25519KeyHashHex
381+ )
382+ }
383+ )
384+ . addOutput ( txBuilder . buildOutput ( ) . address ( outputs [ 0 ] . address ) . coin ( BigInt ( 5_111_111n ) ) . toTxOut ( ) )
385+ . customize ( ( { txBody } ) => ( {
386+ ...txBody ,
387+ withdrawals : [ ]
388+ } ) )
389+ . build ( )
390+ . inspect ( ) ;
391+
392+ const {
393+ witness : { signatures }
394+ } = await wallet . finalizeTx ( { tx : builtTx , witness : builtTx . witness } ) ;
395+
396+ expect ( signatures . size ) . toBe ( 1 ) ;
397+ } ) ;
398+
399+ it ( 'can sign transaction with native script - Stake credential' , async ( ) => {
400+ const selector = new LargeFirstSelector ( {
401+ changeAddressResolver : new MockChangeAddressResolver ( )
402+ } ) ;
403+ wallet . setInputSelector ( selector ) ;
404+ const txBuilder = wallet . createTxBuilder ( ) ;
405+
406+ const builtTx = await txBuilder
407+ . addInput (
408+ [
409+ {
410+ address : Cardano . PaymentAddress ( 'addr_test1vzztre5epvtj5p72sh28nvrs3e6s4xxn95f66cvg0sqsk7qd3mah0' ) ,
411+ index : 0 ,
412+ txId : Cardano . TransactionId ( '0f3abbc8fc19c2e61bab6059bf8a466e6e754833a08a62a6c56fe0e78f19d9d5' )
413+ } ,
414+ outputs [ 0 ]
415+ ] ,
416+ {
417+ script : getPayToPubKeyHashScript (
418+ getStakeCredential ( ( await firstValueFrom ( wallet . delegation . rewardAccounts$ ) ) ?. [ 0 ] . address )
419+ . hash as unknown as Crypto . Ed25519KeyHashHex
420+ )
421+ }
422+ )
423+ . addOutput ( txBuilder . buildOutput ( ) . address ( outputs [ 0 ] . address ) . coin ( BigInt ( 5_111_111n ) ) . toTxOut ( ) )
424+ . customize ( ( { txBody } ) => ( {
425+ ...txBody ,
426+ withdrawals : [ ]
427+ } ) )
428+ . build ( )
429+ . inspect ( ) ;
430+
431+ const {
432+ witness : { signatures }
433+ } = await wallet . finalizeTx ( { tx : builtTx , witness : builtTx . witness } ) ;
434+
435+ expect ( signatures . size ) . toBe ( 1 ) ;
436+ } ) ;
437+ } ) ;
438+
360439 describe ( 'conway-era' , ( ) => {
361440 describe ( 'ordinary tx mode' , ( ) => {
362441 let dRepPublicKey : Crypto . Ed25519PublicKeyHex | undefined ;
0 commit comments