use chia_protocol::Bytes32;
use chia_sha2::Sha256;
use clvmr::allocator::{Allocator, NodePtr};
pub fn compute_coin_id(
a: &Allocator,
parent_id: NodePtr,
puzzle_hash: NodePtr,
amount: &[u8],
) -> Bytes32 {
let mut hasher = Sha256::new();
hasher.update(a.atom(parent_id));
hasher.update(a.atom(puzzle_hash));
hasher.update(amount);
let coin_id: [u8; 32] = hasher.finalize();
coin_id.into()
}
#[test]
fn test_compute_coin_id() {
let mut a = Allocator::new();
let parent_id1 = a
.new_atom(&[
0x61, 0x62, 0x61, 0x62, 0x61, 0x62, 0x61, 0x62, 0x61, 0x62, 0x61, 0x62, 0x61, 0x62,
0x61, 0x62, 0x61, 0x62, 0x61, 0x62, 0x61, 0x62, 0x61, 0x62, 0x61, 0x62, 0x61, 0x62,
0x61, 0x62, 0x61, 0x62,
])
.unwrap();
let parent_id2 = a
.new_atom(&[
0x62, 0x61, 0x62, 0x61, 0x62, 0x61, 0x62, 0x61, 0x62, 0x61, 0x62, 0x61, 0x62, 0x61,
0x62, 0x61, 0x62, 0x61, 0x62, 0x61, 0x62, 0x61, 0x62, 0x61, 0x62, 0x61, 0x62, 0x61,
0x62, 0x61, 0x62, 0x61,
])
.unwrap();
let puzzle_hash1 = a
.new_atom(&[
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31,
])
.unwrap();
let coin_id = &[
0xd8, 0x2e, 0xd7, 0x4b, 0x94, 0x5e, 0x6a, 0x14, 0x0f, 0xfe, 0xcd, 0xa9, 0xa6, 0x19, 0xc3,
0x0c, 0x32, 0x3c, 0xdf, 0x20, 0x53, 0xa5, 0x8d, 0xae, 0x89, 0x22, 0xc0, 0xc1, 0x5a, 0x87,
0x64, 0x6e,
];
assert_eq!(
compute_coin_id(&a, parent_id1, puzzle_hash1, &[123]).as_ref(),
coin_id
);
let coin_id = &[
0xb9, 0xca, 0xc8, 0xf1, 0xb1, 0x5b, 0xce, 0x73, 0xad, 0x14, 0xf3, 0x94, 0x51, 0xda, 0xc4,
0x6f, 0x73, 0x49, 0x4e, 0x70, 0xf2, 0x3d, 0xf2, 0xd8, 0xfd, 0xad, 0xdf, 0x26, 0xcf, 0xd8,
0x34, 0x68,
];
assert_eq!(
compute_coin_id(&a, parent_id1, puzzle_hash1, &[3]).as_ref(),
coin_id
);
let coin_id = &[
0x0b, 0x85, 0x37, 0x7e, 0x9d, 0xa2, 0x40, 0x41, 0x56, 0x0e, 0xe2, 0xe1, 0xdb, 0x76, 0xbf,
0xa8, 0x6a, 0xfd, 0xb0, 0x48, 0x6b, 0x6b, 0xed, 0x98, 0x42, 0x8e, 0x2b, 0x35, 0x53, 0x6f,
0xdf, 0x97,
];
assert_eq!(
compute_coin_id(&a, parent_id2, puzzle_hash1, &[3]).as_ref(),
coin_id
);
}