Skip to content

Commit

Permalink
Basic Script Deployment Utils (#193)
Browse files Browse the repository at this point in the history
* feat: script deployments and script ref provider query

* docs change

* example fix

* rebase

* fmt
  • Loading branch information
EzePze authored Oct 31, 2024
1 parent 08ae1ce commit f4ae116
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 57 deletions.
9 changes: 9 additions & 0 deletions .changeset/eleven-walls-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@blaze-cardano/emulator": minor
"@blaze-cardano/query": minor
"@blaze-cardano/core": minor
"@blaze-cardano/tx": minor
"@blaze-cardano/ogmios": patch
---

feat: Script deployment methods for tx and script-ref resolving provider query
13 changes: 12 additions & 1 deletion packages/blaze-core/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type {
PaymentAddress,
Script,
NetworkId,
Credential,
Ed25519PrivateExtendedKeyHex,
Ed25519PrivateNormalKeyHex,
} from "./types";
Expand All @@ -18,6 +17,7 @@ import {
AddressType,
Hash32ByteBase16,
Ed25519SignatureHex,
Credential,
} from "./types";
import { sha256 } from "@noble/hashes/sha256";
import * as sha3 from "@noble/hashes/sha3";
Expand Down Expand Up @@ -269,6 +269,17 @@ export const addressFromCredentials = (
});
};

const burnCred = Credential.fromCore({
hash: Hash28ByteBase16(
// From https://cardano-tools.io/burn-address
"bbece14f554b0020fe2715d05801f4680ebd40d11a58f14740b9f2c5",
),
type: CredentialType.ScriptHash,
});

export const getBurnAddress = (network: NetworkId) =>
addressFromCredential(network, burnCred);

/**
* Interface for objects that can be serialized to CBOR.
*/
Expand Down
6 changes: 4 additions & 2 deletions packages/blaze-emulator/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,24 @@ import {
TransactionInput,
PlutusData,
TransactionOutput,
NetworkId,
} from "@blaze-cardano/core";
import { TransactionUnspentOutput } from "@blaze-cardano/core";
import type { Provider } from "@blaze-cardano/query";
import { Provider } from "@blaze-cardano/query";
import type { Emulator } from "./emulator";

