Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc review comments #15

Merged
merged 1 commit into from
Mar 27, 2024
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
16 changes: 9 additions & 7 deletions src/edict.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
import * as bitcoin from 'bitcoinjs-lib';
import { Option, Some, None } from '@sniptt/monads';
import { RuneId } from './runeid';
import { u128 } from './u128';
import { U32_MAX, u128 } from './u128';

export type Edict = {
id: RuneId;
amount: u128;
output: u128;
output: number;
};

export namespace Edict {
export function fromIntegers(
tx: bitcoin.Transaction,
id: u128,
id: RuneId,
amount: u128,
output: u128
): Option<Edict> {
const runeId = RuneId.fromU128(id);
if (id.block === 0 && id.tx > 0) {
return None;
}

if (runeId.block === 0 && runeId.tx > 0) {
if (output > u128(U32_MAX)) {
return None;
}

if (output > u128(tx.outs.length)) {
if (output > tx.outs.length) {
return None;
}

return Some({ id: runeId, amount, output });
return Some({ id, amount, output: Number(output) });
}
}
8 changes: 5 additions & 3 deletions src/rune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class Rune {
}

static getReserved(n: u128): Rune {
return new Rune(u128.checkedAdd(RESERVED, n));
return new Rune(u128.checkedAdd(RESERVED, n).unwrap());
}

toString() {
Expand Down Expand Up @@ -109,9 +109,11 @@ export class Rune {
if (i > 0) {
x = u128(x + 1n);
}
x = u128.checkedMultiply(x, u128(26));
x = u128.checkedMultiply(x, u128(26)).unwrap();
if ('A' <= c && c <= 'Z') {
x = u128.checkedAdd(x, u128(c.charCodeAt(0) - 'A'.charCodeAt(0)));
x = u128
.checkedAdd(x, u128(c.charCodeAt(0) - 'A'.charCodeAt(0)))
.unwrap();
} else {
throw new Error(`invalid character in rune name: ${c}`);
}
Expand Down
62 changes: 55 additions & 7 deletions src/runeid.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,66 @@
import { u128 } from './u128';
import { None, Option, Some } from '@sniptt/monads';
import _ from 'lodash';
import { U32_MAX, u128 } from './u128';

export class RuneId {
constructor(readonly block: number, readonly tx: number) {}

toU128() {
return u128((BigInt(this.block) << 16n) | BigInt(this.tx));
static new(block: number, tx: number): Option<RuneId> {
const id = new RuneId(block, tx);

if (id.block === 0 && id.tx > 0) {
return None;
}

return Some(id);
}

toString() {
return `${this.block}:${this.tx}`;
static sort(runeIds: RuneId[]): RuneId[] {
return _.sortBy(runeIds, (runeId) => [runeId.block, runeId.tx]);
}

delta(next: RuneId): Option<[u128, u128]> {
const block = next.block - this.block;
if (block < 0) {
return None;
}

let tx: number;
if (block === 0) {
tx = next.tx - this.tx;
if (tx < 0) {
return None;
}
} else {
tx = next.tx;
}

return Some([u128(block), u128(tx)]);
}

static fromU128(n: u128) {
return new RuneId(Number(n >> 16n), Number(n & 0xffffn));
next(block: u128, tx: u128): Option<RuneId> {
if (block > BigInt(U32_MAX) || tx > BigInt(U32_MAX)) {
return None;
}

const blockNumber = Number(block);
const txNumber = Number(tx);

const nextBlock = this.block + blockNumber;
if (nextBlock > U32_MAX) {
return None;
}

const nextTx = blockNumber === 0 ? this.tx + txNumber : txNumber;
if (nextTx > U32_MAX) {
return None;
}

return RuneId.new(nextBlock, nextTx);
}

toString() {
return `${this.block}:${this.tx}`;
}

static fromString(s: string) {
Expand Down
Loading