hello world for starters
This commit is contained in:
13
examples/hello_world/aiken.lock
Normal file
13
examples/hello_world/aiken.lock
Normal file
@@ -0,0 +1,13 @@
|
||||
# This file was generated by Aiken
|
||||
# You typically do not need to edit this file
|
||||
|
||||
[[requirements]]
|
||||
name = "aiken-lang/stdlib"
|
||||
version = "main"
|
||||
source = "github"
|
||||
|
||||
[[packages]]
|
||||
name = "aiken-lang/stdlib"
|
||||
version = "main"
|
||||
requirements = []
|
||||
source = "github"
|
||||
7
examples/hello_world/aiken.toml
Normal file
7
examples/hello_world/aiken.toml
Normal file
@@ -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" },
|
||||
]
|
||||
9
examples/hello_world/hello_world-keygen.ts
Normal file
9
examples/hello_world/hello_world-keygen.ts
Normal file
@@ -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);
|
||||
65
examples/hello_world/hello_world-lock.ts
Normal file
65
examples/hello_world/hello_world-lock.ts
Normal file
@@ -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<SpendingValidator> {
|
||||
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<TxHash>{
|
||||
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();
|
||||
}
|
||||
63
examples/hello_world/hello_world-unlock.ts
Normal file
63
examples/hello_world/hello_world-unlock.ts
Normal file
@@ -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<TxHash> {
|
||||
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<SpendingValidator> {
|
||||
return {
|
||||
type: "PlutusV2",
|
||||
script: toHex(cbor.encode(fromHex(await Deno.readTextFile(filepath)))),
|
||||
};
|
||||
}
|
||||
22
examples/hello_world/validators/hello_world.ak
Normal file
22
examples/hello_world/validators/hello_world.ak
Normal file
@@ -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<Blake2b_224, VerificationKey>,
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user