/**
* The EmulatorProvider class implements the Provider interface.
* It provides methods to interact with the Emulator.
*/
export class EmulatorProvider implements Provider {
export class EmulatorProvider extends Provider {
/**
* The Emulator instance.
*/
private emulator: Emulator;

constructor(emulator: Emulator) {
super(NetworkId.Testnet);
this.emulator = emulator;
}
getParameters(): Promise<ProtocolParameters> {
Expand Down
55 changes: 26 additions & 29 deletions packages/blaze-emulator/test/Emulator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
import { HotWallet } from "@blaze-cardano/wallet";
import { Emulator, EmulatorProvider } from "../src";
import {
DEPLOYMENT_ADDR,
ONE_PLUTUS_DATA,
VOID_PLUTUS_DATA,
alwaysTrueScript,
Expand Down Expand Up @@ -67,7 +66,8 @@ describe("Emulator", () => {
});

test("Should be able to pay from one wallet to another", async () => {
const tx = await (await blaze.newTransaction())
const tx = await blaze
.newTransaction()
.payLovelace(wallet2.address, 2_000_000_000n)
.complete();
const txHash = await signAndSubmit(tx, blaze);
Expand All @@ -79,9 +79,8 @@ describe("Emulator", () => {
});

test("Should be able to spend from a script", async () => {
const tx = await (
await blaze.newTransaction()
)
const tx = await blaze
.newTransaction()
.lockAssets(
addressFromCredential(
NetworkId.Testnet,
Expand All @@ -100,9 +99,8 @@ describe("Emulator", () => {
const out = emulator.getOutput(inp);
isDefined(out);
isDefined(out.datum());
const spendTx = await (
await blaze.newTransaction()
)
const spendTx = await blaze
.newTransaction()
.addInput(new TransactionUnspentOutput(inp, out), VOID_PLUTUS_DATA)
.lockAssets(
addressFromCredential(
Expand All @@ -125,25 +123,22 @@ describe("Emulator", () => {
});

test("Should be able to spend from a script with a reference input", async () => {
const refTx = await (await blaze.newTransaction())
.lockAssets(
DEPLOYMENT_ADDR,
makeValue(1_000_000_000n),
ONE_PLUTUS_DATA,
alwaysTrueScript,
)
const refTx = await blaze
.newTransaction()
.deployScript(alwaysTrueScript)
.complete();
const refTxHash = await signAndSubmit(refTx, blaze);
emulator.awaitTransactionConfirmation(refTxHash);
const refIn = new TransactionInput(refTxHash, 0n);
const refUtxo = new TransactionUnspentOutput(
refIn,
emulator.getOutput(refIn)!,
);
const refUtxo = await provider.resolveScriptRef(alwaysTrueScript);
isDefined(refUtxo);
// const refIn = new TransactionInput(refTxHash, 0n);
// const refUtxo = new TransactionUnspentOutput(
// refIn,
// emulator.getOutput(refIn)!
// );

const tx = await (
await blaze.newTransaction()
)
const tx = await blaze
.newTransaction()
.lockAssets(
addressFromCredential(
NetworkId.Testnet,
Expand All @@ -164,7 +159,8 @@ describe("Emulator", () => {

isDefined(out);

const spendTx = await (await blaze.newTransaction())
const spendTx = await blaze
.newTransaction()
.addInput(new TransactionUnspentOutput(inp, out), VOID_PLUTUS_DATA)
.addReferenceInput(refUtxo)
.complete();
Expand All @@ -184,7 +180,8 @@ describe("Emulator", () => {
}),
);

const tx = await (await blaze.newTransaction())
const tx = await blaze
.newTransaction()
.lockAssets(
addr,
makeValue(1_000_000_000n),
Expand All @@ -200,7 +197,8 @@ describe("Emulator", () => {

isDefined(out);

const spendTx = await (await blaze.newTransaction())
const spendTx = await blaze
.newTransaction()
.addInput(new TransactionUnspentOutput(inp, out), VOID_PLUTUS_DATA)
.complete();
const spendTxHash = await signAndSubmit(spendTx, blaze);
Expand All @@ -212,9 +210,8 @@ describe("Emulator", () => {

test("Should be able to mint from a policy", async () => {
const policy = PolicyId(alwaysTrueScript.hash());
const tx = await (
await blaze.newTransaction()
)
const tx = await blaze
.newTransaction()
.addMint(policy, new Map([[AssetName(""), 1n]]), VOID_PLUTUS_DATA)
.provideScript(alwaysTrueScript)
.complete();
Expand Down
15 changes: 0 additions & 15 deletions packages/blaze-emulator/test/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@ import {
Transaction,
TransactionId,
AddressType,
CredentialType,
HexBlob,
NetworkId,
Address,
Script,
PlutusV2Script,
PlutusData,
Credential,
Hash28ByteBase16,
addressFromCredential,
} from "@blaze-cardano/core";
import type { Provider } from "@blaze-cardano/query";
import type { Blaze } from "@blaze-cardano/sdk";
Expand All @@ -28,17 +24,6 @@ export const generateSeedPhrase = () => generateMnemonic(wordlist);
export const VOID_PLUTUS_DATA = PlutusData.fromCbor(HexBlob("00"));
export const ONE_PLUTUS_DATA = PlutusData.fromCbor(HexBlob("01"));

// From https://cardano-tools.io/burn-address
export const LOCK_SCRIPT_HASH =
"bbece14f554b0020fe2715d05801f4680ebd40d11a58f14740b9f2c5";
export const DEPLOYMENT_ADDR = addressFromCredential(
NetworkId.Testnet,
Credential.fromCore({
hash: Hash28ByteBase16(LOCK_SCRIPT_HASH),
type: CredentialType.ScriptHash,
}),
);

export const SAMPLE_PLUTUS_DATA = PlutusData.fromCore(
new Uint8Array([1, 2, 3]),
);
Expand Down
2 changes: 1 addition & 1 deletion packages/blaze-ogmios/src/unwrapped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import WebSocket from "isomorphic-ws";
import type * as schema from "./schema";

export class Ogmios {
url: string;
private ws: WebSocket;
private url: string;
private requests: Record<
string,
{ resolve: (value: any) => void; reject: (reason: any) => void }
Expand Down
6 changes: 4 additions & 2 deletions packages/blaze-query/src/blockfrost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
hardCodedProtocolParams,
Hash28ByteBase16,
HexBlob,
NetworkId,
PlutusData,
PlutusV1Script,
PlutusV2Script,
Expand All @@ -31,9 +32,9 @@ import {
Value,
} from "@blaze-cardano/core";
import { PlutusLanguageVersion } from "@blaze-cardano/core";
import { purposeToTag, type Provider } from "./types";
import { purposeToTag, Provider } from "./provider";

export class Blockfrost implements Provider {
export class Blockfrost extends Provider {
url: string;
private projectId: string;

Expand All @@ -48,6 +49,7 @@ export class Blockfrost implements Provider {
| "cardano-sanchonet";
projectId: string;
}) {
super(network == "cardano-mainnet" ? NetworkId.Mainnet : NetworkId.Testnet);
this.url = `https://${network}.blockfrost.io/api/v0/`;
this.projectId = projectId;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/blaze-query/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from "./maestro";
export * from "./blockfrost";
export * from "./kupmios";
export * from "./types";
export * from "./provider";
8 changes: 6 additions & 2 deletions packages/blaze-query/src/kupmios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ import {
PlutusV1Script,
PlutusV2Script,
PlutusV3Script,
NetworkId,
} from "@blaze-cardano/core";
import { purposeToTag, type Provider } from "./types";
import { purposeToTag, Provider } from "./provider";
import type { Unwrapped } from "@blaze-cardano/ogmios";
import type * as Schema from "@cardano-ogmios/schema";

export class Kupmios implements Provider {
export class Kupmios extends Provider {
kupoUrl: string;
ogmios: Unwrapped.Ogmios;

Expand All @@ -48,6 +49,9 @@ export class Kupmios implements Provider {
* @param ogmiosUrl - URL of the Ogmios service.
*/
constructor(kupoUrl: string, ogmios: Unwrapped.Ogmios) {
super(
ogmios.url.includes("mainnet-v6") ? NetworkId.Mainnet : NetworkId.Testnet,
);
this.kupoUrl = kupoUrl;
this.ogmios = ogmios;
}
Expand Down
7 changes: 4 additions & 3 deletions packages/blaze-query/src/maestro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
CostModels,
Credential,
} from "@blaze-cardano/core";
import { RedeemerTag } from "@blaze-cardano/core";
import { NetworkId, RedeemerTag } from "@blaze-cardano/core";
import {
TransactionUnspentOutput,
Address,
Expand All @@ -21,9 +21,9 @@ import {
Redeemers,
ExUnits,
} from "@blaze-cardano/core";
import type { Provider } from "./types";
import { Provider } from "./provider";

export class Maestro implements Provider {
export class Maestro extends Provider {
private url: string;
private apiKey: string;

Expand All @@ -34,6 +34,7 @@ export class Maestro implements Provider {
network: "mainnet" | "preview" | "preprod";
apiKey: string;
}) {
super(network == "mainnet" ? NetworkId.Mainnet : NetworkId.Testnet);
this.url = `https://${network}.gomaestro-api.org/v1`;
this.apiKey = apiKey;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,25 @@ import {
type Transaction,
type ProtocolParameters,
type Redeemers,
type NetworkId,
type Hash28ByteBase16,
RedeemerPurpose,
RedeemerTag,
Script,
getBurnAddress,
} from "@blaze-cardano/core";

/**
* Abstract class for the Provider.
* This class provides an interface for interacting with the blockchain.
*/
export abstract class Provider {
network: NetworkId;

constructor(network: NetworkId) {
this.network = network;
}

/**
* Retrieves the parameters for a transaction.
*
Expand Down Expand Up @@ -107,6 +117,41 @@ export abstract class Provider {
tx: Transaction,
additionalUtxos: TransactionUnspentOutput[],
): Promise<Redeemers>;

/**
* Resolves the script deployment by finding a UTxO containing the script reference.
*
* @param {Script | Hash28ByteBase16} script - The script or its hash to resolve.
* @param {Address} [address] - The address to search for the script deployment. Defaults to a burn address.
* @returns {Promise<TransactionUnspentOutput | undefined>} - The UTxO containing the script reference, or undefined if not found.
*
* @remarks
* This is a default implementation that works but may not be optimal.
* Subclasses of Provider should implement their own version for better performance.
*
* The method searches for a UTxO at the given address (or a burn address by default)
* that contains a script reference matching the provided script or script hash.
*
* @example
* ```typescript
* const scriptUtxo = await provider.resolveScriptRef(myScript);
* if (scriptUtxo) {
* console.log("Script found in UTxO:", scriptUtxo.input().toCore());
* } else {
* console.log("Script not found");
* }
* ```
*/
async resolveScriptRef(
script: Script | Hash28ByteBase16,
address: Address = getBurnAddress(this.network),
): Promise<TransactionUnspentOutput | undefined> {
const utxos = await this.getUnspentOutputs(address);
if (script instanceof Script) {
script = script.hash();
}
return utxos.find((utxo) => utxo.output().scriptRef()?.hash() === script);
}
}

/**
Expand Down
Loading

0 comments on commit f4ae116

Please sign in to comment.