5
5
ElectrumNetworkProvider ,
6
6
Network ,
7
7
TransactionBuilder ,
8
+ SignatureAlgorithm ,
9
+ HashType ,
8
10
} from '../../src/index.js' ;
9
11
import {
10
12
alicePriv ,
@@ -16,6 +18,7 @@ import { gatherUtxos, getTxOutputs } from '../test-util.js';
16
18
import { FailedRequireError } from '../../src/Errors.js' ;
17
19
import artifact from '../fixture/hodl_vault.artifact.js' ;
18
20
import { randomUtxo } from '../../src/utils.js' ;
21
+ import { placeholder } from '@cashscript/utils' ;
19
22
20
23
describe ( 'HodlVault' , ( ) => {
21
24
const provider = process . env . TESTS_USE_MOCKNET
@@ -94,5 +97,52 @@ describe('HodlVault', () => {
94
97
const txOutputs = getTxOutputs ( tx ) ;
95
98
expect ( txOutputs ) . toEqual ( expect . arrayContaining ( [ { to, amount } ] ) ) ;
96
99
} ) ;
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
+ } ) ;
97
147
} ) ;
98
148
} ) ;
0 commit comments