Skip to content

Commit af86428

Browse files
authored
Merge pull request #84 from mlabs-haskell/df/credential-improvements
Improve various aspects of credentials/hashes/assets
2 parents a415d01 + b1bff18 commit af86428

File tree

5 files changed

+66
-77
lines changed

5 files changed

+66
-77
lines changed

addons/@mlabs-haskell/godot-cardano/src/address/address.gd

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ static func build(
7272

7373
func payment_credential() -> Credential:
7474
return Credential.new(_address.payment_credential())
75-
75+
## If the address contains staking credentials, return them.
76+
## Otherwise, return [code]null[/code].
7677
func stake_credential() -> Credential:
77-
return Credential.new(_address.stake_credential())
78+
var _cred = _address.stake_credential()
79+
if _cred != null:
80+
return Credential.new(_cred)
81+
return null

addons/@mlabs-haskell/godot-cardano/src/address/credential.gd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ class_name Credential
66
var _credential: _Credential = null
77

88
enum CredentialType {
9-
PAYMENT = 0,
10-
STAKE = 1,
9+
KEY_HASH = 0,
10+
SCRIPT_HASH = 1,
1111
}
1212

1313
enum Status { SUCCESS = 0, INCORRECT_TYPE = 1 }

addons/@mlabs-haskell/godot-cardano/src/value/multi_asset.gd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ func get_asset_quantity(asset: AssetClass) -> BigInt:
5757
asset._asset_name._asset_name
5858
))
5959

60+
## Get a Dictionary mapping [class AssetName]s to [class BigInt]s under the
61+
## given [param policy_id].
62+
func get_tokens(policy_id: PolicyId) -> Dictionary:
63+
return _multi_asset._get_tokens(policy_id._policy_id)
64+
6065
## Set the [param quantity] of the given [param asset].
6166
func set_asset_quantity(asset: AssetClass, quantity: BigInt) -> SetAssetResult:
6267
return SetAssetResult.new(_multi_asset._set_asset_quantity(

addons/@mlabs-haskell/godot-cardano/src/value/policy_id.gd

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ class_name PolicyId
33

44
## An asset policy ID
55

6-
var _policy_id: _PolicyId
6+
var _policy_id: _ScriptHash
77

88
enum Status { SUCCESS = 0, COULD_NOT_DECODE_HEX = 1 }
99

1010
## WARNING: Do not use this constructor directly, use [method from_hex],
1111
## [method from_script] or [method from_script_source] for safe building.
12-
func _init(policy_id: _PolicyId):
12+
func _init(policy_id: _ScriptHash):
1313
_policy_id = policy_id
1414

1515
class FromHexResult extends Result:
@@ -23,16 +23,16 @@ class FromHexResult extends Result:
2323
## Try to parse a [PolicyId] from its minting policy's [param hash] encoded as
2424
## hex.
2525
static func from_hex(hash: String) -> FromHexResult:
26-
return FromHexResult.new(_PolicyId._from_hex(hash))
26+
return FromHexResult.new(_ScriptHash._from_hex(hash))
2727

2828
## Obtain a [PolicyId] from the minting policy's [param script].
2929
static func from_script(script: PlutusScript) -> PolicyId:
30-
var result = _PolicyId._from_hex(script.hash_as_hex())
30+
var result = _ScriptHash._from_hex(script.hash_as_hex())
3131
return new(result.unsafe_value())
3232

3333
## Obtain a [PolicyId] from the minting policy's [param script_source].
3434
static func from_script_source(script_source: PlutusScriptSource) -> PolicyId:
35-
var result = _PolicyId._from_hex(script_source.hash().to_hex())
35+
var result = _ScriptHash._from_hex(script_source.hash().to_hex())
3636
return new(result.unsafe_value())
3737

3838
## Get the hex encoding of the minting policy's hash

libcsl_godot/src/ledger/transaction.rs

Lines changed: 48 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -19,52 +19,7 @@ use godot::prelude::*;
1919
use crate::bigint::BigInt;
2020
use 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)]
369347
pub enum CredentialType {
370-
Payment,
371-
Stake,
348+
KeyHash,
349+
ScriptHash,
372350
}
373351

374352
impl 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

Comments
 (0)