@@ -16,7 +16,14 @@ import { HID } from 'node-hid';
16
16
import { Hash32ByteBase16 } from '@cardano-sdk/crypto' ;
17
17
import { HexBlob } from '@cardano-sdk/util' ;
18
18
import { InitializeTxProps , InitializeTxResult } from '@cardano-sdk/tx-construction' ;
19
+ import { LargeFirstSelector } from '@cardano-sdk/input-selection' ;
19
20
import { LedgerKeyAgent , LedgerTransportType } from '@cardano-sdk/hardware-ledger' ;
21
+ import {
22
+ MockChangeAddressResolver ,
23
+ getPayToPubKeyHashScript ,
24
+ getPaymentCredential ,
25
+ getStakeCredential
26
+ } from '../utils' ;
20
27
import { buildDRepAddressFromDRepKey } from '../../util' ;
21
28
import { firstValueFrom } from 'rxjs' ;
22
29
import { getDevices } from '@ledgerhq/hw-transport-node-hid-noevents' ;
@@ -40,14 +47,6 @@ const cleanupEstablishedConnections = async () => {
40
47
LedgerKeyAgent . deviceConnections = [ ] ;
41
48
} ;
42
49
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
-
51
50
const signAndDecode = async ( signWith : Cardano . PaymentAddress | Cardano . RewardAccount , wallet : BaseWallet ) => {
52
51
const dataSignature = await wallet . signData ( {
53
52
payload : HexBlob ( 'abc123' ) ,
@@ -357,6 +356,86 @@ describe('LedgerKeyAgent', () => {
357
356
expect ( signatures . size ) . toBe ( 3 ) ;
358
357
} ) ;
359
358
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
+
360
439
describe ( 'conway-era' , ( ) => {
361
440
describe ( 'ordinary tx mode' , ( ) => {
362
441
let dRepPublicKey : Crypto . Ed25519PublicKeyHex | undefined ;
0 commit comments