Skip to content

Commit

Permalink
Merge pull request #231 from xmtp/ar/consent-proof-signature-package
Browse files Browse the repository at this point in the history
Add consent proof message package
  • Loading branch information
alexrisch authored May 6, 2024
2 parents 40d74b3 + 6fbe931 commit 654b089
Show file tree
Hide file tree
Showing 13 changed files with 314 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/beige-camels-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@xmtp/consent-proof-signature": patch
---

Created package
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ To learn more about the contents of this repository, see this README and the REA
- [`eslint-config-xmtp-web`](https://github.com/xmtp/xmtp-web/blob/main/packages/eslint-config-xmtp-web): An opinionated ESLint configuration for XMTP web projects
- [`react-sdk`](https://github.com/xmtp/xmtp-web/blob/main/packages/react-sdk): XMTP React client SDK
- [`frames-client`](https://github.com/xmtp/xmtp-web/blob/main/packages/frames-client): XMTP Open Frames client
- [`consent-proof-signature`](https://github.com/xmtp/xmtp-web/blob/main/packages/consent-proof-signature): Lightweight package for creating consent proofs


### Examples

Expand Down
7 changes: 7 additions & 0 deletions packages/consent-proof-signature/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
root: true,
extends: ["xmtp-web"],
parserOptions: {
project: "./tsconfig.eslint.json",
},
};
23 changes: 23 additions & 0 deletions packages/consent-proof-signature/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Consent Proof Signature

## Usage

```ts
const timestamp = Date.now();
const message = createConsentMessage(broadcastAddress, timestamp);
// Sign the message for example with Viem
import { createWalletClient, custom } from "viem";

const walletClient = createWalletClient({
chain: mainnet,
transport: custom((window as any).ethereum!),
});
const [account] = await walletClient.getAddresses();
const signature = await walletClient.signMessage({
account,
message,
});
const consentProofBytes = createConsentProofPayload(signature, timestamp);
```

Now the consentProofBytes can be encoded and sent to a service to decode and add in a new conversation invitation
97 changes: 97 additions & 0 deletions packages/consent-proof-signature/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"name": "@xmtp/consent-proof-signature",
"version": "0.1.0",
"keywords": [
"xmtp",
"messaging",
"web3",
"sdk",
"js",
"ts",
"javascript",
"typescript"
],
"homepage": "https://github.com/xmtp/xmtp-web",
"bugs": {
"url": "https://github.com/xmtp/xmtp-web/issues"
},
"repository": {
"type": "git",
"url": "git@github.com:xmtp/xmtp-web.git",
"directory": "packages/consent-proof-signature"
},
"license": "MIT",
"author": "XMTP Labs <eng@xmtp.com>",
"sideEffects": false,
"type": "module",
"exports": {
".": {
"types": "./lib/index.d.ts",
"default": "./lib/index.js"
}
},
"main": "index.js",
"module": "lib/index.js",
"browser": "lib/index.js",
"types": "lib/index.d.ts",
"files": [
"lib",
"src",
"!src/**/*.test.*",
"tsconfig.json"
],
"scripts": {
"build": "yarn clean:lib && yarn rollup -c",
"clean": "rm -rf .turbo && rm -rf node_modules && yarn clean:lib",
"clean:lib": "rm -rf lib",
"dev": "yarn clean:lib && yarn rollup -c --watch",
"format": "yarn format:base -w .",
"format:base": "prettier --ignore-path ../../.gitignore",
"format:check": "yarn format:base -c .",
"lint": "eslint . --ignore-path ../../.gitignore",
"test": "vitest run --passWithNoTests",
"typecheck": "tsc",
"typedoc": "typedoc"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 3 chrome versions",
"last 3 firefox versions",
"last 3 safari versions"
]
},
"dependencies": {
"@xmtp/proto": "3.56.0",
"long": "^5.2.3"
},
"devDependencies": {
"@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-typescript": "^11.1.6",
"@xmtp/tsconfig": "workspace:*",
"eslint": "^8.57.0",
"eslint-config-xmtp-web": "workspace:*",
"ethers": "^6.11.1",
"prettier": "^3.2.5",
"prettier-plugin-packagejson": "^2.4.12",
"rollup": "^4.13.0",
"rollup-plugin-dts": "^6.1.0",
"rollup-plugin-filesize": "^10.0.0",
"rollup-plugin-tsconfig-paths": "^1.5.2",
"typedoc": "^0.25.12",
"typescript": "^5.4.2",
"vite": "^5.1.6",
"vite-tsconfig-paths": "^4.3.1",
"vitest": "^1.3.1"
},
"packageManager": "yarn@4.0.2",
"publishConfig": {
"access": "public",
"provenance": true,
"registry": "https://registry.npmjs.org/"
}
}
50 changes: 50 additions & 0 deletions packages/consent-proof-signature/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { defineConfig } from "rollup";
import typescript from "@rollup/plugin-typescript";
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";

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

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

export default defineConfig([
{
input: "src/index.ts",
output: {
file: "lib/index.js",
format: "es",
sourcemap: true,
},
external,
plugins,
},
{
input: "src/index.ts",
output: {
file: "lib/browser/index.js",
format: "es",
sourcemap: true,
},
external,
plugins: [terser(), ...plugins],
},
{
input: "src/index.ts",
output: {
file: "lib/index.d.ts",
format: "es",
},
plugins: [tsConfigPaths(), dts()],
},
]);
30 changes: 30 additions & 0 deletions packages/consent-proof-signature/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { describe, expect, it } from "vitest";
import { createConsentMessage, createConsentProofPayload } from ".";

describe("createConsentMessage", () => {
it("should return a signature", () => {
const timestampMs = 1581663600000;
const exampleAddress = "0x1234567890abcdef";
const signatureMessage = createConsentMessage(exampleAddress, timestampMs);
expect(signatureMessage).toEqual(
"XMTP : Grant inbox consent to sender\n\nCurrent Time: Fri, 14 Feb 2020 07:00:00 GMT\nFrom Address: 0x1234567890abcdef\n\nFor more info: https://xmtp.org/signatures/",
);
});
});

describe("createConsentProofPayload", () => {
it("should return data of consent proof", () => {
const timestampMs = 1581663600000;
const exampleSignature = "0x1234567890abcdef";
const signatureMessage = createConsentProofPayload(
exampleSignature,
timestampMs,
);
expect(signatureMessage).toEqual(
Buffer.from([
10, 18, 48, 120, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 97, 98, 99,
100, 101, 102, 16, 128, 251, 252, 147, 132, 46, 24, 1,
]),
);
});
});
35 changes: 35 additions & 0 deletions packages/consent-proof-signature/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Long from "long";
import { invitation } from "@xmtp/proto";
/**
*
* @param peerAddress - Ethereum address of the broadcaster
* @param timestampMs - Timestamp in milliseconds used in the signature
* @returns
*/
export const createConsentMessage = (
peerAddress: string,
timestampMs: number,
): string =>
"XMTP : Grant inbox consent to sender\n" +
"\n" +
`Current Time: ${new Date(timestampMs).toUTCString()}\n` +
`From Address: ${peerAddress}\n` +
"\n" +
"For more info: https://xmtp.org/signatures/";

/**
*
* @param signature hex string of the signature
* @param timestampMs timestamp in milliseconds used in the signature
* @returns Uint8Array of the consent proof payload
*/
export const createConsentProofPayload = (
signature: string,
timestampMs: number,
): Uint8Array =>
invitation.ConsentProofPayload.encode({
signature,
timestamp: Long.fromNumber(timestampMs),
payloadVersion:
invitation.ConsentProofPayloadVersion.CONSENT_PROOF_PAYLOAD_VERSION_1,
}).finish();
5 changes: 5 additions & 0 deletions packages/consent-proof-signature/tsconfig.eslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "./tsconfig.json",
"include": [".", ".eslintrc.cjs", "rollup.config.js"],
"exclude": ["lib", "node_modules"]
}
4 changes: 4 additions & 0 deletions packages/consent-proof-signature/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "@xmtp/tsconfig/react-sdk.json",
"include": ["src", ".eslintrc.cjs", "vitest.config.ts", "vitest.setup.ts"]
}
17 changes: 17 additions & 0 deletions packages/consent-proof-signature/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { defineConfig, mergeConfig } from "vite";
import { defineConfig as defineVitestConfig } from "vitest/config";
import tsconfigPaths from "vite-tsconfig-paths";

