Skip to content

Commit

Permalink
Merge pull request #175 from xmtp/rygine/crypto
Browse files Browse the repository at this point in the history
  • Loading branch information
rygine authored Jan 30, 2024
2 parents b38de4c + 8b21c05 commit 679621c
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 42 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-planets-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@xmtp/frames-client": patch
---

Updated crypto imports, build, and exports
4 changes: 3 additions & 1 deletion packages/frames-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
"author": "XMTP Labs <eng@xmtp.com>",
"license": "MIT",
"type": "module",
"browser": "lib/index.js",
"browser": "lib/browser/index.js",
"module": "lib/index.js",
"types": "lib/index.d.ts",
"exports": {
".": {
"types": "./lib/index.d.ts",
"browser": "./lib/browser/index.js",
"default": "./lib/index.js"
}
},
Expand Down Expand Up @@ -59,6 +60,7 @@
"devDependencies": {
"@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-typescript": "^11.1.6",
"@xmtp/rollup-plugin-resolve-extensions": "^1.0.1",
"@xmtp/tsconfig": "workspace:*",
"@xmtp/xmtp-js": "^11.3.7",
"eslint": "^8.56.0",
Expand Down
36 changes: 27 additions & 9 deletions packages/frames-client/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ import { dts } from "rollup-plugin-dts";
import tsConfigPaths from "rollup-plugin-tsconfig-paths";
import terser from "@rollup/plugin-terser";
import filesize from "rollup-plugin-filesize";
import { resolveExtensions } from "@xmtp/rollup-plugin-resolve-extensions";

const plugins = [
tsConfigPaths(),
typescript({
declaration: false,
declarationMap: false,
}),
filesize({
showMinifiedSize: false,
}),
];

const external = ["@xmtp/proto", "node:crypto"];

export default defineConfig([
{
Expand All @@ -13,18 +27,22 @@ export default defineConfig([
format: "es",
sourcemap: true,
},
external,
plugins,
},
{
input: "src/index.ts",
output: {
file: "lib/browser/index.js",
format: "es",
sourcemap: true,
},
external,
plugins: [
tsConfigPaths(),
typescript({
declaration: false,
declarationMap: false,
}),
resolveExtensions({ extensions: [".browser"] }),
terser(),
filesize({
showMinifiedSize: false,
}),
...plugins,
],
external: [],
},
{
input: "src/index.ts",
Expand Down
1 change: 1 addition & 0 deletions packages/frames-client/src/crypto.browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const { crypto } = window;
25 changes: 1 addition & 24 deletions packages/frames-client/src/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1 @@
let cachedCrypto: Crypto | undefined;

async function getCrypto(): Promise<Crypto> {
if (typeof crypto !== "undefined") {
return crypto;
}

if (typeof cachedCrypto !== "undefined") {
return cachedCrypto;
}

if (typeof window !== "undefined" && window.crypto) {
return window.crypto;
}

cachedCrypto = (await import("crypto")).webcrypto as Crypto;

return cachedCrypto;
}

export async function sha256(data: Uint8Array): Promise<Uint8Array> {
const digest = await (await getCrypto()).subtle.digest("SHA-256", data);
return new Uint8Array(digest);
}
export { webcrypto as crypto } from "node:crypto";
2 changes: 1 addition & 1 deletion packages/frames-client/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Wallet } from "ethers";
import { frames, fetcher } from "@xmtp/proto";
import { it, expect, describe, beforeEach } from "vitest";
import { FramesClient } from ".";
import { sha256 } from "./crypto";
import { sha256 } from "./utils";

const { b64Decode } = fetcher;

Expand Down
2 changes: 1 addition & 1 deletion packages/frames-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Client } from "@xmtp/xmtp-js";
import { frames, fetcher } from "@xmtp/proto";
import { OG_PROXY_URL } from "./constants";
import type { FramePostPayload, FramesApiResponse } from "./types";
import { sha256 } from "./crypto";
import { sha256 } from "./utils";
import { v1ToV2Bundle } from "./converters";

const { b64Encode } = fetcher;
Expand Down
6 changes: 6 additions & 0 deletions packages/frames-client/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { crypto } from "./crypto";

export async function sha256(data: Uint8Array): Promise<Uint8Array> {
const digest = await crypto.subtle.digest("SHA-256", data);
return new Uint8Array(digest);
}
1 change: 0 additions & 1 deletion packages/frames-client/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const vitestConfig = defineVitestConfig({
test: {
globals: true,
environment: "happy-dom",
setupFiles: "./vitest.setup.ts",
},
});

Expand Down
5 changes: 0 additions & 5 deletions packages/frames-client/vitest.setup.ts

This file was deleted.

66 changes: 66 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4312,6 +4312,7 @@ __metadata:
"@rollup/plugin-terser": "npm:^0.4.4"
"@rollup/plugin-typescript": "npm:^11.1.6"
"@xmtp/proto": "npm:3.41.0-beta.2"
"@xmtp/rollup-plugin-resolve-extensions": "npm:^1.0.1"
"@xmtp/tsconfig": "workspace:*"
"@xmtp/xmtp-js": "npm:^11.3.7"
eslint: "npm:^8.56.0"
Expand Down Expand Up @@ -4462,6 +4463,19 @@ __metadata:
languageName: unknown
linkType: soft

"@xmtp/rollup-plugin-resolve-extensions@npm:^1.0.1":
version: 1.0.1
resolution: "@xmtp/rollup-plugin-resolve-extensions@npm:1.0.1"
dependencies:
find-up: "npm:^7.0.0"
typescript: "npm:^5.3.3"
peerDependencies:
rollup: ^2.14.0||^3.0.0||^4.0.0
typescript: ">=5"
checksum: dd65d94fb87a8bf7fcea5a6d386e898c1e27fc85d9a8a4990170490d7bf6c56c46d5c4f105e37d5e7ae5f9a4d6ffd077e491c26405d287b89ee065f9e87cb41d
languageName: node
linkType: hard

"@xmtp/tsconfig@workspace:*, @xmtp/tsconfig@workspace:shared/tsconfig":
version: 0.0.0-use.local
resolution: "@xmtp/tsconfig@workspace:shared/tsconfig"
Expand Down Expand Up @@ -7439,6 +7453,17 @@ __metadata:
languageName: node
linkType: hard

"find-up@npm:^7.0.0":
version: 7.0.0
resolution: "find-up@npm:7.0.0"
dependencies:
locate-path: "npm:^7.2.0"
path-exists: "npm:^5.0.0"
unicorn-magic: "npm:^0.1.0"
checksum: 7e6b08fbc05a10677e25e74bb0a020054a86b31d1806c5e6a9e32e75472bbf177210bc16e5f97453be8bda7ae2e3d97669dbb2901f8c30b39ce53929cbea6746
languageName: node
linkType: hard

"find-yarn-workspace-root2@npm:1.2.16":
version: 1.2.16
resolution: "find-yarn-workspace-root2@npm:1.2.16"
Expand Down Expand Up @@ -9204,6 +9229,15 @@ __metadata:
languageName: node
linkType: hard

"locate-path@npm:^7.2.0":
version: 7.2.0
resolution: "locate-path@npm:7.2.0"
dependencies:
p-locate: "npm:^6.0.0"
checksum: 1c6d269d4efec555937081be964e8a9b4a136319c79ca1d45ac6382212a8466113c75bd89e44521ca8ecd1c47fb08523b56eee5c0712bc7d14fec5f729deeb42
languageName: node
linkType: hard

"lodash.defaults@npm:^4.2.0":
version: 4.2.0
resolution: "lodash.defaults@npm:4.2.0"
Expand Down Expand Up @@ -10375,6 +10409,15 @@ __metadata:
languageName: node
linkType: hard

"p-limit@npm:^4.0.0":
version: 4.0.0
resolution: "p-limit@npm:4.0.0"
dependencies:
yocto-queue: "npm:^1.0.0"
checksum: 01d9d70695187788f984226e16c903475ec6a947ee7b21948d6f597bed788e3112cc7ec2e171c1d37125057a5f45f3da21d8653e04a3a793589e12e9e80e756b
languageName: node
linkType: hard

"p-limit@npm:^5.0.0":
version: 5.0.0
resolution: "p-limit@npm:5.0.0"
Expand Down Expand Up @@ -10402,6 +10445,15 @@ __metadata:
languageName: node
linkType: hard

"p-locate@npm:^6.0.0":
version: 6.0.0
resolution: "p-locate@npm:6.0.0"
dependencies:
p-limit: "npm:^4.0.0"
checksum: 2bfe5234efa5e7a4e74b30a5479a193fdd9236f8f6b4d2f3f69e3d286d9a7d7ab0c118a2a50142efcf4e41625def635bd9332d6cbf9cc65d85eb0718c579ab38
languageName: node
linkType: hard

"p-map@npm:^2.0.0":
version: 2.1.0
resolution: "p-map@npm:2.1.0"
Expand Down Expand Up @@ -10481,6 +10533,13 @@ __metadata:
languageName: node
linkType: hard

"path-exists@npm:^5.0.0":
version: 5.0.0
resolution: "path-exists@npm:5.0.0"
checksum: 8ca842868cab09423994596eb2c5ec2a971c17d1a3cb36dbf060592c730c725cd524b9067d7d2a1e031fef9ba7bd2ac6dc5ec9fb92aa693265f7be3987045254
languageName: node
linkType: hard

"path-is-absolute@npm:^1.0.0":
version: 1.0.1
resolution: "path-is-absolute@npm:1.0.1"
Expand Down Expand Up @@ -13200,6 +13259,13 @@ __metadata:
languageName: node
linkType: hard

"unicorn-magic@npm:^0.1.0":
version: 0.1.0
resolution: "unicorn-magic@npm:0.1.0"
checksum: 9b4d0e9809807823dc91d0920a4a4c0cff2de3ebc54ee87ac1ee9bc75eafd609b09d1f14495e0173aef26e01118706196b6ab06a75fe0841028b3983a8af313f
languageName: node
linkType: hard

"unique-filename@npm:^2.0.0":
version: 2.0.1
resolution: "unique-filename@npm:2.0.1"
Expand Down

0 comments on commit 679621c

Please sign in to comment.