Skip to content

Commit

Permalink
Force packages using Buffer to declare a dependency on it (#2199)
Browse files Browse the repository at this point in the history
A few of our packages (but, most impactfully, `@pcd/pod`) make use of
the `Buffer` class without declaring a dependency on it. This does not
trigger build errors, because in NodeJS `Buffer` is a built-in global.
However, in browser environments it does not exist. This causes code
using `Buffer` to crash in browser environments.

A workaround for this is to install a polyfill. However, the polyfill
for Buffer is just the NPM `buffer` package, which we can declare a
direct dependency on. This improves the experience for the user of our
packages, who is not forced to work out how to install polyfills, and so
on.

I've added an eslint rule to prevent use of the global `Buffer` object,
so you now have to import it directly from a package you've declared as
a dependency. This flagged up multiple other examples of use of `Buffer`
without a declared dependency, so I've fixed those too.
  • Loading branch information
robknight authored Jan 31, 2025
1 parent 8474f0d commit 5ad35f9
Show file tree
Hide file tree
Showing 41 changed files with 52 additions and 17 deletions.
1 change: 1 addition & 0 deletions apps/consumer-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@simplewebauthn/browser": "^7.2.0",
"@simplewebauthn/server": "^7.2.0",
"@zk-kit/eddsa-poseidon": "^1.1.0",
"buffer": "^6.0.3",
"dotenv": "^16.0.3",
"ethers": "^5.7.2",
"json-bigint": "^1.0.0",
Expand Down
1 change: 1 addition & 0 deletions apps/consumer-client/src/pages/examples/add-pcd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
verifyRegistrationResponse
} from "@simplewebauthn/server";
import { derivePublicKey } from "@zk-kit/eddsa-poseidon";
import { Buffer } from "buffer";
import { ethers } from "ethers";
import JSONBig from "json-bigint";
import { useEffect, useState } from "react";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { PODData, podToPODData } from "@parcnet-js/podspec";
import { POD, PODEntries, encodePrivateKey } from "@pcd/pod";
import { v3tov4Identity } from "@pcd/semaphore-identity-pcd";
import { Buffer } from "buffer";
import { ReactNode, useMemo } from "react";
import styled from "styled-components";
import { BottomModalHeader } from "../../../new-components/shared/BottomModal";
import { Button2 } from "../../../new-components/shared/Button";
import { Typography } from "../../../new-components/shared/Typography";
import { hideScrollCSS } from "../../../new-components/shared/utils";
import { useIdentityV3, useZappOrigin } from "../../../src/appHooks";
import { BANNER_HEIGHT } from "../../../src/sharedConstants";
import { useSyncE2EEStorage } from "../../../src/useSyncE2EEStorage";
import { AppContainer } from "../../shared/AppContainer";
import { displayPODValue } from "../../shared/uiUtil";
import { BottomModalHeader } from "../../../new-components/shared/BottomModal";
import { Typography } from "../../../new-components/shared/Typography";
import { Button2 } from "../../../new-components/shared/Button";
import { BANNER_HEIGHT } from "../../../src/sharedConstants";
import { hideScrollCSS } from "../../../new-components/shared/utils";

