Skip to content

Commit

Permalink
chore(transport): change from lzw to msgpack
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcnk committed Oct 3, 2024
1 parent 47809dd commit fe385d8
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 215 deletions.
Binary file modified bun.lockb
Binary file not shown.
109 changes: 52 additions & 57 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,59 +1,54 @@
{
"name": "silex",
"version": "0.0.1",
"type": "module",
"private": true,
"scripts": {
"dev": "bunx --bun vite dev",
"build": "bunx --bun vite build",
"preview": "bunx --bun vite preview",
"test": "bun test",
"lint": "bunx biome check .",
"format": "bunx biome check . --write --unsafe",
"cleanup": "rimraf .svelte-kit node_modules"
},
"devDependencies": {
"@biomejs/biome": "1.9.2",
"@sveltejs/adapter-static": "^3.0.5",
"@sveltejs/kit": "^2.0.0",
"@sveltejs/vite-plugin-svelte": "^3.0.0",
"@tailwindcss/typography": "^0.5.14",
"@types/bun": "^1.1.10",
"@types/webappsec-credential-management": "^0.6.8",
"@vite-pwa/sveltekit": "^0.6.5",
"autoprefixer": "^10.4.20",
"daisyui": "^4.12.10",
"postcss": "^8.4.47",
"rimraf": "^6.0.1",
"svelte": "^4.2.7",
"svelte-check": "^4.0.0",
"tailwindcss": "^3.4.9",
"typescript": "^5.0.0",
"vite": "^5.0.3",
"zustand": "^5.0.0-rc.2"
},
"dependencies": {
"@blu3r4y/lzma": "2.3.3",
"@castlenine/svelte-qrcode": "^2.2.0",
"@mina-js/accounts": "https://pkg.pr.new/palladians/mina-js/@mina-js/accounts@8107d7d",
"@noble/hashes": "^1.5.0",
"@scure/base": "^1.1.9",
"@scure/bip32": "^1.5.0",
"@scure/bip39": "^1.4.0",
"compress-json": "^3.1.0",
"html5-qrcode": "^2.3.8",
"immer": "^10.1.1",
"lucide-svelte": "^0.446.0",
"lzw-compressor": "^21.6.5",
"mina-signer": "^3.0.7",
"minizlib": "^3.0.1",
"nanoid": "^5.0.7",
"o1js": "^1.8.0",
"svelte-sonner": "^0.3.28",
"toml": "^3.0.0",
"ts-pattern": "^5.4.0",
"zipson": "^0.2.12",
"zipurl": "^1.0.2",
"zod": "^3.23.8"
}
"name": "silex",
"version": "0.0.1",
"type": "module",
"private": true,
"scripts": {
"dev": "bunx --bun vite dev",
"build": "bunx --bun vite build",
"preview": "bunx --bun vite preview",
"test": "bun test",
"lint": "bunx biome check .",
"format": "bunx biome check . --write --unsafe",
"cleanup": "rimraf .svelte-kit node_modules"
},
"devDependencies": {
"@biomejs/biome": "1.9.2",
"@sveltejs/adapter-static": "^3.0.5",
"@sveltejs/kit": "^2.0.0",
"@sveltejs/vite-plugin-svelte": "^3.0.0",
"@tailwindcss/typography": "^0.5.14",
"@types/bun": "^1.1.10",
"@types/webappsec-credential-management": "^0.6.8",
"@vite-pwa/sveltekit": "^0.6.5",
"autoprefixer": "^10.4.20",
"daisyui": "^4.12.10",
"postcss": "^8.4.47",
"rimraf": "^6.0.1",
"svelte": "^4.2.7",
"svelte-check": "^4.0.0",
"tailwindcss": "^3.4.9",
"typescript": "^5.0.0",
"vite": "^5.0.3",
"zustand": "^5.0.0-rc.2"
},
"dependencies": {
"@castlenine/svelte-qrcode": "^2.2.0",
"@mina-js/accounts": "https://pkg.pr.new/palladians/mina-js/@mina-js/accounts@8107d7d",
"@msgpack/msgpack": "^3.0.0-beta2",
"@noble/hashes": "^1.5.0",
"@scure/base": "^1.1.9",
"@scure/bip32": "^1.5.0",
"@scure/bip39": "^1.4.0",
"html5-qrcode": "^2.3.8",
"immer": "^10.1.1",
"lucide-svelte": "^0.446.0",
"mina-signer": "^3.0.7",
"nanoid": "^5.0.7",
"o1js": "^1.8.0",
"svelte-sonner": "^0.3.28",
"toml": "^3.0.0",
"ts-pattern": "^5.4.0",
"zod": "^3.23.8"
}
}
124 changes: 62 additions & 62 deletions src/lib/transport.spec.ts
Original file line number Diff line number Diff line change
@@ -1,91 +1,91 @@
import { expect, it } from "bun:test";
import type { MinaUnsignedTransaction } from "./signers/mina";
import {
getAccountFromTransportable,
getTransportableAccount,
signRequestToTransportable,
signatureToTransportable,
transportableToSignRequest,
transportableToSignature,
getAccountFromTransportable,
getTransportableAccount,
signRequestToTransportable,
signatureToTransportable,
transportableToSignRequest,
transportableToSignature,
} from "./transport";
import { pathToArray } from "./utils";

