@trustsig/sdk 1.0.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +83 -28
- package/package.json +17 -13
- package/dist/index.d.mts +0 -23
- package/dist/index.d.ts +0 -23
- package/dist/index.js +0 -1
- package/dist/index.mjs +0 -1
package/README.md
CHANGED
|
@@ -1,49 +1,104 @@
|
|
|
1
|
-
# TrustSig
|
|
1
|
+
# TrustSig SDK
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Node.js, Edge, and React SDK for TrustSig bot protection and risk verification.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
This repository is an NPM workspace with isolated packages. Backend cryptography is not bundled into client applications, and frontend frameworks are not required on backend edge workers.
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
- Zero-Dependency Core Cryptography (via `@noble/ciphers`)
|
|
9
|
-
- Local Decryption (Zero Latency)
|
|
10
|
-
- Remote API Verification
|
|
7
|
+
## Packages
|
|
11
8
|
|
|
12
|
-
|
|
9
|
+
| Package | Environment | Purpose |
|
|
10
|
+
| --- | --- | --- |
|
|
11
|
+
| `@trustsig/react` | Browser / SSR | React Context Provider and hooks. |
|
|
12
|
+
| `@trustsig/client` | Browser | Vanilla JavaScript DOM token generator. |
|
|
13
|
+
| `@trustsig/server` | Node.js / Edge | Cryptography and API verification. |
|
|
14
|
+
| `@trustsig/types` | Shared | TypeScript interfaces. |
|
|
15
|
+
|
|
16
|
+
## 1. Client-Side (React/Next.js)
|
|
13
17
|
|
|
14
18
|
```bash
|
|
15
|
-
npm install @trustsig/
|
|
19
|
+
npm install @trustsig/react
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
### Setup Provider
|
|
23
|
+
```tsx
|
|
24
|
+
import { TrustSigProvider } from '@trustsig/react';
|
|
25
|
+
|
|
26
|
+
export default function RootLayout({ children }) {
|
|
27
|
+
return (
|
|
28
|
+
<TrustSigProvider siteKey="YOUR_PUBLIC_KEY" interceptRequests={true}>
|
|
29
|
+
{children}
|
|
30
|
+
</TrustSigProvider>
|
|
31
|
+
);
|
|
32
|
+
}
|
|
16
33
|
```
|
|
17
34
|
|
|
18
|
-
|
|
35
|
+
### Usage
|
|
36
|
+
```tsx
|
|
37
|
+
import { useTrustSig } from '@trustsig/react';
|
|
38
|
+
|
|
39
|
+
export function LoginForm() {
|
|
40
|
+
const { getResponse } = useTrustSig();
|
|
41
|
+
|
|
42
|
+
const handleSubmit = async (e) => {
|
|
43
|
+
e.preventDefault();
|
|
44
|
+
const token = await getResponse();
|
|
45
|
+
|
|
46
|
+
await fetch('/api/login', {
|
|
47
|
+
method: 'POST',
|
|
48
|
+
headers: { 'X-TrustSig-Response': token || '' },
|
|
49
|
+
body: JSON.stringify({ email: "user@example.com" })
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
return (
|
|
54
|
+
<form onSubmit={handleSubmit}>
|
|
55
|
+
<button type="submit">Login</button>
|
|
56
|
+
</form>
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## 2. Client-Side (Vanilla JS / Vue)
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
npm install @trustsig/client
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
```javascript
|
|
68
|
+
import { TrustSigClient } from '@trustsig/client';
|
|
69
|
+
|
|
70
|
+
const client = new TrustSigClient({
|
|
71
|
+
siteKey: "YOUR_PUBLIC_KEY"
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
await client.load();
|
|
75
|
+
const token = await client.getResponse();
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## 3. Server-Side (Verification)
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
npm install @trustsig/server
|
|
82
|
+
```
|
|
19
83
|
|
|
20
84
|
```typescript
|
|
21
|
-
import { TrustSig } from '@trustsig/
|
|
85
|
+
import { TrustSig } from '@trustsig/server';
|
|
22
86
|
|
|
23
|
-
const
|
|
87
|
+
const ts = new TrustSig({
|
|
24
88
|
secretKey: process.env.TRUSTSIG_SECRET_KEY
|
|
25
89
|
});
|
|
26
90
|
|
|
27
|
-
const token =
|
|
91
|
+
const token = request.headers.get('X-TrustSig-Response');
|
|
28
92
|
|
|
29
|
-
const
|
|
30
|
-
if (resultRemote.action === 'BLOCK') {
|
|
31
|
-
throw new Error("Access Denied");
|
|
32
|
-
}
|
|
93
|
+
const resultLocal = ts.verifyLocal(token);
|
|
33
94
|
|
|
34
|
-
const resultLocal = trustsig.verifyLocal(token);
|
|
35
95
|
if (resultLocal.action === 'BLOCK') {
|
|
36
96
|
throw new Error("Access Denied");
|
|
37
97
|
}
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
## Verification Modes
|
|
41
|
-
|
|
42
|
-
### Remote Verification (`verifyRemote`)
|
|
43
|
-
Sends the token to the TrustSig Edge API for validation. Recommended for standard integrations.
|
|
44
98
|
|
|
45
|
-
|
|
46
|
-
Decrypts the token locally using your Project Secret Key. Requires no outbound network requests. Recommended for high-throughput Edge environments.
|
|
99
|
+
const resultRemote = await ts.verifyRemote(token);
|
|
47
100
|
|
|
48
|
-
|
|
49
|
-
|
|
101
|
+
if (resultRemote.action === 'BLOCK') {
|
|
102
|
+
throw new Error("Access Denied");
|
|
103
|
+
}
|
|
104
|
+
```
|
package/package.json
CHANGED
|
@@ -1,21 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trustsig/sdk",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "TrustSig
|
|
5
|
-
"main": "./dist/index.js",
|
|
6
|
-
"module": "./dist/index.mjs",
|
|
7
|
-
"types": "./dist/index.d.ts",
|
|
3
|
+
"version": "1.2.1",
|
|
4
|
+
"description": "TrustSig SDK for Node.js, Edge, and browser environments",
|
|
8
5
|
"scripts": {
|
|
9
|
-
"build": "
|
|
6
|
+
"build": "npm run build -w @trustsig/types && npm run build -w @trustsig/client && npm run build -w @trustsig/server && npm run build -w @trustsig/react",
|
|
10
7
|
"test": "vitest run"
|
|
11
8
|
},
|
|
12
9
|
"dependencies": {
|
|
13
10
|
"@noble/ciphers": "^1.1.0"
|
|
14
11
|
},
|
|
15
12
|
"devDependencies": {
|
|
16
|
-
"
|
|
13
|
+
"@testing-library/dom": "^10.4.1",
|
|
14
|
+
"@testing-library/react": "^16.3.2",
|
|
15
|
+
"@types/react": "^19.2.14",
|
|
16
|
+
"@types/react-dom": "^19.2.3",
|
|
17
|
+
"happy-dom": "^20.8.9",
|
|
18
|
+
"html-encoding-sniffer": "^6.0.0",
|
|
19
|
+
"react": "^19.2.4",
|
|
20
|
+
"react-dom": "^19.2.4",
|
|
17
21
|
"tsup": "^8.0.0",
|
|
18
|
-
"
|
|
22
|
+
"typescript": "^5.0.0",
|
|
23
|
+
"vitest": "^1.6.1"
|
|
19
24
|
},
|
|
20
25
|
"files": [
|
|
21
26
|
"dist"
|
|
@@ -27,8 +32,7 @@
|
|
|
27
32
|
],
|
|
28
33
|
"author": "TrustSig",
|
|
29
34
|
"license": "MIT",
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
+
"workspaces": [
|
|
36
|
+
"packages/*"
|
|
37
|
+
]
|
|
38
|
+
}
|
package/dist/index.d.mts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
interface TrustSigOptions {
|
|
2
|
-
secretKey: string;
|
|
3
|
-
endpoint?: string;
|
|
4
|
-
}
|
|
5
|
-
interface BotAnalysisResponse {
|
|
6
|
-
is_bot: boolean;
|
|
7
|
-
score: number;
|
|
8
|
-
action: string;
|
|
9
|
-
request_id: string;
|
|
10
|
-
factors: string[];
|
|
11
|
-
evidence: Record<string, any>;
|
|
12
|
-
site_key: string;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
declare class TrustSig {
|
|
16
|
-
private secretKey;
|
|
17
|
-
private endpoint;
|
|
18
|
-
constructor(options: TrustSigOptions);
|
|
19
|
-
verifyLocal(token: string): BotAnalysisResponse;
|
|
20
|
-
verifyRemote(token: string): Promise<BotAnalysisResponse>;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export { type BotAnalysisResponse, TrustSig, type TrustSigOptions };
|
package/dist/index.d.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
interface TrustSigOptions {
|
|
2
|
-
secretKey: string;
|
|
3
|
-
endpoint?: string;
|
|
4
|
-
}
|
|
5
|
-
interface BotAnalysisResponse {
|
|
6
|
-
is_bot: boolean;
|
|
7
|
-
score: number;
|
|
8
|
-
action: string;
|
|
9
|
-
request_id: string;
|
|
10
|
-
factors: string[];
|
|
11
|
-
evidence: Record<string, any>;
|
|
12
|
-
site_key: string;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
declare class TrustSig {
|
|
16
|
-
private secretKey;
|
|
17
|
-
private endpoint;
|
|
18
|
-
constructor(options: TrustSigOptions);
|
|
19
|
-
verifyLocal(token: string): BotAnalysisResponse;
|
|
20
|
-
verifyRemote(token: string): Promise<BotAnalysisResponse>;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export { type BotAnalysisResponse, TrustSig, type TrustSigOptions };
|
package/dist/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";var i=Object.defineProperty;var u=Object.getOwnPropertyDescriptor;var f=Object.getOwnPropertyNames;var l=Object.prototype.hasOwnProperty;var O=(r,t)=>{for(var e in t)i(r,e,{get:t[e],enumerable:!0})},R=(r,t,e,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of f(t))!l.call(r,s)&&s!==e&&i(r,s,{get:()=>t[s],enumerable:!(n=u(t,s))||n.enumerable});return r};var T=r=>R(i({},"__esModule",{value:!0}),r);var g={};O(g,{TrustSig:()=>c});module.exports=T(g);var a=require("@noble/ciphers/chacha");function A(r){let t=atob(r),e=t.length,n=new Uint8Array(e);for(let s=0;s<e;s++)n[s]=t.charCodeAt(s);return n}function y(r,t){let e=new Uint8Array(32),n=new TextEncoder().encode(r),s=Math.min(n.length,32);e.set(n.slice(0,s));let o=A(t);if(o.length<12)throw new Error("TOKEN_TOO_SHORT");let p=o.slice(0,12),d=o.slice(12),_=(0,a.chacha20poly1305)(e,p).decrypt(d),h=new TextDecoder().decode(_);return JSON.parse(h)}var c=class{secretKey;endpoint;constructor(t){if(!t.secretKey)throw new Error("SECRET_KEY_REQUIRED");this.secretKey=t.secretKey,this.endpoint=t.endpoint||"https://api.trustsig.com"}verifyLocal(t){try{let e=y(this.secretKey,t);return{is_bot:e.is_bot??!0,score:e.score??100,action:e.action??"BLOCK_MALFORMED_VERDICT",request_id:e.request_id??"",factors:e.factors??[],evidence:e.evidence??{},site_key:e.site_key??""}}catch{return{is_bot:!0,score:100,action:"BLOCK_CRYPTO_FAIL",request_id:"",factors:["CRYPTO_FAIL"],evidence:{},site_key:""}}}async verifyRemote(t){try{let e=await fetch(`${this.endpoint}/verify`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({secret:this.secretKey,response:t})});if(!e.ok)throw new Error("HTTP_ERROR");return await e.json()}catch{return{is_bot:!0,score:100,action:"BLOCK_API_FAIL",request_id:"",factors:["API_FAIL"],evidence:{},site_key:""}}}};0&&(module.exports={TrustSig});
|
package/dist/index.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{chacha20poly1305 as _}from"@noble/ciphers/chacha";function h(n){let t=atob(n),e=t.length,r=new Uint8Array(e);for(let s=0;s<e;s++)r[s]=t.charCodeAt(s);return r}function i(n,t){let e=new Uint8Array(32),r=new TextEncoder().encode(n),s=Math.min(r.length,32);e.set(r.slice(0,s));let o=h(t);if(o.length<12)throw new Error("TOKEN_TOO_SHORT");let a=o.slice(0,12),y=o.slice(12),p=_(e,a).decrypt(y),d=new TextDecoder().decode(p);return JSON.parse(d)}var c=class{secretKey;endpoint;constructor(t){if(!t.secretKey)throw new Error("SECRET_KEY_REQUIRED");this.secretKey=t.secretKey,this.endpoint=t.endpoint||"https://api.trustsig.com"}verifyLocal(t){try{let e=i(this.secretKey,t);return{is_bot:e.is_bot??!0,score:e.score??100,action:e.action??"BLOCK_MALFORMED_VERDICT",request_id:e.request_id??"",factors:e.factors??[],evidence:e.evidence??{},site_key:e.site_key??""}}catch{return{is_bot:!0,score:100,action:"BLOCK_CRYPTO_FAIL",request_id:"",factors:["CRYPTO_FAIL"],evidence:{},site_key:""}}}async verifyRemote(t){try{let e=await fetch(`${this.endpoint}/verify`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({secret:this.secretKey,response:t})});if(!e.ok)throw new Error("HTTP_ERROR");return await e.json()}catch{return{is_bot:!0,score:100,action:"BLOCK_API_FAIL",request_id:"",factors:["API_FAIL"],evidence:{},site_key:""}}}};export{c as TrustSig};
|