diff --git a/.gitignore b/.gitignore index 826412aa..51b2578b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ /target .idea _site/ -temp \ No newline at end of file +temp +build/ +assets/ \ No newline at end of file diff --git a/examples/sample/aiken.lock b/examples/hello_world/aiken.lock similarity index 100% rename from examples/sample/aiken.lock rename to examples/hello_world/aiken.lock diff --git a/examples/hello_world/aiken.toml b/examples/hello_world/aiken.toml new file mode 100644 index 00000000..88bf5be0 --- /dev/null +++ b/examples/hello_world/aiken.toml @@ -0,0 +1,7 @@ +name = "aiken-lang/hello_world" +version = "0.0.0" +licences = ["Apache-2.0"] +description = "Aiken contracts for project 'aiken-lang/hello_world'" +dependencies = [ + { name = "aiken-lang/stdlib", version = "main", source = "github" }, +] diff --git a/examples/hello_world/hello_world-keygen.ts b/examples/hello_world/hello_world-keygen.ts new file mode 100644 index 00000000..ad297cd6 --- /dev/null +++ b/examples/hello_world/hello_world-keygen.ts @@ -0,0 +1,9 @@ +import { Lucid } from "https://deno.land/x/lucid@0.8.3/mod.ts"; + +const lucid = await Lucid.new(undefined, "Preview"); + +const privateKey = lucid.utils.generatePrivateKey(); +await Deno.writeTextFile("key.sk", privateKey); + +const address = await lucid.selectWalletFromPrivateKey(privateKey).wallet.address(); +await Deno.writeTextFile("key.addr", address); diff --git a/examples/hello_world/hello_world-lock.ts b/examples/hello_world/hello_world-lock.ts new file mode 100644 index 00000000..d9eae792 --- /dev/null +++ b/examples/hello_world/hello_world-lock.ts @@ -0,0 +1,65 @@ +import { + Blockfrost, + Constr, + Data, + Lucid, + SpendingValidator, + TxHash, + fromHex, + toHex +} from "https://deno.land/x/lucid@0.8.3/mod.ts"; +import * as cbor from "https://deno.land/x/cbor@v1.4.1/index.js"; + +const lucid = await Lucid.new( + new Blockfrost( + "https://cardano-preview.blockfrost.io/api/v0", + /* BLOCKFROST API KEY */ + ), + "Preview", +); + +lucid.selectWalletFromPrivateKey(await Deno.readTextFile("./key.sk")); + +const validator = await readValidator("./assets/hello_world/spend/script.cbor"); + +// --- Supporting functions + +async function readValidator(filepath: String): Promise { + return { + type: "PlutusV2", + script: toHex(cbor.encode(fromHex(await Deno.readTextFile(filepath)))), + }; +} + +const publicKeyHash = lucid.utils + .getAddressDetails(await lucid.wallet.address()) + .paymentCredential + .hash; + +const datum = Data.to(new Constr(0, [ publicKeyHash ])); + +const txLock = await lock(1000000, { into: validator, owner: datum }); + +await lucid.awaitTx(txLock); + +console.log(`1 ADA locked into the contract + Tx ID: ${txLock} + Datum: ${datum} +`); + +// --- Supporting functions + +async function lock(lovelace, { into, owner }): Promise{ + const contractAddress = lucid.utils.validatorToAddress(into); + + const tx = await lucid + .newTx() + .payToContract(contractAddress, { inline: owner }, { lovelace }) + .complete(); + + const signedTx = await tx + .sign() + .complete(); + + return signedTx.submit(); +} \ No newline at end of file diff --git a/examples/hello_world/hello_world-unlock.ts b/examples/hello_world/hello_world-unlock.ts new file mode 100644 index 00000000..8c903e8e --- /dev/null +++ b/examples/hello_world/hello_world-unlock.ts @@ -0,0 +1,63 @@ +import { + Blockfrost, + Constr, + Data, + Lucid, + SpendingValidator, + TxHash, + fromHex, + toHex, + utf8ToHex + } from "https://deno.land/x/lucid@0.8.3/mod.ts"; + import * as cbor from "https://deno.land/x/cbor@v1.4.1/index.js"; + +const lucid = await Lucid.new( + new Blockfrost( + "https://cardano-preview.blockfrost.io/api/v0", + /* BLOCKFROST API KEY */ + ), + "Preview", + ); + +lucid.selectWalletFromPrivateKey(await Deno.readTextFile("./key.sk")); + +const validator = await readValidator("./assets/hello_world/spend/script.cbor"); + +const utxo = { txHash: '/* Tx ID from hello_world_lock */', outputIndex: 0 }; + +const redeemer = Data.to(new Constr(0, [ utf8ToHex("Hello, World!")]) ); + +const txUnlock = await unlock(utxo, { from: validator, using: redeemer }); + +await lucid.awaitTx(txUnlock); + +console.log(`1 ADA recovered from the contract + Tx ID: ${txUnlock} + Redeemer: ${redeemer} +`); + +// --- Supporting functions + +async function unlock(ref, { from, using }): Promise { + const [utxo] = await lucid.utxosByOutRef([ref]); + + const tx = await lucid + .newTx() + .collectFrom([utxo], using) + .addSigner(await lucid.wallet.address()) + .attachSpendingValidator(from) + .complete(); + + const signedTx = await tx + .sign() + .complete(); + + return signedTx.submit(); +} + +async function readValidator(filepath: String): Promise { + return { + type: "PlutusV2", + script: toHex(cbor.encode(fromHex(await Deno.readTextFile(filepath)))), + }; +} diff --git a/examples/hello_world/validators/hello_world.ak b/examples/hello_world/validators/hello_world.ak new file mode 100644 index 00000000..bb74edd5 --- /dev/null +++ b/examples/hello_world/validators/hello_world.ak @@ -0,0 +1,22 @@ +use aiken/hash.{Blake2b_224, Hash} +use aiken/list +use aiken/string +use aiken/transaction.{ScriptContext} +use aiken/transaction/credential.{VerificationKey} + +pub type Datum { + owner: Hash, +} + +pub type Redeemer { + msg: ByteArray, +} + +pub fn spend(datum: Datum, redeemer: Redeemer, context: ScriptContext) -> Bool { + let must_say_hello = string.from_bytearray(redeemer.msg) == "Hello, World!" + + let must_be_signed = + list.has(context.transaction.extra_signatories, datum.owner) + + must_say_hello && must_be_signed +} diff --git a/examples/sample/.gitignore b/examples/sample/.gitignore deleted file mode 100644 index 567609b1..00000000 --- a/examples/sample/.gitignore +++ /dev/null @@ -1 +0,0 @@ -build/ diff --git a/examples/sample/aiken.toml b/examples/sample/aiken.toml deleted file mode 100644 index 74580cc9..00000000 --- a/examples/sample/aiken.toml +++ /dev/null @@ -1,6 +0,0 @@ -name = "aiken/sample" -version = "0.0.1" - -dependencies = [ - { name = "aiken-lang/stdlib", version = "main", source = "github" }, -] diff --git a/examples/sample/assets/swap/spend/mainnet.addr b/examples/sample/assets/swap/spend/mainnet.addr deleted file mode 100644 index 3d559d5a..00000000 --- a/examples/sample/assets/swap/spend/mainnet.addr +++ /dev/null @@ -1 +0,0 @@ -addr1w9ke4flw57jhv5tjefm8xwkfdc56et559npkcycd6u9ascc07drtu \ No newline at end of file diff --git a/examples/sample/assets/swap/spend/payment_script.json b/examples/sample/assets/swap/spend/payment_script.json deleted file mode 100644 index 53ca12e9..00000000 --- a/examples/sample/assets/swap/spend/payment_script.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "type": "PlutusScriptV2", - "description": "Generated by Aiken", - "cborHex": "58c058be0100002225333573464646464a666ae68008400452819191919800801919b8f001375c6600c600e01490001800800911192999aab9f00114a026464a666ae68c01000852889998030030008021aba2002375c6ae84004008dd619801180199801180180224000900819b97323001375c66004600600a900011b9900149010d48656c6c6f2c20576f726c64210022323330010014800000c888cccd5cd19b870040025742466600800866e0000d20023574400200246aae78dd50008a4c2d" -} \ No newline at end of file diff --git a/examples/sample/assets/swap/spend/script.cbor b/examples/sample/assets/swap/spend/script.cbor deleted file mode 100644 index dd7ef103..00000000 --- a/examples/sample/assets/swap/spend/script.cbor +++ /dev/null @@ -1 +0,0 @@ -58be0100002225333573464646464a666ae68008400452819191919800801919b8f001375c6600c600e01490001800800911192999aab9f00114a026464a666ae68c01000852889998030030008021aba2002375c6ae84004008dd619801180199801180180224000900819b97323001375c66004600600a900011b9900149010d48656c6c6f2c20576f726c64210022323330010014800000c888cccd5cd19b870040025742466600800866e0000d20023574400200246aae78dd50008a4c2d \ No newline at end of file diff --git a/examples/sample/assets/swap/spend/testnet.addr b/examples/sample/assets/swap/spend/testnet.addr deleted file mode 100644 index 141a7c52..00000000 --- a/examples/sample/assets/swap/spend/testnet.addr +++ /dev/null @@ -1 +0,0 @@ -addr_test1wpke4flw57jhv5tjefm8xwkfdc56et559npkcycd6u9ascc5kelye \ No newline at end of file diff --git a/examples/sample/lib/sample.ak b/examples/sample/lib/sample.ak deleted file mode 100644 index 5f7c7ebc..00000000 --- a/examples/sample/lib/sample.ak +++ /dev/null @@ -1,7 +0,0 @@ -pub type Redeemer { - signer: ByteArray, -} - -pub type Datum { - random: ByteArray, -} diff --git a/examples/sample/lib/sample/thing.ak b/examples/sample/lib/sample/thing.ak deleted file mode 100644 index 503d6da2..00000000 --- a/examples/sample/lib/sample/thing.ak +++ /dev/null @@ -1,5 +0,0 @@ -use aiken/builtin - -test bar() { - builtin.length_of_bytearray(#[2, 2, 3]) == 3 -} diff --git a/examples/sample/validators/swap.ak b/examples/sample/validators/swap.ak deleted file mode 100644 index 16a95b3b..00000000 --- a/examples/sample/validators/swap.ak +++ /dev/null @@ -1,52 +0,0 @@ -use aiken/list -use aiken/string -use aiken/hash.{Blake2b_224, Hash} -use aiken/transaction.{ScriptContext} -use aiken/transaction/credential.{VerificationKey} - -pub type Datum { - owner: Hash, -} - -pub type Redeemer { - msg: ByteArray, -} - -pub type Dummy { - Mannequin { - hands: ByteArray, - feet: Int, - } - Doll { - hands: ByteArray, - datum: Datum, - feet: Int, - } - Puppet { - hands: ByteArray, - dummy: Dummy, - } - Statue { - hands: ByteArray, - boots: ByteArray, - } -} - -pub fn spending(datum: Datum, redeemer: Redeemer, context: Dummy) -> Bool { - let must_say_hello = string.from_bytearray(redeemer.msg) == "Hello, World!" - let must_be_signed = #(1, datum, #(redeemer, context )) - // context.transaction.extra_signatories - // |> list.any(fn(vk) { vk == datum.owner }) - - when must_be_signed is { - #(a, b, #(c, Mannequin{ feet, ..})) -> feet == 2 - _ -> False - - } - -} - - -test spend_1(){ - spending(Datum{ owner: #[254]}, Redeemer{msg: string.to_bytearray("Hello, World!")}, Mannequin{hands: #[], feet: 2}) == True -} \ No newline at end of file