Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 33 additions & 12 deletions packages/core/src/builder/outputBuilder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,18 @@ describe("Additional registers", () => {
builder = new OutputBuilder(SAFE_MIN_BOX_VALUE, ergoTreeHex, height);
});

it("should merge additional registers", () => {
builder.setAdditionalRegisters({ R4: "0101", R6: "0102" });
expect(builder.additionalRegisters).toEqual({ R4: "0101", R6: "0102" });

expect(() => builder.build()).toThrow(InvalidRegistersPacking); // R5 is missing

builder.setAdditionalRegisters({ R5: "0103" });
expect(builder.additionalRegisters).toEqual({ R4: "0101", R5: "0103", R6: "0102" });

expect(() => builder.build()).not.toThrow();
});

it("Should bind registers properly", () => {
builder.setAdditionalRegisters({
R4: "0580c0fc82aa02",
Expand Down Expand Up @@ -335,28 +347,37 @@ describe("Additional registers", () => {
* R9 without adding register R4 to R8, for example.
*/
it("Should throw if some register is skipped", () => {
let outputBuilder = new OutputBuilder(SAFE_MIN_BOX_VALUE, ergoTreeHex, height);
// R4 not included
expect(() => {
builder.setAdditionalRegisters({
R5: "0580c0fc82aa02"
} as NonMandatoryRegisters);
outputBuilder
.setAdditionalRegisters({
R5: "0580c0fc82aa02"
} as NonMandatoryRegisters)
.build();
}).toThrow(InvalidRegistersPacking);

outputBuilder = new OutputBuilder(SAFE_MIN_BOX_VALUE, ergoTreeHex, height);
// R5 not included
expect(() => {
builder.setAdditionalRegisters({
R4: "0580c0fc82aa02",
R6: "0580c0fc82aa02"
} as NonMandatoryRegisters);
outputBuilder
.setAdditionalRegisters({
R4: "0580c0fc82aa02",
R6: "0580c0fc82aa02"
} as NonMandatoryRegisters)
.build();
}).toThrow(InvalidRegistersPacking);

outputBuilder = new OutputBuilder(SAFE_MIN_BOX_VALUE, ergoTreeHex, height);
// R5 explicitly set to undefined
expect(() => {
builder.setAdditionalRegisters({
R4: "0580c0fc82aa02",
R5: undefined,
R6: "0580c0fc82aa02"
} as NonMandatoryRegisters);
outputBuilder
.setAdditionalRegisters({
R4: "0580c0fc82aa02",
R5: undefined,
R6: "0580c0fc82aa02"
} as NonMandatoryRegisters)
.build();
}).toThrow(InvalidRegistersPacking);
});
});
Expand Down
9 changes: 3 additions & 6 deletions packages/core/src/builder/outputBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,13 @@ export class OutputBuilder {
}

setAdditionalRegisters(registers: NonMandatoryRegisters<ConstantInput>): OutputBuilder {
const hexRegisters: NonMandatoryRegisters = {};
for (const key in registers) {
const r = registers[key as keyof NonMandatoryRegisters];
if (!r) continue;

hexRegisters[key as keyof NonMandatoryRegisters] = typeof r === "string" ? r : r.toHex();
this.#registers[key as keyof NonMandatoryRegisters] = typeof r === "string" ? r : r.toHex();
}

if (!areRegistersDenselyPacked(hexRegisters)) throw new InvalidRegistersPacking();
this.#registers = hexRegisters;

return this;
}

Expand All @@ -187,8 +183,9 @@ export class OutputBuilder {
}

build(transactionInputs?: UnsignedInput[] | Box<Amount>[]): ErgoBoxCandidate {
let tokens: TokenAmount<bigint>[];
if (!areRegistersDenselyPacked(this.#registers)) throw new InvalidRegistersPacking();

let tokens: TokenAmount<bigint>[];
if (this.minting) {
const mintingTokenId = transactionInputs ? transactionInputs[0]?.boxId : undefined;
tokens = this.assets.toArray(mintingTokenId);
Expand Down