// https://vitejs.dev/config/
const viteConfig = defineConfig({
plugins: [tsconfigPaths()],
});

const vitestConfig = defineVitestConfig({
test: {
globals: true,
environment: "happy-dom",
},
});

export default mergeConfig(viteConfig, vitestConfig);
2 changes: 1 addition & 1 deletion packages/frames-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"repository": {
"type": "git",
"url": "git@github.com:xmtp/xmtp-web.git",
"directory": "packages/react-sdk"
"directory": "packages/frames-client"
},
"license": "MIT",
"author": "XMTP Labs <eng@xmtp.com>",
Expand Down
38 changes: 38 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5367,6 +5367,32 @@ __metadata:
languageName: node
linkType: hard

"@xmtp/consent-proof-signature@workspace:packages/consent-proof-signature":
version: 0.0.0-use.local
resolution: "@xmtp/consent-proof-signature@workspace:packages/consent-proof-signature"
dependencies:
"@rollup/plugin-terser": "npm:^0.4.4"
"@rollup/plugin-typescript": "npm:^11.1.6"
"@xmtp/proto": "npm:3.56.0"
"@xmtp/tsconfig": "workspace:*"
eslint: "npm:^8.57.0"
eslint-config-xmtp-web: "workspace:*"
ethers: "npm:^6.11.1"
long: "npm:^5.2.3"
prettier: "npm:^3.2.5"
prettier-plugin-packagejson: "npm:^2.4.12"
rollup: "npm:^4.13.0"
rollup-plugin-dts: "npm:^6.1.0"
rollup-plugin-filesize: "npm:^10.0.0"
rollup-plugin-tsconfig-paths: "npm:^1.5.2"
typedoc: "npm:^0.25.12"
typescript: "npm:^5.4.2"
vite: "npm:^5.1.6"
vite-tsconfig-paths: "npm:^4.3.1"
vitest: "npm:^1.3.1"
languageName: unknown
linkType: soft

"@xmtp/content-type-reaction@npm:^1.1.7":
version: 1.1.7
resolution: "@xmtp/content-type-reaction@npm:1.1.7"
Expand Down Expand Up @@ -5495,6 +5521,18 @@ __metadata:
languageName: node
linkType: hard

"@xmtp/proto@npm:3.56.0":
version: 3.56.0
resolution: "@xmtp/proto@npm:3.56.0"
dependencies:
long: "npm:^5.2.0"
protobufjs: "npm:^7.0.0"
rxjs: "npm:^7.8.0"
undici: "npm:^5.8.1"
checksum: 10/f752e6858692464319d6f22861fe8f23c46d9bb0eb390fe2220e0b4932a4de84be2e9e1cbafc0200e1bfe2a0ed3a3fb6079941630e57fb80e6325bc2a52bf10d
languageName: node
linkType: hard

"@xmtp/proto@npm:^3.29.0":
version: 3.44.0
resolution: "@xmtp/proto@npm:3.44.0"
Expand Down

0 comments on commit 654b089

Please sign in to comment.