···11+export * from "./const.ts";
22+export * from "./did.ts";
33+export * from "./multibase.ts";
44+export * from "./random.ts";
55+export * from "./sha.ts";
66+export * from "./types.ts";
77+export * from "./verify.ts";
88+export * from "./utils.ts";
99+1010+export * from "./p256/keypair.ts";
1111+export * from "./p256/plugin.ts";
1212+1313+export * from "./secp256k1/keypair.ts";
1414+export * from "./secp256k1/plugin.ts";
+52
crypto/multibase.ts
···11+import { fromString, type SupportedEncodings, toString } from "@atp/ui8";
22+33+export const multibaseToBytes = (mb: string): Uint8Array => {
44+ const base = mb[0];
55+ const key = mb.slice(1);
66+ switch (base) {
77+ case "f":
88+ return fromString(key, "base16");
99+ case "F":
1010+ return fromString(key, "base16upper");
1111+ case "b":
1212+ return fromString(key, "base32");
1313+ case "B":
1414+ return fromString(key, "base32upper");
1515+ case "z":
1616+ return fromString(key, "base58btc");
1717+ case "m":
1818+ return fromString(key, "base64");
1919+ case "u":
2020+ return fromString(key, "base64url");
2121+ case "U":
2222+ return fromString(key, "base64urlpad");
2323+ default:
2424+ throw new Error(`Unsupported multibase: :${mb}`);
2525+ }
2626+};
2727+2828+export const bytesToMultibase = (
2929+ mb: Uint8Array,
3030+ encoding: SupportedEncodings,
3131+): string => {
3232+ switch (encoding) {
3333+ case "base16":
3434+ return "f" + toString(mb, encoding);
3535+ case "base16upper":
3636+ return "F" + toString(mb, encoding);
3737+ case "base32":
3838+ return "b" + toString(mb, encoding);
3939+ case "base32upper":
4040+ return "B" + toString(mb, encoding);
4141+ case "base58btc":
4242+ return "z" + toString(mb, encoding);
4343+ case "base64":
4444+ return "m" + toString(mb, encoding);
4545+ case "base64url":
4646+ return "u" + toString(mb, encoding);
4747+ case "base64urlpad":
4848+ return "U" + toString(mb, encoding);
4949+ default:
5050+ throw new Error(`Unsupported multibase: :${encoding}`);
5151+ }
5252+};
+22
crypto/p256/encoding.ts
···11+import { p256 } from "@noble/curves/nist.js";
22+import { toString } from "@atp/ui8";
33+44+export const compressPubkey = (pubkeyBytes: Uint8Array): Uint8Array => {
55+ // Check if key is already compressed (33 bytes starting with 0x02 or 0x03)
66+ if (
77+ pubkeyBytes.length === 33 &&
88+ (pubkeyBytes[0] === 0x02 || pubkeyBytes[0] === 0x03)
99+ ) {
1010+ return pubkeyBytes;
1111+ }
1212+ const point = p256.Point.fromHex(toString(pubkeyBytes, "hex"));
1313+ return point.toBytes(true);
1414+};
1515+1616+export const decompressPubkey = (compressed: Uint8Array): Uint8Array => {
1717+ if (compressed.length !== 33) {
1818+ throw new Error("Expected 33 byte compress pubkey");
1919+ }
2020+ const point = p256.Point.fromHex(toString(compressed, "hex"));
2121+ return point.toBytes(false);
2222+};
···11+/**
22+ * Returns a `Uint8Array` of the requested size. Referenced memory will
33+ * be initialized to 0.
44+ */
55+export function alloc(size: number = 0): Uint8Array {
66+ return new Uint8Array(size);
77+}
88+99+/**
1010+ * Where possible returns a Uint8Array of the requested size that references
1111+ * uninitialized memory. Only use if you are certain you will immediately
1212+ * overwrite every value in the returned `Uint8Array`.
1313+ */
1414+export function allocUnsafe(size: number = 0): Uint8Array {
1515+ return new Uint8Array(size);
1616+}
+66
ui8/compare.ts
···11+/**
22+ * Can be used with Array.sort to sort and array with Uint8Array entries
33+ */
44+export function compare(a: Uint8Array, b: Uint8Array): number {
55+ for (let i = 0; i < a.byteLength; i++) {
66+ if (a[i] < b[i]) {
77+ return -1;
88+ }
99+1010+ if (a[i] > b[i]) {
1111+ return 1;
1212+ }
1313+ }
1414+1515+ if (a.byteLength > b.byteLength) {
1616+ return 1;
1717+ }
1818+1919+ if (a.byteLength < b.byteLength) {
2020+ return -1;
2121+ }
2222+2323+ return 0;
2424+}
2525+2626+/**
2727+ * Returns true if the two passed Uint8Arrays have the same content
2828+ */
2929+export function equals(a: Uint8Array, b: Uint8Array): boolean {
3030+ if (a === b) {
3131+ return true;
3232+ }
3333+3434+ if (a.byteLength !== b.byteLength) {
3535+ return false;
3636+ }
3737+3838+ for (let i = 0; i < a.byteLength; i++) {
3939+ if (a[i] !== b[i]) {
4040+ return false;
4141+ }
4242+ }
4343+4444+ return true;
4545+}
4646+4747+/**
4848+ * Compares two Uint8Arrays representing two xor distances. Returns `-1` if `a`
4949+ * is a lower distance, `1` if `b` is a lower distance or `0` if the distances
5050+ * are equal.
5151+ */
5252+export function xorCompare(a: Uint8Array, b: Uint8Array): -1 | 0 | 1 {
5353+ if (a.byteLength !== b.byteLength) {
5454+ throw new Error("Inputs should have the same length");
5555+ }
5656+5757+ for (let i = 0; i < a.byteLength; i++) {
5858+ if (a[i] === b[i]) {
5959+ continue;
6060+ }
6161+6262+ return a[i] < b[i] ? -1 : 1;
6363+ }
6464+6565+ return 0;
6666+}
+21
ui8/concat.ts
···11+import { allocUnsafe } from "./alloc.ts";
22+import { asUint8Array } from "./util.ts";
33+44+/**
55+ * Returns a new Uint8Array created by concatenating the passed Uint8Arrays
66+ */
77+export function concat(arrays: Uint8Array[], length?: number): Uint8Array {
88+ if (length == null) {
99+ length = arrays.reduce((acc, curr) => acc + curr.length, 0);
1010+ }
1111+1212+ const output = allocUnsafe(length);
1313+ let offset = 0;
1414+1515+ for (const arr of arrays) {
1616+ output.set(arr, offset);
1717+ offset += arr.length;
1818+ }
1919+2020+ return asUint8Array(output);
2121+}
···11+import { allocUnsafe } from "./alloc.ts";
22+import { asUint8Array } from "./util.ts";
33+44+/**
55+ * Returns the xor distance between two Uint8Arrays
66+ */
77+export function xor(a: Uint8Array, b: Uint8Array): Uint8Array {
88+ if (a.length !== b.length) {
99+ throw new Error("Inputs should have the same length");
1010+ }
1111+1212+ const result = allocUnsafe(a.length);
1313+1414+ for (let i = 0; i < a.length; i++) {
1515+ result[i] = a[i] ^ b[i];
1616+ }
1717+1818+ return asUint8Array(result);
1919+}