@sip-protocol/sdk 0.5.0 → 0.6.0
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/LICENSE +21 -0
- package/dist/TransportWebUSB-TQ7WZ4LE.mjs +3098 -0
- package/dist/browser.d.mts +1 -1
- package/dist/browser.d.ts +1 -1
- package/dist/browser.js +3216 -35
- package/dist/browser.mjs +2 -1
- package/dist/chunk-KBS3OMSZ.mjs +14737 -0
- package/dist/chunk-TK3FWQNC.mjs +14737 -0
- package/dist/chunk-UJCSKKID.mjs +30 -0
- package/dist/index-05W_S8A7.d.mts +9237 -0
- package/dist/index-C5ehlFhR.d.mts +9443 -0
- package/dist/index-CNzhx-WH.d.mts +9316 -0
- package/dist/index-CqSppS4i.d.ts +9237 -0
- package/dist/index-DBa_jiZF.d.mts +9606 -0
- package/dist/index-DLNdSQFQ.d.ts +9316 -0
- package/dist/index-Ink8HnKW.d.ts +9606 -0
- package/dist/index-h7B23m5b.d.ts +9443 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +3209 -28
- package/dist/index.mjs +2 -1
- package/dist/proofs/noir.mjs +1 -0
- package/package.json +15 -15
- package/src/crypto.ts +79 -9
- package/src/intent.ts +82 -4
- package/src/privacy.ts +88 -2
- package/src/sip.ts +324 -25
- package/src/stealth.ts +120 -9
- package/src/wallet/hardware/ledger.ts +1 -2
package/dist/index.mjs
CHANGED
|
@@ -238,7 +238,7 @@ import {
|
|
|
238
238
|
walletRegistry,
|
|
239
239
|
withSecureBuffer,
|
|
240
240
|
withSecureBufferSync
|
|
241
|
-
} from "./chunk-
|
|
241
|
+
} from "./chunk-KBS3OMSZ.mjs";
|
|
242
242
|
import {
|
|
243
243
|
CryptoError,
|
|
244
244
|
EncryptionNotImplementedError,
|
|
@@ -255,6 +255,7 @@ import {
|
|
|
255
255
|
isSIPError,
|
|
256
256
|
wrapError
|
|
257
257
|
} from "./chunk-HGU6HZRC.mjs";
|
|
258
|
+
import "./chunk-UJCSKKID.mjs";
|
|
258
259
|
export {
|
|
259
260
|
ATTESTATION_VERSION,
|
|
260
261
|
AptosStealthService,
|
package/dist/proofs/noir.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sip-protocol/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Core SDK for Shielded Intents Protocol - Privacy layer for cross-chain transactions",
|
|
5
5
|
"author": "SIP Protocol <hello@sip-protocol.org>",
|
|
6
6
|
"homepage": "https://sip-protocol.org",
|
|
@@ -36,17 +36,6 @@
|
|
|
36
36
|
"dist",
|
|
37
37
|
"src"
|
|
38
38
|
],
|
|
39
|
-
"scripts": {
|
|
40
|
-
"build": "tsup src/index.ts src/browser.ts src/proofs/noir.ts --format cjs,esm --dts",
|
|
41
|
-
"dev": "tsup src/index.ts src/browser.ts src/proofs/noir.ts --format cjs,esm --dts --watch",
|
|
42
|
-
"lint": "eslint --ext .ts src/",
|
|
43
|
-
"typecheck": "tsc --noEmit",
|
|
44
|
-
"clean": "rm -rf dist",
|
|
45
|
-
"test": "vitest",
|
|
46
|
-
"test:coverage": "vitest run --coverage",
|
|
47
|
-
"bench": "vitest bench --config vitest.bench.config.ts",
|
|
48
|
-
"bench:json": "vitest bench --config vitest.bench.config.ts --outputJson benchmarks/results.json"
|
|
49
|
-
},
|
|
50
39
|
"dependencies": {
|
|
51
40
|
"@aztec/bb.js": "^0.63.1",
|
|
52
41
|
"@noble/ciphers": "^2.0.1",
|
|
@@ -55,7 +44,7 @@
|
|
|
55
44
|
"@noir-lang/noir_js": "^1.0.0-beta.16",
|
|
56
45
|
"@noir-lang/types": "1.0.0-beta.16",
|
|
57
46
|
"@scure/base": "^2.0.0",
|
|
58
|
-
"@sip-protocol/types": "^0.
|
|
47
|
+
"@sip-protocol/types": "^0.2.0"
|
|
59
48
|
},
|
|
60
49
|
"devDependencies": {
|
|
61
50
|
"@vitest/coverage-v8": "1.6.1",
|
|
@@ -72,5 +61,16 @@
|
|
|
72
61
|
"stealth-addresses",
|
|
73
62
|
"zcash"
|
|
74
63
|
],
|
|
75
|
-
"license": "MIT"
|
|
76
|
-
|
|
64
|
+
"license": "MIT",
|
|
65
|
+
"scripts": {
|
|
66
|
+
"build": "tsup src/index.ts src/browser.ts src/proofs/noir.ts --format cjs,esm --dts",
|
|
67
|
+
"dev": "tsup src/index.ts src/browser.ts src/proofs/noir.ts --format cjs,esm --dts --watch",
|
|
68
|
+
"lint": "eslint --ext .ts src/",
|
|
69
|
+
"typecheck": "tsc --noEmit",
|
|
70
|
+
"clean": "rm -rf dist",
|
|
71
|
+
"test": "vitest",
|
|
72
|
+
"test:coverage": "vitest run --coverage",
|
|
73
|
+
"bench": "vitest bench --config vitest.bench.config.ts",
|
|
74
|
+
"bench:json": "vitest bench --config vitest.bench.config.ts --outputJson benchmarks/results.json"
|
|
75
|
+
}
|
|
76
|
+
}
|
package/src/crypto.ts
CHANGED
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Cryptographic utilities for SIP Protocol
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
4
|
+
* This module provides low-level cryptographic primitives including:
|
|
5
|
+
* - Hash functions (SHA-256)
|
|
6
|
+
* - Random number generation
|
|
7
|
+
* - Pedersen commitments (legacy wrappers)
|
|
8
8
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
9
|
+
* **Important:**
|
|
10
|
+
* - For ZK proofs, use {@link ProofProvider} interface (see ./proofs/)
|
|
11
|
+
* - For Pedersen commitments in new code, use ./commitment.ts directly
|
|
12
|
+
* - This module maintains legacy functions for backward compatibility
|
|
13
|
+
*
|
|
14
|
+
* @module crypto
|
|
15
|
+
* @see {@link ProofProvider} for zero-knowledge proof generation
|
|
16
|
+
* @see ./commitment.ts for modern Pedersen commitment API
|
|
17
|
+
* @see ./stealth.ts for stealth address cryptography
|
|
11
18
|
*/
|
|
12
19
|
|
|
13
20
|
import { sha256 } from '@noble/hashes/sha256'
|
|
@@ -70,7 +77,18 @@ export function verifyCommitment(
|
|
|
70
77
|
}
|
|
71
78
|
|
|
72
79
|
/**
|
|
73
|
-
* Generate a
|
|
80
|
+
* Generate a unique intent identifier
|
|
81
|
+
*
|
|
82
|
+
* Creates a cryptographically random intent ID with the `sip-` prefix.
|
|
83
|
+
* IDs are globally unique with negligible collision probability (128-bit randomness).
|
|
84
|
+
*
|
|
85
|
+
* @returns Intent ID string in format: `sip-<32 hex chars>`
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* const intentId = generateIntentId()
|
|
90
|
+
* console.log(intentId) // "sip-a1b2c3d4e5f67890a1b2c3d4e5f67890"
|
|
91
|
+
* ```
|
|
74
92
|
*/
|
|
75
93
|
export function generateIntentId(): string {
|
|
76
94
|
const bytes = randomBytes(16)
|
|
@@ -78,7 +96,36 @@ export function generateIntentId(): string {
|
|
|
78
96
|
}
|
|
79
97
|
|
|
80
98
|
/**
|
|
81
|
-
*
|
|
99
|
+
* Compute SHA-256 hash of data
|
|
100
|
+
*
|
|
101
|
+
* General-purpose cryptographic hash function used throughout SIP protocol
|
|
102
|
+
* for commitment derivation, data integrity, and key derivation.
|
|
103
|
+
*
|
|
104
|
+
* **Use cases:**
|
|
105
|
+
* - Hash transaction data for commitments
|
|
106
|
+
* - Derive deterministic IDs from inputs
|
|
107
|
+
* - Verify data integrity
|
|
108
|
+
*
|
|
109
|
+
* @param data - Input data as UTF-8 string or raw bytes
|
|
110
|
+
* @returns 32-byte hash as hex string with `0x` prefix
|
|
111
|
+
*
|
|
112
|
+
* @example Hash a string
|
|
113
|
+
* ```typescript
|
|
114
|
+
* const messageHash = hash('Hello, SIP Protocol!')
|
|
115
|
+
* console.log(messageHash) // "0xabc123..."
|
|
116
|
+
* ```
|
|
117
|
+
*
|
|
118
|
+
* @example Hash binary data
|
|
119
|
+
* ```typescript
|
|
120
|
+
* const dataBytes = new Uint8Array([1, 2, 3, 4])
|
|
121
|
+
* const dataHash = hash(dataBytes)
|
|
122
|
+
* ```
|
|
123
|
+
*
|
|
124
|
+
* @example Use in commitment scheme
|
|
125
|
+
* ```typescript
|
|
126
|
+
* const intentHash = hash(intent.intentId)
|
|
127
|
+
* const commitment = commit(amount, hexToBytes(intentHash))
|
|
128
|
+
* ```
|
|
82
129
|
*/
|
|
83
130
|
export function hash(data: string | Uint8Array): Hash {
|
|
84
131
|
const input = typeof data === 'string' ? new TextEncoder().encode(data) : data
|
|
@@ -86,7 +133,30 @@ export function hash(data: string | Uint8Array): Hash {
|
|
|
86
133
|
}
|
|
87
134
|
|
|
88
135
|
/**
|
|
89
|
-
* Generate random bytes
|
|
136
|
+
* Generate cryptographically secure random bytes
|
|
137
|
+
*
|
|
138
|
+
* Uses the platform's secure random source (Web Crypto API in browsers,
|
|
139
|
+
* crypto.randomBytes in Node.js) to generate unpredictable random data.
|
|
140
|
+
*
|
|
141
|
+
* **Use cases:**
|
|
142
|
+
* - Generate private keys
|
|
143
|
+
* - Create nonces for encryption
|
|
144
|
+
* - Produce blinding factors for commitments
|
|
145
|
+
* - Generate ephemeral keypairs
|
|
146
|
+
*
|
|
147
|
+
* @param length - Number of random bytes to generate
|
|
148
|
+
* @returns Random bytes as hex string with `0x` prefix
|
|
149
|
+
*
|
|
150
|
+
* @example Generate a 32-byte private key
|
|
151
|
+
* ```typescript
|
|
152
|
+
* const privateKey = generateRandomBytes(32)
|
|
153
|
+
* console.log(privateKey) // "0xabc123...def" (64 hex chars)
|
|
154
|
+
* ```
|
|
155
|
+
*
|
|
156
|
+
* @example Generate a nonce
|
|
157
|
+
* ```typescript
|
|
158
|
+
* const nonce = generateRandomBytes(24) // For XChaCha20
|
|
159
|
+
* ```
|
|
90
160
|
*/
|
|
91
161
|
export function generateRandomBytes(length: number): HexString {
|
|
92
162
|
return `0x${bytesToHex(randomBytes(length))}` as HexString
|
package/src/intent.ts
CHANGED
|
@@ -41,19 +41,97 @@ import {
|
|
|
41
41
|
|
|
42
42
|
/**
|
|
43
43
|
* Options for creating a shielded intent
|
|
44
|
+
*
|
|
45
|
+
* Additional configuration passed to {@link createShieldedIntent} that
|
|
46
|
+
* affects proof generation and sender identification.
|
|
44
47
|
*/
|
|
45
48
|
export interface CreateIntentOptions {
|
|
46
|
-
/**
|
|
49
|
+
/**
|
|
50
|
+
* Wallet address of the sender
|
|
51
|
+
*
|
|
52
|
+
* Used for:
|
|
53
|
+
* - Generating ownership proofs (proving control of input funds)
|
|
54
|
+
* - Creating sender commitments
|
|
55
|
+
* - Enabling refunds if transaction fails
|
|
56
|
+
*
|
|
57
|
+
* Optional but recommended for production use.
|
|
58
|
+
*/
|
|
47
59
|
senderAddress?: string
|
|
60
|
+
|
|
48
61
|
/**
|
|
49
|
-
* Proof provider for
|
|
50
|
-
*
|
|
62
|
+
* Proof provider for automatic ZK proof generation
|
|
63
|
+
*
|
|
64
|
+
* If provided and privacy level is SHIELDED or COMPLIANT, proofs will be
|
|
65
|
+
* generated automatically during intent creation. If not provided, proofs
|
|
66
|
+
* must be attached later using {@link attachProofs}.
|
|
67
|
+
*
|
|
68
|
+
* **Available providers:**
|
|
69
|
+
* - {@link MockProofProvider}: For testing
|
|
70
|
+
* - `NoirProofProvider`: For production (Node.js)
|
|
71
|
+
* - `BrowserNoirProvider`: For browsers
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* import { NoirProofProvider } from '@sip-protocol/sdk/proofs/noir'
|
|
76
|
+
*
|
|
77
|
+
* const provider = new NoirProofProvider()
|
|
78
|
+
* await provider.initialize()
|
|
79
|
+
*
|
|
80
|
+
* const intent = await createShieldedIntent(params, {
|
|
81
|
+
* senderAddress: wallet.address,
|
|
82
|
+
* proofProvider: provider,
|
|
83
|
+
* })
|
|
84
|
+
* ```
|
|
51
85
|
*/
|
|
52
86
|
proofProvider?: ProofProvider
|
|
53
87
|
}
|
|
54
88
|
|
|
55
89
|
/**
|
|
56
|
-
*
|
|
90
|
+
* Fluent builder for creating shielded intents
|
|
91
|
+
*
|
|
92
|
+
* Provides a chainable API for constructing intents step-by-step.
|
|
93
|
+
* More ergonomic than passing a large parameter object directly.
|
|
94
|
+
*
|
|
95
|
+
* **Builder Methods:**
|
|
96
|
+
* - {@link input}: Set input asset and amount
|
|
97
|
+
* - {@link output}: Set output asset and constraints
|
|
98
|
+
* - {@link privacy}: Set privacy level
|
|
99
|
+
* - {@link recipient}: Set recipient stealth meta-address
|
|
100
|
+
* - {@link slippage}: Set slippage tolerance
|
|
101
|
+
* - {@link ttl}: Set time-to-live
|
|
102
|
+
* - {@link withProvider}: Set proof provider
|
|
103
|
+
* - {@link build}: Create the intent
|
|
104
|
+
*
|
|
105
|
+
* @example Basic intent
|
|
106
|
+
* ```typescript
|
|
107
|
+
* const intent = await new IntentBuilder()
|
|
108
|
+
* .input('solana', 'SOL', 10n * 10n**9n)
|
|
109
|
+
* .output('ethereum', 'ETH', 0n)
|
|
110
|
+
* .privacy(PrivacyLevel.SHIELDED)
|
|
111
|
+
* .build()
|
|
112
|
+
* ```
|
|
113
|
+
*
|
|
114
|
+
* @example Full-featured intent with proofs
|
|
115
|
+
* ```typescript
|
|
116
|
+
* import { IntentBuilder, PrivacyLevel, MockProofProvider } from '@sip-protocol/sdk'
|
|
117
|
+
*
|
|
118
|
+
* const builder = new IntentBuilder()
|
|
119
|
+
* const intent = await builder
|
|
120
|
+
* .input('near', 'NEAR', 100n * 10n**24n, wallet.address) // 100 NEAR
|
|
121
|
+
* .output('zcash', 'ZEC', 95n * 10n**8n) // Min 95 ZEC
|
|
122
|
+
* .privacy(PrivacyLevel.COMPLIANT)
|
|
123
|
+
* .recipient('sip:zcash:0x02abc...123:0x03def...456')
|
|
124
|
+
* .slippage(1) // 1%
|
|
125
|
+
* .ttl(600) // 10 minutes
|
|
126
|
+
* .withProvider(new MockProofProvider())
|
|
127
|
+
* .build()
|
|
128
|
+
*
|
|
129
|
+
* console.log('Intent created:', intent.intentId)
|
|
130
|
+
* console.log('Has proofs:', !!intent.fundingProof && !!intent.validityProof)
|
|
131
|
+
* ```
|
|
132
|
+
*
|
|
133
|
+
* @see {@link createShieldedIntent} for the underlying implementation
|
|
134
|
+
* @see {@link CreateIntentParams} for parameter types
|
|
57
135
|
*/
|
|
58
136
|
export class IntentBuilder {
|
|
59
137
|
private params: Partial<CreateIntentParams> = {}
|
package/src/privacy.ts
CHANGED
|
@@ -48,7 +48,41 @@ export interface PrivacyConfig {
|
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
/**
|
|
51
|
-
* Get privacy configuration for a privacy level
|
|
51
|
+
* Get privacy configuration for a given privacy level
|
|
52
|
+
*
|
|
53
|
+
* Returns a configuration object that determines which privacy features
|
|
54
|
+
* to enable for an intent. Used internally by the SDK to configure
|
|
55
|
+
* privacy behavior.
|
|
56
|
+
*
|
|
57
|
+
* **Privacy Levels:**
|
|
58
|
+
* - `'transparent'`: No privacy, fully public on-chain
|
|
59
|
+
* - `'shielded'`: Full privacy, hidden sender/amount/recipient
|
|
60
|
+
* - `'compliant'`: Privacy with viewing key for regulatory compliance
|
|
61
|
+
*
|
|
62
|
+
* @param level - Privacy level to configure
|
|
63
|
+
* @param viewingKey - Required for compliant mode, optional otherwise
|
|
64
|
+
* @returns Configuration object specifying privacy features
|
|
65
|
+
*
|
|
66
|
+
* @throws {ValidationError} If compliant mode specified without viewing key
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* // Transparent (no privacy)
|
|
71
|
+
* const config = getPrivacyConfig('transparent')
|
|
72
|
+
* // { level: 'transparent', useStealth: false, encryptData: false }
|
|
73
|
+
*
|
|
74
|
+
* // Shielded (full privacy)
|
|
75
|
+
* const config = getPrivacyConfig('shielded')
|
|
76
|
+
* // { level: 'shielded', useStealth: true, encryptData: true }
|
|
77
|
+
*
|
|
78
|
+
* // Compliant (privacy + audit)
|
|
79
|
+
* const viewingKey = generateViewingKey()
|
|
80
|
+
* const config = getPrivacyConfig('compliant', viewingKey)
|
|
81
|
+
* // { level: 'compliant', viewingKey, useStealth: true, encryptData: true }
|
|
82
|
+
* ```
|
|
83
|
+
*
|
|
84
|
+
* @see {@link PrivacyLevel} for available privacy levels
|
|
85
|
+
* @see {@link generateViewingKey} to create viewing keys
|
|
52
86
|
*/
|
|
53
87
|
export function getPrivacyConfig(
|
|
54
88
|
level: PrivacyLevel,
|
|
@@ -96,7 +130,59 @@ export function getPrivacyConfig(
|
|
|
96
130
|
}
|
|
97
131
|
|
|
98
132
|
/**
|
|
99
|
-
* Generate a new viewing key
|
|
133
|
+
* Generate a new viewing key for compliant privacy mode
|
|
134
|
+
*
|
|
135
|
+
* Creates a cryptographically random viewing key that enables selective
|
|
136
|
+
* disclosure of transaction details to auditors or regulators while
|
|
137
|
+
* maintaining on-chain privacy.
|
|
138
|
+
*
|
|
139
|
+
* **Use Cases:**
|
|
140
|
+
* - Regulatory compliance (AML/KYC audits)
|
|
141
|
+
* - Internal accounting and reconciliation
|
|
142
|
+
* - Voluntary disclosure to trusted parties
|
|
143
|
+
* - Hierarchical key management (via derivation paths)
|
|
144
|
+
*
|
|
145
|
+
* **Security:**
|
|
146
|
+
* - Keep viewing keys secret - they decrypt all transaction details
|
|
147
|
+
* - Use hierarchical derivation for key management (BIP32-style paths)
|
|
148
|
+
* - Rotate keys periodically for forward secrecy
|
|
149
|
+
*
|
|
150
|
+
* @param path - Hierarchical derivation path (BIP32-style, e.g., "m/0", "m/44'/0'/0'")
|
|
151
|
+
* @returns Viewing key object with key, path, and hash
|
|
152
|
+
*
|
|
153
|
+
* @example Generate master viewing key
|
|
154
|
+
* ```typescript
|
|
155
|
+
* const masterKey = generateViewingKey('m/0')
|
|
156
|
+
* console.log(masterKey.key) // "0xabc123..."
|
|
157
|
+
* console.log(masterKey.path) // "m/0"
|
|
158
|
+
* console.log(masterKey.hash) // "0xdef456..." (for identification)
|
|
159
|
+
* ```
|
|
160
|
+
*
|
|
161
|
+
* @example Generate organization-specific keys
|
|
162
|
+
* ```typescript
|
|
163
|
+
* const auditKey = generateViewingKey('m/0/audit')
|
|
164
|
+
* const accountingKey = generateViewingKey('m/0/accounting')
|
|
165
|
+
*
|
|
166
|
+
* // Share different keys with different departments
|
|
167
|
+
* shareWithAuditor(auditKey)
|
|
168
|
+
* shareWithAccounting(accountingKey)
|
|
169
|
+
* ```
|
|
170
|
+
*
|
|
171
|
+
* @example Use in compliant intent
|
|
172
|
+
* ```typescript
|
|
173
|
+
* const viewingKey = generateViewingKey()
|
|
174
|
+
*
|
|
175
|
+
* const intent = await sip.createIntent({
|
|
176
|
+
* input: { asset: { chain: 'near', symbol: 'NEAR', address: null, decimals: 24 }, amount: 100n },
|
|
177
|
+
* output: { asset: { chain: 'zcash', symbol: 'ZEC', address: null, decimals: 8 }, minAmount: 0n, maxSlippage: 0.01 },
|
|
178
|
+
* privacy: PrivacyLevel.COMPLIANT,
|
|
179
|
+
* viewingKey: viewingKey.key,
|
|
180
|
+
* })
|
|
181
|
+
* ```
|
|
182
|
+
*
|
|
183
|
+
* @see {@link deriveViewingKey} to derive child keys hierarchically
|
|
184
|
+
* @see {@link encryptForViewing} to encrypt data with viewing key
|
|
185
|
+
* @see {@link decryptWithViewing} to decrypt data with viewing key
|
|
100
186
|
*/
|
|
101
187
|
export function generateViewingKey(path: string = 'm/0'): ViewingKey {
|
|
102
188
|
const keyBytes = randomBytes(32)
|