Huge commit. Multiple fixes. Refactored commands. Invitations, resources, template inspection, mnemonic stuff, cli utils, pretty printing, remove unreserve on start, fix connectino requirement for invitations, format cashAddress to lockingBytecode on send, lots and lots of other stuff.

This commit is contained in:
2026-04-06 11:56:09 +00:00
parent b475b23beb
commit 55c75501d5
24 changed files with 3284 additions and 77 deletions

View File

@@ -3,6 +3,7 @@
* Pulled directly from the old stack package.
*/
import { z } from "zod";
import { decodeBip39Mnemonic } from "@bitauth/libauth";
export type BCHMnemonicURLRaw = {
entropy: Uint8Array;
@@ -86,6 +87,18 @@ export class BCHMnemonicURL {
return new BCHMnemonicURL(raw);
}
static fromSeed(seed: string): BCHMnemonicURL {
// Encode the seed to a Uint8Array
const entropy = decodeBip39Mnemonic(seed);
// If the decode failed, throw an error
if (typeof entropy === "string") {
throw new Error(`Invalid seed: ${entropy}`);
}
return BCHMnemonicURL.fromRaw({ entropy });
}
constructor(protected raw: BCHMnemonicURLRaw) {}
toObject() {

View File

@@ -1,5 +1,6 @@
import type { XOTemplate, XOTemplateTransactionOutput } from "@xo-cash/types";
import type { Invitation } from "../services/invitation.js";
import { cashAddressToLockingBytecode, binToHex } from "@bitauth/libauth";
export interface SelectableUtxoLike {
outpointTransactionHash: string;
@@ -97,6 +98,34 @@ export const getTransactionOutputIdentifier = (
export const normalizeLockingBytecodeHex = (value: string): string =>
value.trim().replace(/^0x/i, "");
/**
* Checks whether a string looks like a CashAddress and, if so, converts it
* to locking bytecode hex. Returns undefined when the value is not a
* recognizable CashAddress (callers should fall through to treat it as raw hex).
*/
export const tryCashAddressToLockingBytecodeHex = (
value: string,
): string | undefined => {
const trimmed = value.trim();
// Quick prefix check so we don't run the decoder on obvious hex strings.
const looksLikeCashAddress =
trimmed.startsWith("bitcoincash:") ||
trimmed.startsWith("bchtest:") ||
trimmed.startsWith("bchreg:") ||
// Handle prefix-less addresses (e.g. "qp..." or "pp...")
/^[qpQP][a-zA-Z0-9]{41,}$/.test(trimmed);
if (!looksLikeCashAddress) return undefined;
const result = cashAddressToLockingBytecode(trimmed);
// cashAddressToLockingBytecode returns a string on failure.
if (typeof result === "string") return undefined;
return binToHex(result.bytecode);
};
export const resolveProvidedLockingBytecodeHex = (
template: XOTemplate,
outputIdentifier: string,
@@ -128,6 +157,10 @@ export const resolveProvidedLockingBytecodeHex = (
const providedValue = variableValues[variableIdentifier];
if (!providedValue) return undefined;
// If the user pasted a CashAddress, convert it to locking bytecode hex.
const fromAddress = tryCashAddressToLockingBytecodeHex(providedValue);
if (fromAddress) return fromAddress;
return normalizeLockingBytecodeHex(providedValue);
};