export function EmbeddedSignPODScreen({
entries,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { decodeQRPayload } from "@pcd/passport-ui";
import { ZKEdDSAEventTicketPCDPackage } from "@pcd/zk-eddsa-event-ticket-pcd";
import { Buffer } from "buffer";
import { useEffect, useState } from "react";
import { useQuery } from "../../../../../src/appHooks";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { serializeStorage } from "@pcd/passport-interface";
import { LinkButton } from "@pcd/passport-ui";
import { Buffer } from "buffer";
import { useCallback, useEffect, useState } from "react";
import { toast } from "react-hot-toast";
import {
Expand Down
1 change: 1 addition & 0 deletions apps/passport-client/new-components/shared/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { serializeStorage } from "@pcd/passport-interface";
import { Buffer } from "buffer";
import { useCallback, useEffect, useState } from "react";
import { css } from "styled-components";
import {
Expand Down
1 change: 1 addition & 0 deletions apps/passport-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"@types/react-swipeable-views": "^0.13.5",
"boring-avatars": "^1.10.1",
"broadcast-channel": "^5.3.0",
"buffer": "^6.0.3",
"csstype": "^3.1.3",
"dotenv": "^16.0.3",
"email-validator": "^2.0.4",
Expand Down
1 change: 1 addition & 0 deletions apps/passport-client/src/dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
} from "@pcd/semaphore-identity-pcd";
import { assertUnreachable, sleep } from "@pcd/util";
import { Identity } from "@semaphore-protocol/identity";
import { Buffer } from "buffer";
import * as localForage from "localforage";
import _ from "lodash";
import { createContext } from "react";
Expand Down
1 change: 1 addition & 0 deletions apps/passport-client/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from "@pcd/passport-interface";
import { splitPath } from "@pcd/pcd-collection";
import { sleep } from "@pcd/util";
import { Buffer } from "buffer";
import _ from "lodash";
import { v4 as uuid } from "uuid";
import { Dispatcher } from "./dispatch";
Expand Down
1 change: 1 addition & 0 deletions apps/passport-client/src/zapp/ZappServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
ticketToPOD
} from "@pcd/pod-ticket-pcd";
import { v3tov4Identity } from "@pcd/semaphore-identity-pcd";
import { Buffer } from "buffer";
import { appConfig } from "../appConfig";
import { StateContextValue } from "../dispatch";
import { EmbeddedScreenType } from "../embedded";
Expand Down
5 changes: 2 additions & 3 deletions apps/passport-server/test/user/testLogin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { arrayBufferToHexString } from "@pcd/passport-crypto";
import {
requestConfirmationEmail,
requestCreateNewUser,
Expand Down Expand Up @@ -87,10 +86,10 @@ export async function testLogin(
token = serverToken;
}

const salt = arrayBufferToHexString(randomBytes(32));
const salt = randomBytes(32).toString("hex");
let encryptionKey: string | undefined = undefined;
if (skipSetupPassword) {
encryptionKey = arrayBufferToHexString(randomBytes(32));
encryptionKey = randomBytes(32).toString("hex");
}

const newUserResult = await requestCreateNewUser(
Expand Down
5 changes: 2 additions & 3 deletions apps/passport-server/test/user/testUserSync.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
arrayBufferToHexString,
EncryptedPacket,
HexString,
passportDecrypt,
Expand Down Expand Up @@ -244,7 +243,7 @@ export async function testUserSyncKeyChangeNoRev(
const encryptionKey1 = await crypto.generateRandomKey();

const encryptionKey2 = await crypto.generateRandomKey();
const newSalt = arrayBufferToHexString(randomBytes(32));
const newSalt = randomBytes(32).toString("hex");

const plaintextData1 = {
test: "test",
Expand Down Expand Up @@ -355,7 +354,7 @@ export async function testUserSyncKeyChangeWithRev(
const encryptionKey1 = await crypto.generateRandomKey();

const encryptionKey2 = await crypto.generateRandomKey();
const newSalt = arrayBufferToHexString(randomBytes(32));
const newSalt = randomBytes(32).toString("hex");

const plaintextData1 = {
test: "test",
Expand Down
1 change: 1 addition & 0 deletions packages/lib/gpcircuits/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
},
"dependencies": {
"@pcd/pod": "0.5.0",
"buffer": "^6.0.3",
"fastfile": "0.0.20",
"snarkjs": "^0.7.4",
"url-join": "4.0.1"
Expand Down
1 change: 1 addition & 0 deletions packages/lib/gpcircuits/src/artifacts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// <reference types="./declarations/fastfile.d.ts" />
import { Buffer } from "buffer";
import * as fastfile from "fastfile";
import urljoin from "url-join";
import { CircuitArtifactPaths, CircuitDesc } from "./types";
Expand Down
1 change: 1 addition & 0 deletions packages/lib/passport-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
},
"dependencies": {
"@pcd/message-pcd": "0.5.0",
"buffer": "^6.0.3",
"medium-zoom": "^1.0.8",
"pako": "^2.1.0",
"qrcode": "^1.5.4",
Expand Down
1 change: 1 addition & 0 deletions packages/lib/passport-ui/src/QR.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Buffer } from "buffer";
import { gzip, ungzip } from "pako";
import * as QRCode from "qrcode";
import { useCallback, useEffect, useMemo, useState } from "react";
Expand Down
3 changes: 2 additions & 1 deletion packages/lib/pod/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@zk-kit/eddsa-poseidon": "^1.1.0",
"@zk-kit/lean-imt": "^2.2.2",
"@zk-kit/utils": "^1.2.1",
"buffer": "^6.0.3",
"js-sha256": "^0.10.1",
"poseidon-lite": "^0.3.0"
},
Expand All @@ -51,8 +52,8 @@
"@pcd/pcd-types": "0.15.0",
"@pcd/semaphore-identity-v3-wrapper": "0.1.0",
"@pcd/tsconfig": "0.15.0",
"@types/circomlibjs": "^0.1.6",
"@types/chai": "^4.3.5",
"@types/circomlibjs": "^0.1.6",
"@types/mocha": "^10.0.1",
"@zk-kit/baby-jubjub": "^1.0.3",
"circomlibjs": "^0.1.7",
Expand Down
1 change: 1 addition & 0 deletions packages/lib/pod/src/podCrypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
verifySignature
} from "@zk-kit/eddsa-poseidon";
import { BigNumber, leBigIntToBuffer, leBufferToBigInt } from "@zk-kit/utils";
import { Buffer } from "buffer";
import { sha256 } from "js-sha256";
import { poseidon1 } from "poseidon-lite/poseidon1";
import { poseidon2 } from "poseidon-lite/poseidon2";
Expand Down
1 change: 1 addition & 0 deletions packages/lib/pod/src/podUtil.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Buffer } from "buffer";
import { podValueToJSON } from "./podJSON";
import { PODEntries, PODValue, PODValueTuple } from "./podTypes";

Expand Down
1 change: 1 addition & 0 deletions packages/lib/pod/test/alternateCrypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { generateSnarkMessageHash } from "@pcd/util";
import { Point } from "@zk-kit/baby-jubjub";
import { Signature } from "@zk-kit/eddsa-poseidon";
import { BigNumber } from "@zk-kit/utils";
import { Buffer } from "buffer";
import { expect } from "chai";
import {
Eddsa as CLEddsa,
Expand Down
1 change: 1 addition & 0 deletions packages/lib/pod/test/common.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Identity as IdentityV3 } from "@pcd/semaphore-identity-v3-wrapper";
import { BABY_JUB_NEGATIVE_ONE } from "@pcd/util";
import { Point } from "@zk-kit/baby-jubjub";
import { Buffer } from "buffer";
import { PODEntries } from "../src";

// Key borrowed from https://github.com/iden3/circomlibjs/blob/4f094c5be05c1f0210924a3ab204d8fd8da69f49/test/eddsa.js#L103
Expand Down
1 change: 1 addition & 0 deletions packages/lib/pod/test/podChecks.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Buffer } from "buffer";
import { expect } from "chai";
import "mocha";
import {
Expand Down
1 change: 1 addition & 0 deletions packages/lib/pod/test/podCrypto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
derivePublicKey,
signMessage
} from "@zk-kit/eddsa-poseidon";
import { Buffer } from "buffer";
import { expect } from "chai";
import "mocha";
import { poseidon2 } from "poseidon-lite/poseidon2";
Expand Down
1 change: 1 addition & 0 deletions packages/lib/pod/test/podJSON.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Buffer } from "buffer";
import { expect } from "chai";
import "mocha";
import {
Expand Down
1 change: 1 addition & 0 deletions packages/pcd/eddsa-ticket-pcd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@pcd/eddsa-pcd": "0.10.0",
"@pcd/pcd-types": "0.15.0",
"@pcd/util": "0.9.0",
"buffer": "^6.0.3",
"chai": "^4.3.7",
"json-bigint": "^1.0.0",
"lodash": "^4.17.21",
Expand Down
1 change: 1 addition & 0 deletions packages/pcd/eddsa-ticket-pcd/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
numberToBigInt,
uuidToBigInt
} from "@pcd/util";
import { Buffer } from "buffer";
import urlJoin from "url-join";
import { EdDSATicketPCD, ITicketData } from "./EdDSATicketPCD";

Expand Down
1 change: 1 addition & 0 deletions packages/pcd/ethereum-group-pcd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@pcd/util": "0.9.0",
"@personaelabs/spartan-ecdsa": "^2.1.4",
"@semaphore-protocol/identity": "^3.15.2",
"buffer": "^6.0.3",
"ethers": "^5.7.2",
"json-bigint": "^1.0.0",
"uuid": "^9.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
ProverConfig,
PublicInput
} from "@personaelabs/spartan-ecdsa";
import { Buffer } from "buffer";
import { ethers } from "ethers";
import JSONBig from "json-bigint";
import { v4 as uuid } from "uuid";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { MerkleProof, Poseidon, Tree } from "@personaelabs/spartan-ecdsa";
import { Identity } from "@semaphore-protocol/identity";
import assert from "assert";
import { Buffer } from "buffer";
import { ethers } from "ethers";
import JSONBig from "json-bigint";
import "mocha";
Expand Down
1 change: 1 addition & 0 deletions packages/pcd/halo-nonce-pcd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"dependencies": {
"@pcd/pcd-types": "0.15.0",
"@pcd/util": "0.9.0",
"buffer": "^6.0.3",
"elliptic": "^6.5.4",
"js-sha256": "^0.10.1",
"uuid": "^9.0.0"
Expand Down
1 change: 1 addition & 0 deletions packages/pcd/halo-nonce-pcd/src/HaLoNoncePCDPackage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DisplayOptions, PCDPackage, SerializedPCD } from "@pcd/pcd-types";
import { requireDefinedParameter } from "@pcd/util";
import { Buffer } from "buffer";
import { sha256 } from "js-sha256";
import { v4 as uuid } from "uuid";
import {
Expand Down
1 change: 1 addition & 0 deletions packages/pcd/message-pcd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@pcd/pcd-types": "0.15.0",
"@pcd/semaphore-signature-pcd": "0.15.0",
"@trufflesuite/bigint-buffer": "^1.1.10",
"buffer": "^6.0.3",
"chai": "^4.3.7",
"json-bigint": "^1.0.0",
"uuid": "^9.0.0",
Expand Down
1 change: 1 addition & 0 deletions packages/pcd/message-pcd/src/utils/serialization.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { toBigIntBE, toBufferBE } from "@trufflesuite/bigint-buffer";
import { Buffer } from "buffer";
import { Message, MessageSchema } from "../Message";

export type MsgAsInt = {
Expand Down
1 change: 1 addition & 0 deletions packages/pcd/rsa-pcd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"dependencies": {
"@pcd/pcd-types": "0.15.0",
"@pcd/util": "0.9.0",
"buffer": "^6.0.3",
"chai": "^4.3.7",
"json-bigint": "^1.0.0",
"node-rsa": "^1.1.1",
Expand Down
1 change: 1 addition & 0 deletions packages/pcd/rsa-pcd/src/RSAPCDPackage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DisplayOptions, PCDPackage, SerializedPCD } from "@pcd/pcd-types";
import { requireDefinedParameter } from "@pcd/util";
import { Buffer } from "buffer";
import JSONBig from "json-bigint";
import { v4 as uuid } from "uuid";
import {
Expand Down
1 change: 1 addition & 0 deletions packages/pcd/semaphore-identity-pcd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@types/json-bigint": "^1.0.3",
"@zk-kit/eddsa-poseidon": "^1.1.0",
"@zk-kit/utils": "^1.2.1",
"buffer": "^6.0.3",
"js-sha256": "^0.11.0",
"json-bigint": "^1.0.0",
"poseidon-lite": "^0.3.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/pcd/semaphore-identity-pcd/src/v4IdentityUtils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { decodePublicKey, encodePrivateKey, encodePublicKey } from "@pcd/pod";
import { randomUUID } from "@pcd/util";
import { beBigIntToBuffer } from "@zk-kit/utils";
import { Buffer } from "buffer";
import { sha256 } from "js-sha256";
import { poseidon2 } from "poseidon-lite/poseidon2";
import { IdentityV3, IdentityV4 } from "./forwardedTypes";
import { SemaphoreIdentityPCD } from "./SemaphoreIdentityPCD";
import { IdentityV3, IdentityV4 } from "./forwardedTypes";

/**
* Given a semaphore v4 public key, returns the corresponding v4 identity commitment.
Expand Down
2 changes: 1 addition & 1 deletion packages/tools/eslint-config-custom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = {
message: "Manual timeouts in Mocha tests are not allowed."
}
],
"no-restricted-globals": ["error", "origin"]
"no-restricted-globals": ["error", "origin", "Buffer"]
},
settings: {
"import/resolver": {
Expand Down
1 change: 1 addition & 0 deletions packages/ui/pod-ticket-pcd-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@pcd/passport-ui": "0.15.0",
"@pcd/pcd-types": "0.15.0",
"@pcd/pod-ticket-pcd": "0.5.0",
"buffer": "^6.0.3",
"url-join": "4.0.1"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions packages/ui/pod-ticket-pcd-ui/src/CardBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { QRDisplayWithRegenerateAndStorage, styled } from "@pcd/passport-ui";
import { PCDUI } from "@pcd/pcd-types";
import { PODTicketPCD } from "@pcd/pod-ticket-pcd";
import { IPODTicketData } from "@pcd/pod-ticket-pcd/src/schema";
import { Buffer } from "buffer";
import { useCallback, useRef } from "react";
import urlJoin from "url-join";

Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -20067,9 +20067,9 @@ semver@^6.3.0, semver@^6.3.1:
integrity sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==

semver@^7.6.0:
version "7.6.3"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.6.3.tgz#980f7b5550bc175fb4dc09403085627f9eb33143"
integrity sha512-oVekP1cKtI+CTDvHWYFUcMtsK/00wmAEfyqKfNdARm8u1wNVhSgaX7A8d4UuIlUI5e84iEwOhs7ZPYRmzU9U6A==
version "7.7.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.7.0.tgz#9c6fe61d0c6f9fa9e26575162ee5a9180361b09c"
integrity sha512-DrfFnPzblFmNrIZzg5RzHegbiRWg7KMR7btwi2yjHwx06zsUbO5g613sVwEV7FTwmzJu+Io0lJe2GJ3LxqpvBQ==

send@0.18.0:
version "0.18.0"
Expand Down

0 comments on commit 5ad35f9

Please sign in to comment.