const testAccount = {
address: "B62qkYa1o6Mj6uTTjDQCob7FYZspuhkm4RRQhgJg9j4koEBWiSrTQrS",
derivationPath: pathToArray("m/44'/12586'/0'/0/0"),
signer: 2,
address: "B62qkYa1o6Mj6uTTjDQCob7FYZspuhkm4RRQhgJg9j4koEBWiSrTQrS",
derivationPath: pathToArray("m/44'/12586'/0'/0/0"),
signer: 2,
};

it("creates decodeable transport data", async () => {
const encoded = getTransportableAccount(testAccount);
expect(encoded).toEqual(
"eyJhZGRyZXNzIjoiQjYycWtZYTFvNk1qNnVUVGpEUUNvYjdGWVpzcHVoa200UlJRaGdKZzlqNGtvRUJXaVNyVFFyUyIsInNpZ25lcsSJMsWEZMWKaXZhdGlvblDFk2jEiVsyMTQ3NDgzNjnFjcWdxZ80OcSNMzQsxafFoMWiNsWhLDDFtF19",
);
const encoded = getTransportableAccount(testAccount);
expect(encoded).toEqual(
"g6dhZGRyZXNz2TdCNjJxa1lhMW82TWo2dVRUakRRQ29iN0ZZWnNwdWhrbTRSUlFoZ0pnOWo0a29FQldpU3JUUXJTpnNpZ25lcgKuZGVyaXZhdGlvblBhdGiVzoAAACzOgAAxKs6AAAAAAAA=",
);
});

it("decodes transport data", async () => {
const mockData =
"eyJhZGRyZXNzIjoiQjYycWtZYTFvNk1qNnVUVGpEUUNvYjdGWVpzcHVoa200UlJRaGdKZzlqNGtvRUJXaVNyVFFyUyIsInNpZ25lcsSJMsWEZMWKaXZhdGlvblDFk2jEiVsyMTQ3NDgzNjnFjcWdxZ80OcSNMzQsxafFoMWiNsWhLDDFtF19";
const account = getAccountFromTransportable(mockData);
expect(account.address).toEqual(testAccount.address);
const mockData =
"g6dhZGRyZXNz2TdCNjJxa1lhMW82TWo2dVRUakRRQ29iN0ZZWnNwdWhrbTRSUlFoZ0pnOWo0a29FQldpU3JUUXJTpnNpZ25lcgKuZGVyaXZhdGlvblBhdGiVzoAAACzOgAAxKs6AAAAAAAA=";
const account = getAccountFromTransportable(mockData);
expect(account.address).toEqual(testAccount.address);
});

it("converts sign request to transportable", () => {
const signRequest = {
payload: "Bonjour",
derivationPath: pathToArray("m/44'/12586'/0'/0/0"),
signer: 2,
type: 0,
options: [0],
};
const result = signRequestToTransportable(signRequest);
expect(result).toEqual(
"eyJwYXlsb2FkIjoiQm9uam91ciIsImRlcml2YXRpxI1QxJtoxIlbMjE0NzQ4MzY5MizEpMSmNDk2MjM0xK7EpcSnxKk2xKgsMMS9XcSUc2lnbsSXxInErSJ0eXBlxInEviJvcMScxI1zxKIwXX0=",
);
const signRequest = {
payload: "Bonjour",
derivationPath: pathToArray("m/44'/12586'/0'/0/0"),
signer: 2,
type: 0,
options: [0],
};
const result = signRequestToTransportable(signRequest);
expect(result).toEqual(
"hadwYXlsb2Fkp0JvbmpvdXKuZGVyaXZhdGlvblBhdGiVzoAAACzOgAAxKs6AAAAAAACmc2lnbmVyAqR0eXBlAKdvcHRpb25zkQA=",
);
});

