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

Runestone encoding/decoding #7

Merged
merged 3 commits into from
Mar 20, 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
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"typescript": "^5.3.3"
},
"dependencies": {
"@sniptt/monads": "^0.5.10",
"bip86": "^0.0.3",
"bitcoinjs-lib": "^6.1.5",
"ecpair": "^2.1.0",
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export const MAX_DIVISIBILITY = 38;
export const MAX_LIMIT = u128(1n << 64n);
export const RESERVED = u128(6402364363415443603228541259936211926n);
export const SUBSIDY_HALVING_INTERVAL = 210_000;
export const MAX_SCRIPT_ELEMENT_SIZE = 520;
26 changes: 19 additions & 7 deletions src/etching.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
import { None, Option, Some } from '@sniptt/monads';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just using undefined? This package doesn't seem to be very popular on npm.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is emulating rust's monad structure, it was easier to use this to ensure code is equivalent to casey's.

import { Mint } from './mint';
import { Rune } from './rune';

export type Etching = {
divisibility: number;
mint?: Mint;
rune?: Rune;
spacers: number;
symbol?: string;
};
export class Etching {
readonly symbol: Option<string>;

constructor(
readonly divisibility: number,
readonly rune: Option<Rune>,
readonly spacers: number,
symbol: Option<string>,
readonly mint: Option<Mint>
) {
this.symbol = symbol.andThen((value) => {
const codePoint = value.codePointAt(0);
return codePoint !== undefined
? Some(String.fromCodePoint(codePoint))
: None;
});
}
}
23 changes: 23 additions & 0 deletions src/flag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { u128 } from './u128';

export enum Flag {
ETCH = 0,
MINT = 1,
BURN = 127,
}

export namespace Flag {
export function mask(flag: Flag): u128 {
return u128(1n << BigInt(flag));
}

export function take(flags: u128, flag: Flag): { set: boolean; flags: u128 } {
const mask = Flag.mask(flag);
const set = (flags & mask) !== 0n;
return { set, flags: u128(flags - (set ? mask : 0n)) };
}

export function set(flags: u128, flag: Flag): u128 {
return u128(flags | Flag.mask(flag));
}
}
7 changes: 4 additions & 3 deletions src/mint.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Option } from '@sniptt/monads';
import { u128 } from './u128';

export type Mint = {
deadline?: number;
limit?: u128;
term?: number;
deadline: Option<number>;
limit: Option<u128>;
term: Option<number>;
};
2 changes: 1 addition & 1 deletion src/runeid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export class RuneId {
constructor(readonly height: number, readonly index: number) {}

toU128() {
return u128((this.height << 16) | this.index);
return u128((BigInt(this.height) << 16n) | BigInt(this.index));
}

toString() {
Expand Down
Loading