@@ -19,52 +19,7 @@ use godot::prelude::*;
1919use crate :: bigint:: BigInt ;
2020use crate :: gresult:: { FailsWith , GResult } ;
2121
22- #[ derive( GodotClass ) ]
23- #[ class( base=RefCounted , rename=_PolicyId) ]
24- pub struct PolicyId {
25- pub policy_id : CSL :: PolicyID ,
26- }
27-
28- #[ derive( Debug ) ]
29- pub enum PolicyIdError {
30- CouldNotDecodeHex ( String ) ,
31- }
32-
33- impl GodotConvert for PolicyIdError {
34- type Via = i64 ;
35- }
36-
37- impl ToGodot for PolicyIdError {
38- fn to_godot ( & self ) -> Self :: Via {
39- use PolicyIdError :: * ;
40- match self {
41- CouldNotDecodeHex ( _) => 1 ,
42- }
43- }
44- }
45-
46- impl FailsWith for PolicyId {
47- type E = PolicyIdError ;
48- }
49-
50- #[ godot_api]
51- impl PolicyId {
52- fn from_hex ( policy_id : GString ) -> Result < PolicyId , PolicyIdError > {
53- CSL :: crypto:: ScriptHash :: from_hex ( & policy_id. to_string ( ) )
54- . map_err ( |_| PolicyIdError :: CouldNotDecodeHex ( policy_id. to_string ( ) ) )
55- . map ( |policy_id| Self { policy_id } )
56- }
57-
58- #[ func]
59- fn _from_hex ( policy_id : GString ) -> Gd < GResult > {
60- Self :: to_gresult_class ( Self :: from_hex ( policy_id) )
61- }
62-
63- #[ func]
64- fn to_hex ( & self ) -> GString {
65- self . policy_id . to_hex ( ) . into_godot ( )
66- }
67- }
22+ pub type PolicyId = ScriptHash ;
6823
6924#[ derive( GodotClass ) ]
7025#[ class( base=RefCounted , rename=_AssetName) ]
@@ -266,7 +221,7 @@ impl MultiAsset {
266221 quantity : Gd < BigInt > ,
267222 ) -> Result < ( ) , MultiAssetError > {
268223 self . assets . set_asset (
269- & policy_id. bind ( ) . policy_id ,
224+ & policy_id. bind ( ) . hash ,
270225 & asset_name. bind ( ) . asset_name ,
271226 BigNum :: from_str ( & quantity. bind ( ) . to_str ( ) ) ?,
272227 ) ;
@@ -292,12 +247,35 @@ impl MultiAsset {
292247 Gd :: from_object (
293248 BigInt :: from_str (
294249 self . assets
295- . get_asset ( & policy_id. bind ( ) . policy_id , & asset_name. bind ( ) . asset_name )
250+ . get_asset ( & policy_id. bind ( ) . hash , & asset_name. bind ( ) . asset_name )
296251 . to_str ( ) ,
297252 )
298253 . expect ( "Failed to convert asset quantity" ) ,
299254 )
300255 }
256+
257+ #[ func]
258+ pub fn _get_tokens ( & self , policy_id : Gd < PolicyId > ) -> Dictionary {
259+ let tokens = self . assets . get ( & policy_id. bind ( ) . hash ) ;
260+ let mut dict = Dictionary :: new ( ) ;
261+ match tokens {
262+ Some ( tokens_) => {
263+ let asset_names = tokens_. keys ( ) ;
264+ for i in 0 ..asset_names. len ( ) {
265+ let asset_name = asset_names. get ( i) ;
266+ let quantity = match tokens_. get ( & asset_name) {
267+ Some ( quantity) => {
268+ Gd :: from_object ( BigInt :: from_str ( quantity. to_str ( ) ) . unwrap ( ) )
269+ }
270+ None => BigInt :: zero ( ) ,
271+ } ;
272+ dict. insert ( asset_name. to_hex ( ) , quantity) ;
273+ }
274+ }
275+ None => { }
276+ }
277+ dict
278+ }
301279}
302280
303281#[ derive( GodotClass ) ]
@@ -367,8 +345,8 @@ pub struct Credential {
367345
368346#[ derive( Debug ) ]
369347pub enum CredentialType {
370- Payment ,
371- Stake ,
348+ KeyHash ,
349+ ScriptHash ,
372350}
373351
374352impl GodotConvert for CredentialType {
@@ -379,8 +357,8 @@ impl ToGodot for CredentialType {
379357 fn to_godot ( & self ) -> Self :: Via {
380358 use CredentialType :: * ;
381359 match self {
382- Payment => 0 ,
383- Stake => 1 ,
360+ KeyHash => 0 ,
361+ ScriptHash => 1 ,
384362 }
385363 }
386364}
@@ -389,8 +367,8 @@ impl FromGodot for CredentialType {
389367 fn try_from_godot ( v : Self :: Via ) -> Result < Self , ConvertError > {
390368 use CredentialType :: * ;
391369 match v {
392- 0 => Ok ( Payment ) ,
393- 1 => Ok ( Stake ) ,
370+ 0 => Ok ( KeyHash ) ,
371+ 1 => Ok ( ScriptHash ) ,
394372 _ => Err ( ConvertError :: new ( ) ) ,
395373 }
396374 }
@@ -436,7 +414,10 @@ impl Credential {
436414
437415 #[ func]
438416 fn get_type ( & self ) -> CredentialType {
439- CredentialType :: Payment
417+ match self . credential . kind ( ) {
418+ CSL :: address:: StakeCredKind :: Key => CredentialType :: KeyHash ,
419+ CSL :: address:: StakeCredKind :: Script => CredentialType :: ScriptHash ,
420+ }
440421 }
441422
442423 #[ func]
@@ -461,23 +442,22 @@ impl Credential {
461442
462443 #[ func]
463444 fn to_bytes ( & self ) -> PackedByteArray {
464- let bytes = self
465- . credential
466- . to_keyhash ( )
467- . ok_or_else ( || self . credential . to_scripthash ( ) . map ( |x| x . to_bytes ( ) ) )
468- . map ( |x| x . to_bytes ( ) )
469- . unwrap ( ) ;
470- PackedByteArray :: from ( bytes . as_slice ( ) )
445+ PackedByteArray :: from (
446+ match self . get_type ( ) {
447+ CredentialType :: KeyHash => self . credential . to_keyhash ( ) . unwrap ( ) . to_bytes ( ) ,
448+ CredentialType :: ScriptHash => self . credential . to_scripthash ( ) . unwrap ( ) . to_bytes ( ) ,
449+ }
450+ . as_slice ( ) ,
451+ )
471452 }
472453
473454 #[ func]
474455 fn to_hex ( & self ) -> GString {
475- self . credential
476- . to_keyhash ( )
477- . ok_or_else ( || self . credential . to_scripthash ( ) . map ( |x| x. to_hex ( ) ) )
478- . map ( |x| x. to_hex ( ) )
479- . unwrap ( )
480- . into_godot ( )
456+ match self . get_type ( ) {
457+ CredentialType :: KeyHash => self . credential . to_keyhash ( ) . unwrap ( ) . to_hex ( ) ,
458+ CredentialType :: ScriptHash => self . credential . to_scripthash ( ) . unwrap ( ) . to_hex ( ) ,
459+ }
460+ . to_godot ( )
481461 }
482462}
483463
0 commit comments