55 ElectrumNetworkProvider ,
66 Network ,
77 TransactionBuilder ,
8+ SignatureAlgorithm ,
9+ HashType ,
810} from '../../src/index.js' ;
911import {
1012 alicePriv ,
@@ -16,6 +18,7 @@ import { gatherUtxos, getTxOutputs } from '../test-util.js';
1618import { FailedRequireError } from '../../src/Errors.js' ;
1719import artifact from '../fixture/hodl_vault.artifact.js' ;
1820import { randomUtxo } from '../../src/utils.js' ;
21+ import { placeholder } from '@cashscript/utils' ;
1922
2023describe ( 'HodlVault' , ( ) => {
2124 const provider = process . env . TESTS_USE_MOCKNET
@@ -94,5 +97,52 @@ describe('HodlVault', () => {
9497 const txOutputs = getTxOutputs ( tx ) ;
9598 expect ( txOutputs ) . toEqual ( expect . arrayContaining ( [ { to, amount } ] ) ) ;
9699 } ) ;
100+
101+ it ( 'should succeed when price is high enough, ECDSA sig and datasig' , async ( ) => {
102+ // given
103+ const message = oracle . createMessage ( 100000n , 30000n ) ;
104+ const oracleSig = oracle . signMessage ( message , SignatureAlgorithm . ECDSA ) ;
105+ const to = hodlVault . address ;
106+ const amount = 10000n ;
107+ const { utxos, changeAmount } = gatherUtxos ( await hodlVault . getUtxos ( ) , { amount, fee : 2000n } ) ;
108+
109+ const signatureTemplate = new SignatureTemplate ( alicePriv , HashType . SIGHASH_ALL , SignatureAlgorithm . ECDSA ) ;
110+
111+ // when
112+ const tx = await new TransactionBuilder ( { provider } )
113+ . addInputs ( utxos , hodlVault . unlock . spend ( signatureTemplate , oracleSig , message ) )
114+ . addOutput ( { to : to , amount : amount } )
115+ . addOutput ( { to : to , amount : changeAmount } )
116+ . setLocktime ( 100_000 )
117+ . send ( ) ;
118+
119+ // then
120+ const txOutputs = getTxOutputs ( tx ) ;
121+ expect ( txOutputs ) . toEqual ( expect . arrayContaining ( [ { to, amount } ] ) ) ;
122+ } ) ;
123+
124+ it ( 'should fail to accept wrong signature lengths' , async ( ) => {
125+ // given
126+ const message = oracle . createMessage ( 100000n , 30000n ) ;
127+ const oracleSig = oracle . signMessage ( message , SignatureAlgorithm . ECDSA ) ;
128+ const to = hodlVault . address ;
129+ const amount = 10000n ;
130+ const { utxos, changeAmount } = gatherUtxos ( await hodlVault . getUtxos ( ) , { amount, fee : 2000n } ) ;
131+
132+ expect ( ( ) => new TransactionBuilder ( { provider } )
133+ . addInputs ( utxos , hodlVault . unlock . spend ( placeholder ( 100 ) , oracleSig , message ) )
134+ . addOutput ( { to : to , amount : amount } )
135+ . addOutput ( { to : to , amount : changeAmount } )
136+ . setLocktime ( 100_000 )
137+ . send ( ) ) . toThrow ( "Found type 'bytes100' where type 'sig' was expected" ) ;
138+
139+ const signatureTemplate = new SignatureTemplate ( alicePriv , HashType . SIGHASH_ALL , SignatureAlgorithm . ECDSA ) ;
140+ expect ( ( ) => new TransactionBuilder ( { provider } )
141+ . addInputs ( utxos , hodlVault . unlock . spend ( signatureTemplate , placeholder ( 100 ) , message ) )
142+ . addOutput ( { to : to , amount : amount } )
143+ . addOutput ( { to : to , amount : changeAmount } )
144+ . setLocktime ( 100_000 )
145+ . send ( ) ) . toThrow ( "Found type 'bytes100' where type 'datasig' was expected" ) ;
146+ } ) ;
97147 } ) ;
98148} ) ;
0 commit comments