it("converts sign request to transportable for transaction", () => {
const txPayload: MinaUnsignedTransaction = {
from: "B62qkYa1o6Mj6uTTjDQCob7FYZspuhkm4RRQhgJg9j4koEBWiSrTQrS",
to: "B62qkYa1o6Mj6uTTjDQCob7FYZspuhkm4RRQhgJg9j4koEBWiSrTQrS",
fee: "10000000",
nonce: "1",
amount: "1000000000",
memo: "Hello",
};
const signRequest = {
payload: txPayload,
derivationPath: pathToArray("m/44'/12586'/0'/0/0"),
signer: 2,
type: 1,
options: [0],
};
const result = signRequestToTransportable(signRequest);
expect(result).toEqual(
"eyJwYXlsb2FkIjrEgGZyb23EiSJCNjJxa1lhMW82TWo2dVRUakRRQ29iN0ZZWnNwdWhrbTRSUlFoZ0pnOWo0a29FQldpU3JUUXJTIiwidG/EkMSSxJTElsSYxJrEnMSexKDEosSkxKbEqMSqxKzErsSwxLLEtMS2xLjEusS8xL7FgMWCxYTFhsWIxYpmZWXEkDEwxbHFsjDFiSJub25jxa46IjHFtWFtb3VudMWvxbPGh8W1bWXGgcSQSGVsxIUifcWKZGVyaXZhdGnFuFDGmmjEiVsyMTQ3NDgzNjkyLMajxqU0OcSTMzTGrcakxqbGqDbGpywwxrtdxYpzaWduxpbEicasxYt5cMW7McWKb3DGm8W4c8ahMF19",
);
const txPayload: MinaUnsignedTransaction = {
from: "B62qkYa1o6Mj6uTTjDQCob7FYZspuhkm4RRQhgJg9j4koEBWiSrTQrS",
to: "B62qkYa1o6Mj6uTTjDQCob7FYZspuhkm4RRQhgJg9j4koEBWiSrTQrS",
fee: "10000000",
nonce: "1",
amount: "1000000000",
memo: "Hello",
};
const signRequest = {
payload: txPayload,
derivationPath: pathToArray("m/44'/12586'/0'/0/0"),
signer: 2,
type: 1,
options: [0],
};
const result = signRequestToTransportable(signRequest);
expect(result).toEqual(
"hadwYXlsb2FkhqRmcm9t2TdCNjJxa1lhMW82TWo2dVRUakRRQ29iN0ZZWnNwdWhrbTRSUlFoZ0pnOWo0a29FQldpU3JUUXJTonRv2TdCNjJxa1lhMW82TWo2dVRUakRRQ29iN0ZZWnNwdWhrbTRSUlFoZ0pnOWo0a29FQldpU3JUUXJTo2ZlZagxMDAwMDAwMKVub25jZaExpmFtb3VudKoxMDAwMDAwMDAwpG1lbW+lSGVsbG+uZGVyaXZhdGlvblBhdGiVzoAAACzOgAAxKs6AAAAAAACmc2lnbmVyAqR0eXBlAadvcHRpb25zkQA=",
);
});

it("decodes sign request", () => {
const tranportable =
"eyJwYXlsb2FkIjoiQm9uam91ciIsImRlcml2YXRpxI1QxJtoxIlbMjE0NzQ4MzY5MizEpMSmNDk2MjM0xK7EpcSnxKk2xKgsMMS9XcSUc2lnbsSXxInErSJ0eXBlxInEviJvcMScxI1zxKIwXX0=";
const signRequest = transportableToSignRequest(tranportable);
expect(signRequest.payload).toEqual("Bonjour");
const tranportable =
"hadwYXlsb2Fkp0JvbmpvdXKuZGVyaXZhdGlvblBhdGiVzoAAACzOgAAxKs6AAAAAAACmc2lnbmVyAqR0eXBlAKdvcHRpb25zkQA=";
const signRequest = transportableToSignRequest(tranportable);
expect(signRequest.payload).toEqual("Bonjour");
});

