1
1
use clvmr:: allocator:: NodePtr ;
2
2
use clvm_traits:: ToClvm ;
3
3
4
+ use clvm_tools_rs:: classic:: clvm_tools:: binutils:: disassemble;
5
+
4
6
use pyo3:: prelude:: * ;
5
7
use pyo3:: types:: { PyNone , PyBytes , PyTuple } ;
6
8
use rand:: prelude:: * ;
@@ -9,10 +11,9 @@ use rand_chacha::ChaCha8Rng;
9
11
10
12
use indoc:: indoc;
11
13
12
- use crate :: common:: constants:: CREATE_COIN ;
13
- use crate :: common:: standard_coin:: standard_solution;
14
- use crate :: common:: types:: { ErrToError , Error , Puzzle , Amount , Hash , CoinString , CoinID , PuzzleHash , PrivateKey , Aggsig , Node , SpecificTransactionBundle , AllocEncoder , TransactionBundle } ;
15
- use crate :: common:: standard_coin:: ChiaIdentity ;
14
+ use crate :: common:: constants:: { AGG_SIG_ME_ADDITIONAL_DATA , CREATE_COIN , DEFAULT_HIDDEN_PUZZLE_HASH } ;
15
+ use crate :: common:: standard_coin:: { sign_agg_sig_me, standard_solution_partial, ChiaIdentity , calculate_synthetic_secret_key, private_to_public_key, calculate_synthetic_public_key, agg_sig_me_message} ;
16
+ use crate :: common:: types:: { ErrToError , Error , Puzzle , Amount , Hash , CoinString , CoinID , PuzzleHash , PrivateKey , Aggsig , Node , SpecificTransactionBundle , AllocEncoder , TransactionBundle , ToQuotedProgram , Sha256tree } ;
16
17
17
18
// Allow simulator from rust.
18
19
struct Simulator {
@@ -127,6 +128,8 @@ impl Simulator {
127
128
let mut result_coins = Vec :: new ( ) ;
128
129
for i in items. iter ( ) {
129
130
let coin_of_item: PyObject = i. getattr ( py, "coin" ) ?. extract ( py) ?;
131
+ let as_list_str: String = coin_of_item. call_method0 ( py, "__repr__" ) ?. extract ( py) ?;
132
+ eprintln ! ( "as_list_str {as_list_str}" ) ;
130
133
let as_list: Vec < PyObject > = self . coin_as_list . call1 ( py, ( coin_of_item, ) ) ?. extract ( py) ?;
131
134
let parent_coin_info: & PyBytes = as_list[ 0 ] . downcast ( py) ?;
132
135
let parent_coin_info_slice: & [ u8 ] = parent_coin_info. extract ( ) ?;
@@ -231,6 +234,8 @@ impl Simulator {
231
234
let ( inclusion_status, err) : ( PyObject , PyObject ) = spend_res. extract ( py) ?;
232
235
eprintln ! ( "inclusion_status {inclusion_status}" ) ;
233
236
let status: u32 = inclusion_status. extract ( py) ?;
237
+ let e: String = err. call_method0 ( py, "__repr__" ) ?. extract ( py) ?;
238
+ eprintln ! ( "err {e:?}" ) ;
234
239
Ok ( status)
235
240
} )
236
241
}
@@ -241,13 +246,16 @@ fn test_sim() {
241
246
let seed: [ u8 ; 32 ] = [ 0 ; 32 ] ;
242
247
let mut rng = ChaCha8Rng :: from_seed ( seed) ;
243
248
let mut allocator = AllocEncoder :: new ( ) ;
249
+ let agg_sig_me_additional_data = Hash :: from_slice ( & AGG_SIG_ME_ADDITIONAL_DATA ) ;
244
250
let s = Simulator :: new ( ) ;
245
251
let private_key: PrivateKey = rng. gen ( ) ;
246
252
let identity = ChiaIdentity :: new ( & mut allocator, private_key. clone ( ) ) . expect ( "should create" ) ;
253
+ eprintln ! ( "identity public key {:?}" , identity. public_key) ;
247
254
s. farm_block ( & identity. puzzle_hash ) ;
248
255
249
256
let coins = s. get_my_coins ( & identity. puzzle_hash ) . expect ( "got coins" ) ;
250
- eprintln ! ( "coins {coins:?}" ) ;
257
+ eprintln ! ( "coin 0 {:?}" , coins[ 0 ] . to_parts( ) ) ;
258
+ eprintln ! ( "coin 0 id {:?}" , coins[ 0 ] . to_coin_id( ) ) ;
251
259
252
260
let ( first_coin_parent, first_coin_ph, first_coin_amt) = coins[ 0 ] . to_parts ( ) . unwrap ( ) ;
253
261
assert_eq ! ( first_coin_ph, identity. puzzle_hash) ;
@@ -258,38 +266,56 @@ fn test_sim() {
258
266
( Amount :: new ( 1 ) , ( ) )
259
267
)
260
268
) ,
261
- ( CREATE_COIN ,
262
- ( identity. puzzle_hash . clone ( ) ,
263
- ( first_coin_amt - Amount :: new ( 1 ) , ( ) )
264
- )
265
- )
269
+ // (CREATE_COIN,
270
+ // (identity.puzzle_hash.clone(),
271
+ // (first_coin_amt - Amount::new(1), ())
272
+ // )
273
+ // )
266
274
] . to_clvm ( & mut allocator) . expect ( "should create conditions" ) ;
267
275
268
- let ( solution, signature) = standard_solution (
276
+ let default_hidden_puzzle_hash = Hash :: from_bytes ( DEFAULT_HIDDEN_PUZZLE_HASH . clone ( ) ) ;
277
+ let synthetic_secret_key = calculate_synthetic_secret_key (
278
+ & identity. private_key ,
279
+ & default_hidden_puzzle_hash
280
+ ) . expect ( "should be ok" ) ;
281
+ let synthetic_public_key = private_to_public_key ( & synthetic_secret_key) ;
282
+ assert_eq ! (
283
+ synthetic_public_key,
284
+ calculate_synthetic_public_key(
285
+ & identity. public_key,
286
+ & PuzzleHash :: from_hash( default_hidden_puzzle_hash. clone( ) )
287
+ ) . expect( "should be ok" )
288
+ ) ;
289
+ let ( solution, signature1) = standard_solution_partial (
269
290
& mut allocator,
270
- & private_key,
271
- conditions
291
+ & synthetic_secret_key,
292
+ & coins[ 0 ] . to_coin_id ( ) ,
293
+ conditions,
294
+ & synthetic_public_key,
295
+ & agg_sig_me_additional_data,
296
+ false
272
297
) . expect ( "should build" ) ;
273
298
274
- let spend =
275
- Python :: with_gil ( |py| {
276
- s . make_coin_spend (
277
- py ,
278
- & mut allocator ,
279
- & CoinString :: from_parts ( & CoinID :: default ( ) , & PuzzleHash :: default ( ) , & Amount :: default ( ) ) ,
280
- identity . puzzle . clone ( ) ,
281
- solution
282
- )
283
- } ) . expect ( "should get a spend" ) ;
299
+ let quoted_conds = conditions . to_quoted_program ( & mut allocator ) . expect ( "should work" ) ;
300
+ let hashed_conds = quoted_conds . sha256tree ( & mut allocator ) ;
301
+ let agg_sig_me_message = agg_sig_me_message (
302
+ & hashed_conds . bytes ( ) ,
303
+ & coins [ 0 ] . to_coin_id ( ) ,
304
+ & agg_sig_me_additional_data
305
+ ) ;
306
+ eprintln ! ( "our message {agg_sig_me_message:?}" ) ;
307
+ let signature2 = synthetic_secret_key . sign ( & agg_sig_me_message ) ;
308
+ assert_eq ! ( signature1 , signature2 ) ;
284
309
285
- eprintln ! ( "spend {spend:?}" ) ;
310
+ eprintln ! ( "our cond hash {hashed_conds:?}" ) ;
311
+ eprintln ! ( "our signature {signature1:?}" ) ;
286
312
287
313
let specific = SpecificTransactionBundle {
288
314
coin : coins[ 0 ] . clone ( ) ,
289
315
bundle : TransactionBundle {
290
316
puzzle : identity. puzzle . clone ( ) ,
291
317
solution,
292
- signature,
318
+ signature : signature1 ,
293
319
}
294
320
} ;
295
321
0 commit comments