Skip to content

Commit

Permalink
Merge pull request #1 from logicalmechanism/adding-addresses
Browse files Browse the repository at this point in the history
Adding in the address functions
  • Loading branch information
logicalmechanism authored May 16, 2023
2 parents 9ce2e35 + a84ff53 commit 3832c04
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions lib/assist/addresses.ak
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//// This module includes code to help with creating wallet and script addresses.
////
//// Empty keys are assumed to be on purpose.

use aiken/bytearray
use aiken/hash.{Blake2b_224, Hash}
use aiken/transaction/credential.{
Address, Inline, Script, ScriptCredential, VerificationKey,
VerificationKeyCredential,
}

/// Creates a enterprise or base address from the public key hash and stake
/// credential. An empty sc means enterpise address by default.
///
/// ```aiken
/// create_address(#"acab", #"")
/// create_address(datum.wallet.pkh, datum.wallet.sc)
/// ```
pub fn create_address(
pkh: Hash<Blake2b_224, VerificationKey>,
sc: Hash<Blake2b_224, VerificationKey>,
) -> Address {
// empty bytearrays means dont add the sc to the pkh
if bytearray.is_empty(sc) {
credential.from_verification_key(pkh)
} else {
credential.from_verification_key(pkh)
|> credential.with_delegation_key(sc)
}
}

test enterprise_wallet() {
let addr =
Address {
payment_credential: VerificationKeyCredential(#"acab"),
stake_credential: None,
}
create_address(#"acab", #"") == addr
}

test base_wallet() {
let addr =
Address {
payment_credential: VerificationKeyCredential(#"acab"),
stake_credential: Some(Inline(VerificationKeyCredential(#"face"))),
}
create_address(#"acab", #"face") == addr
}

/// Creates an address for a smart contract. The type does not mix address
/// types. Staked smart contracts are contracts as well. An empty sc is
/// assumed to be not staked.
///
/// ```aiken
/// create_script_address(#"acab", #"")
/// create_script_address(ref_datum.sale_script.pkh, ref_datum.sale_script.sc)
/// ```
pub fn create_script_address(
pkh: Hash<Blake2b_224, Script>,
sc: Hash<Blake2b_224, Script>,
) -> Address {
// empty bytearrays means dont add the sc to the pkh
if bytearray.is_empty(sc) {
credential.from_script(pkh)
} else {
credential.from_script(pkh)
|> credential.with_delegation_script(sc)
}
}

test enterprise_script() {
let script_addr =
Address {
payment_credential: ScriptCredential(#"acab"),
stake_credential: None,
}
create_script_address(#"acab", #"") == script_addr
}

test base_script() {
let script_addr =
Address {
payment_credential: ScriptCredential(#"acab"),
stake_credential: Some(Inline(ScriptCredential(#"face"))),
}
create_script_address(#"acab", #"face") == script_addr
}

0 comments on commit 3832c04

Please sign in to comment.