it("converts signature to transportable", () => {
const transportable = signatureToTransportable({
signature: "signature",
publicKey: "publicKey",
});
expect(transportable).toEqual(
"eyJzaWduYXR1cmUiOsSBxIPEhcSHxIkiLCJwdWJsaWNLZXnEi8SUxJbEmMSaxJwifQ==",
);
const transportable = signatureToTransportable({
signature: "signature",
publicKey: "publicKey",
});
expect(transportable).toEqual(
"gqlzaWduYXR1cmWpc2lnbmF0dXJlqXB1YmxpY0tlealwdWJsaWNLZXk=",
);
});

it("decodes signature", () => {
const transportable =
"eyJzaWduYXR1cmUiOsSBxIPEhcSHxIkiLCJwdWJsaWNLZXnEi8SUxJbEmMSaxJwifQ==";
const signature = transportableToSignature(transportable);
expect(signature.signature).toEqual("signature");
const transportable =
"gqlzaWduYXR1cmWpc2lnbmF0dXJlqXB1YmxpY0tlealwdWJsaWNLZXk=";
const signature = transportableToSignature(transportable);
expect(signature.signature).toEqual("signature");
});
58 changes: 29 additions & 29 deletions src/lib/transport.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,58 @@
import { base64, bytesToString, stringToBytes } from "@scure/base";
import { compress, decompress } from "lzw-compressor";
import { base64 } from "@scure/base";
import { encode, decode } from "@msgpack/msgpack";
import type {
TransportableAccount,
TransportableSignRequest,
TransportableSignature,
TransportableAccount,
TransportableSignRequest,
TransportableSignature,
} from "./types";
import {
TransportableAccountSchema,
TransportableSignRequestSchema,
TransportableSignatureSchema,
TransportableAccountSchema,
TransportableSignRequestSchema,
TransportableSignatureSchema,
} from "./validation";

const _base64AndCompress = (data: object) => {
const compressed = compress(JSON.stringify(data));
return base64.encode(stringToBytes("utf8", compressed));
const _base64AndCompress = (data: object): string => {
const packed = encode(data);
return base64.encode(packed);
};

const _decompressAndDebase64 = (encoded: string) => {
const encodedString = bytesToString("utf8", base64.decode(encoded));
return JSON.parse(decompress(encodedString));
const _decompressAndDebase64 = (encoded: string): Record<string, unknown> => {
const debased = base64.decode(encoded);
return decode(debased) as Record<string, unknown>;
};

export const getTransportableAccount = (account: object) => {
return _base64AndCompress(TransportableAccountSchema.parse(account));
return _base64AndCompress(TransportableAccountSchema.parse(account));
};

export const getAccountFromTransportable = (
encoded: string,
encoded: string,
): TransportableAccount => {
const accountPayload = _decompressAndDebase64(encoded);
return TransportableAccountSchema.parse({
address: accountPayload.address,
derivationPath: accountPayload.derivationPath,
signer: accountPayload.signer,
});
const accountPayload = _decompressAndDebase64(encoded);
return TransportableAccountSchema.parse({
address: accountPayload.address,
derivationPath: accountPayload.derivationPath,
signer: accountPayload.signer,
});
};

export const signRequestToTransportable = (payload: object) => {
return _base64AndCompress(TransportableSignRequestSchema.parse(payload));
return _base64AndCompress(TransportableSignRequestSchema.parse(payload));
};

export const transportableToSignRequest = (
encoded: string,
encoded: string,
): TransportableSignRequest => {
const signRequest = _decompressAndDebase64(encoded);
return TransportableSignRequestSchema.parse(signRequest);
const signRequest = _decompressAndDebase64(encoded);
return TransportableSignRequestSchema.parse(signRequest);
};

export const signatureToTransportable = (payload: object) => {
return _base64AndCompress(TransportableSignatureSchema.parse(payload));
return _base64AndCompress(TransportableSignatureSchema.parse(payload));
};

export const transportableToSignature = (
encoded: string,
encoded: string,
): TransportableSignature => {
return TransportableSignatureSchema.parse(_decompressAndDebase64(encoded));
return TransportableSignatureSchema.parse(_decompressAndDebase64(encoded));
};
2 changes: 1 addition & 1 deletion src/routes/(app)/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script>
import BottomNavigation from "$lib/components/bottom-navigation.svelte";
import BottomNavigation from "$lib/components/bottom-navigation.svelte";
</script>

<div class="flex flex-1 flex-col min-h-screen py-20">
Expand Down
1 change: 1 addition & 0 deletions src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ssr = false;
Loading

0 comments on commit fe385d8

Please sign in to comment.