Skip to content

Commit

Permalink
override ErgoTree encoding network from constructor params
Browse files Browse the repository at this point in the history
  • Loading branch information
arobsn committed Dec 5, 2024
1 parent 8ccb516 commit c0a3d35
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-bats-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fleet-sdk/core": patch
---

Override ErgoTree encoding network from constructor params
5 changes: 5 additions & 0 deletions .changeset/tame-files-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fleet-sdk/core": patch
---

Add `ErgoTree.encode()` method
54 changes: 54 additions & 0 deletions packages/core/src/models/ergoTree.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { hex } from "@fleet-sdk/crypto";
import { describe, expect, it, test } from "vitest";
import { ErgoTree } from "./ergoTree";
import { Network } from "@fleet-sdk/common";
import { ErgoAddress } from "./ergoAddress";

describe("ErgoTree model", () => {
test.each([
Expand Down Expand Up @@ -56,3 +58,55 @@ describe("ErgoTree model", () => {
expect(tree.toAddress().ergoTree).to.be.equal(treeHex);
});
});

describe("Encoding", () => {
const treeHex = "100104c801d191a37300";

it("Should default to mainnet", () => {
const tree = new ErgoTree(treeHex);
expect(tree.toAddress().network).to.be.equal(Network.Mainnet);
});

it("Should override encoding network from constructor params", () => {
const mainnetTree = new ErgoTree(treeHex, Network.Mainnet);
expect(mainnetTree.toAddress().network).to.be.equal(Network.Mainnet);

const testnetTree = new ErgoTree(treeHex, Network.Testnet);
expect(testnetTree.toAddress().network).to.be.equal(Network.Testnet);
});

it("Should explicitly encode for testnet", () => {
const tree = new ErgoTree(treeHex);
expect(tree.toAddress(Network.Testnet).network).to.be.equal(Network.Testnet);
});

it("Should explicitly encode for mainnet", () => {
const tree = new ErgoTree(treeHex);
expect(tree.toAddress(Network.Mainnet).network).to.be.equal(Network.Mainnet);
});

it("Should explicitly encode for a network despite of constructor params", () => {
const tree1 = new ErgoTree(treeHex, Network.Mainnet);
expect(tree1.toAddress(Network.Testnet).network).to.be.equal(Network.Testnet);

const tree2 = new ErgoTree(treeHex, Network.Testnet);
expect(tree2.toAddress(Network.Mainnet).network).to.be.equal(Network.Mainnet);
});

it("Should encode to Base58 string", () => {
// should default to mainnet
expect(ErgoAddress.decode(new ErgoTree(treeHex).encode()).network).to.be.equal(
Network.Mainnet
);

// should encode for testnet
expect(
ErgoAddress.decode(new ErgoTree(treeHex).encode(Network.Testnet)).network
).to.be.equal(Network.Testnet);

// should override encoding network from constructor params
expect(
ErgoAddress.decode(new ErgoTree(treeHex, Network.Testnet).encode()).network
).to.be.equal(Network.Testnet);
});
});
30 changes: 21 additions & 9 deletions packages/core/src/models/ergoTree.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
import { ergoTreeHeaderFlags, type HexString, Network } from "@fleet-sdk/common";
import {
ergoTreeHeaderFlags,
Network,
type Base58String,
type HexString
} from "@fleet-sdk/common";
import { hex } from "@fleet-sdk/crypto";
import { ErgoAddress } from "./ergoAddress";

const VERSION_MASK = 0x07;

export class ErgoTree {
private _bytes: Uint8Array;
#bytes: Uint8Array;
#network: Network;

constructor(input: HexString | Uint8Array) {
constructor(input: HexString | Uint8Array, network?: Network) {
if (typeof input === "string") {
this._bytes = hex.decode(input);
this.#bytes = hex.decode(input);
} else {
this._bytes = input;
this.#bytes = input;
}

this.#network = network ?? Network.Mainnet;
}

get header(): number {
return this._bytes[0];
return this.#bytes[0];
}

get version(): number {
Expand All @@ -32,14 +40,18 @@ export class ErgoTree {
}

toBytes(): Uint8Array {
return this._bytes;
return this.#bytes;
}

toHex(): HexString {
return hex.encode(this.toBytes());
}

toAddress(network = Network.Mainnet): ErgoAddress {
return ErgoAddress.fromErgoTree(this.toHex(), network);
toAddress(network?: Network): ErgoAddress {
return ErgoAddress.fromErgoTree(this.toHex(), network ?? this.#network);
}

encode(network?: Network): Base58String {
return this.toAddress(network).encode();
}
}

0 comments on commit c0a3d35

Please sign in to comment.