@smartledger/bsv 5.1.0 → 5.2.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.
@@ -55,19 +55,19 @@ Three advanced modules totaling **~2.7MB** of functionality:
55
55
  - **Purpose**: Threshold cryptography for secure secret distribution
56
56
  - **Use Cases**: Backup keys, multi-party security, key recovery
57
57
  - **Features**: Split secrets into N shares, require M to reconstruct
58
- - **CDN**: `unpkg.com/@smartledger/bsv@5.1.0/bsv-shamir.min.js`
58
+ - **CDN**: `unpkg.com/@smartledger/bsv@5.2.0/bsv-shamir.min.js`
59
59
 
60
60
  #### **🌐 Global Digital Attestation Framework - GDAF (1184KB)**
61
61
  - **Purpose**: W3C Verifiable Credentials and decentralized identity
62
62
  - **Use Cases**: Identity verification, attestations, zero-knowledge proofs
63
63
  - **Features**: DID creation, credential issuance, selective disclosure
64
- - **CDN**: `unpkg.com/@smartledger/bsv@5.1.0/bsv-gdaf.min.js`
64
+ - **CDN**: `unpkg.com/@smartledger/bsv@5.2.0/bsv-gdaf.min.js`
65
65
 
66
66
  #### **⚖️ Legal Token Protocol - LTP (1184KB)**
67
67
  - **Purpose**: Legal compliance framework for tokenized assets
68
68
  - **Use Cases**: Property rights, obligations, compliant tokenization
69
69
  - **Features**: Legal primitives, compliance checking, attestation anchoring
70
- - **CDN**: `unpkg.com/@smartledger/bsv@5.1.0/bsv-ltp.min.js`
70
+ - **CDN**: `unpkg.com/@smartledger/bsv@5.2.0/bsv-ltp.min.js`
71
71
 
72
72
  ### **2. Incorrect File Sizes in Documentation**
73
73
 
@@ -81,18 +81,18 @@ Three advanced modules totaling **~2.7MB** of functionality:
81
81
 
82
82
  | Module | Size | Use Case | CDN Link |
83
83
  |--------|------|----------|----------|
84
- | **bsv.min.js** | 937KB | Core BSV + SmartContract | `unpkg.com/@smartledger/bsv@5.1.0/bsv.min.js` |
85
- | **bsv.bundle.js** | 937KB | Everything in one file | `unpkg.com/@smartledger/bsv@5.1.0/bsv.bundle.js` |
86
- | **bsv-smartcontract.min.js** | 937KB | Covenant development | `unpkg.com/@smartledger/bsv@5.1.0/bsv-smartcontract.min.js` |
87
- | **bsv-covenant.min.js** | 913KB | Covenant operations | `unpkg.com/@smartledger/bsv@5.1.0/bsv-covenant.min.js` |
88
- | **bsv-script-helper.min.js** | 26KB | Custom script tools | `unpkg.com/@smartledger/bsv@5.1.0/bsv-script-helper.min.js` |
89
- | **bsv-security.min.js** | 26KB | Security enhancements (opt-in helpers — see README › Security) | `unpkg.com/@smartledger/bsv@5.1.0/bsv-security.min.js` |
90
- | **bsv-ecies.min.js** | 71KB | Encryption | `unpkg.com/@smartledger/bsv@5.1.0/bsv-ecies.min.js` |
91
- | **bsv-message.min.js** | 26KB | Message signing | `unpkg.com/@smartledger/bsv@5.1.0/bsv-message.min.js` |
92
- | **bsv-mnemonic.min.js** | 681KB | HD wallets | `unpkg.com/@smartledger/bsv@5.1.0/bsv-mnemonic.min.js` |
93
- | **🆕 bsv-shamir.min.js** | 432KB | **Secret sharing** | `unpkg.com/@smartledger/bsv@5.1.0/bsv-shamir.min.js` |
94
- | **🆕 bsv-gdaf.min.js** | 1184KB | **Digital attestation** | `unpkg.com/@smartledger/bsv@5.1.0/bsv-gdaf.min.js` |
95
- | **🆕 bsv-ltp.min.js** | 1184KB | **Legal tokens** | `unpkg.com/@smartledger/bsv@5.1.0/bsv-ltp.min.js` |
84
+ | **bsv.min.js** | 937KB | Core BSV + SmartContract | `unpkg.com/@smartledger/bsv@5.2.0/bsv.min.js` |
85
+ | **bsv.bundle.js** | 937KB | Everything in one file | `unpkg.com/@smartledger/bsv@5.2.0/bsv.bundle.js` |
86
+ | **bsv-smartcontract.min.js** | 937KB | Covenant development | `unpkg.com/@smartledger/bsv@5.2.0/bsv-smartcontract.min.js` |
87
+ | **bsv-covenant.min.js** | 913KB | Covenant operations | `unpkg.com/@smartledger/bsv@5.2.0/bsv-covenant.min.js` |
88
+ | **bsv-script-helper.min.js** | 26KB | Custom script tools | `unpkg.com/@smartledger/bsv@5.2.0/bsv-script-helper.min.js` |
89
+ | **bsv-security.min.js** | 26KB | Security enhancements (opt-in helpers — see README › Security) | `unpkg.com/@smartledger/bsv@5.2.0/bsv-security.min.js` |
90
+ | **bsv-ecies.min.js** | 71KB | Encryption | `unpkg.com/@smartledger/bsv@5.2.0/bsv-ecies.min.js` |
91
+ | **bsv-message.min.js** | 26KB | Message signing | `unpkg.com/@smartledger/bsv@5.2.0/bsv-message.min.js` |
92
+ | **bsv-mnemonic.min.js** | 681KB | HD wallets | `unpkg.com/@smartledger/bsv@5.2.0/bsv-mnemonic.min.js` |
93
+ | **🆕 bsv-shamir.min.js** | 432KB | **Secret sharing** | `unpkg.com/@smartledger/bsv@5.2.0/bsv-shamir.min.js` |
94
+ | **🆕 bsv-gdaf.min.js** | 1184KB | **Digital attestation** | `unpkg.com/@smartledger/bsv@5.2.0/bsv-gdaf.min.js` |
95
+ | **🆕 bsv-ltp.min.js** | 1184KB | **Legal tokens** | `unpkg.com/@smartledger/bsv@5.2.0/bsv-ltp.min.js` |
96
96
 
97
97
  ## 🎯 **Updated Usage Examples**
98
98
 
@@ -100,22 +100,22 @@ Three advanced modules totaling **~2.7MB** of functionality:
100
100
 
101
101
  #### **1. Basic Development (~963KB)**
102
102
  ```html
103
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv.min.js"></script>
104
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv-script-helper.min.js"></script>
103
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv.min.js"></script>
104
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv-script-helper.min.js"></script>
105
105
  ```
106
106
 
107
107
  #### **2. Smart Contract Development (~2.7MB — each bundle re-embeds core BSV)**
108
108
  ```html
109
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv.min.js"></script>
110
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv-covenant.min.js"></script>
111
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv-smartcontract.min.js"></script>
109
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv.min.js"></script>
110
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv-covenant.min.js"></script>
111
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv-smartcontract.min.js"></script>
112
112
  ```
113
113
 
114
114
  #### **3. 🆕 Legal & Compliance Development (~3.2MB — each bundle re-embeds core BSV)**
115
115
  ```html
116
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv.min.js"></script>
117
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv-ltp.min.js"></script>
118
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv-gdaf.min.js"></script>
116
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv.min.js"></script>
117
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv-ltp.min.js"></script>
118
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv-gdaf.min.js"></script>
119
119
  <script>
120
120
  // Legal Token Protocol
121
121
  const legalToken = bsv.createLegalToken({
@@ -132,9 +132,9 @@ Three advanced modules totaling **~2.7MB** of functionality:
132
132
 
133
133
  #### **4. 🆕 Security & Cryptography (~1.4MB)**
134
134
  ```html
135
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv.min.js"></script>
136
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv-security.min.js"></script>
137
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv-shamir.min.js"></script>
135
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv.min.js"></script>
136
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv-security.min.js"></script>
137
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv-shamir.min.js"></script>
138
138
  <script>
139
139
  // Shamir Secret Sharing
140
140
  const shares = bsv.splitSecret('my_secret_key', 5, 3); // 5 shares, 3 needed
@@ -146,7 +146,7 @@ Three advanced modules totaling **~2.7MB** of functionality:
146
146
 
147
147
  #### **5. Everything Bundle (937KB)**
148
148
  ```html
149
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv.bundle.js"></script>
149
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv.bundle.js"></script>
150
150
  <script>
151
151
  // Everything available immediately
152
152
  const shares = bsv.splitSecret('secret', 5, 3);
@@ -722,7 +722,7 @@ interface UTXO {
722
722
  <html>
723
723
  <head>
724
724
  <title>UTXO Manager Demo</title>
725
- <script src="https://cdn.jsdelivr.net/npm/@smartledger/bsv@5.1.0/bsv.min.js"></script>
725
+ <script src="https://cdn.jsdelivr.net/npm/@smartledger/bsv@5.2.0/bsv.min.js"></script>
726
726
  </head>
727
727
  <body>
728
728
  <script>
@@ -48,7 +48,7 @@ const tx: Transaction = new Transaction();
48
48
  #### **Core Library Only (937KB)**
49
49
  For basic Bitcoin SV operations:
50
50
  ```html
51
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv.min.js"></script>
51
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv.min.js"></script>
52
52
  <script>
53
53
  const privateKey = new bsv.PrivateKey();
54
54
  const address = privateKey.toAddress();
@@ -58,7 +58,7 @@ For basic Bitcoin SV operations:
58
58
  #### **Complete Bundle (937KB)**
59
59
  Everything in one file:
60
60
  ```html
61
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv.bundle.js"></script>
61
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv.bundle.js"></script>
62
62
  <script>
63
63
  // All features available immediately
64
64
  const shares = bsv.splitSecret('secret', 5, 3);
@@ -71,9 +71,9 @@ Everything in one file:
71
71
 
72
72
  #### **Smart Contract Development (~2.7MB total — each bundle is self-contained and re-embeds core BSV)**
73
73
  ```html
74
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv.min.js"></script>
75
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv-covenant.min.js"></script>
76
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv-smartcontract.min.js"></script>
74
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv.min.js"></script>
75
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv-covenant.min.js"></script>
76
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv-smartcontract.min.js"></script>
77
77
  <script>
78
78
  const covenant = bsv.SmartContract.createCovenantBuilder()
79
79
  .extractField('amount').push(50000).greaterThanOrEqual().build();
@@ -82,9 +82,9 @@ Everything in one file:
82
82
 
83
83
  #### **Legal & Identity Development (~3.2MB total — each bundle is self-contained and re-embeds core BSV)**
84
84
  ```html
85
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv.min.js"></script>
86
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv-ltp.min.js"></script>
87
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv-gdaf.min.js"></script>
85
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv.min.js"></script>
86
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv-ltp.min.js"></script>
87
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv-gdaf.min.js"></script>
88
88
  <script>
89
89
  // Legal Token Protocol
90
90
  const propertyToken = bsv.createPropertyToken({
@@ -98,9 +98,9 @@ Everything in one file:
98
98
 
99
99
  #### **Security & Cryptography (~1.4MB total — each bundle is self-contained and re-embeds core BSV)**
100
100
  ```html
101
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv.min.js"></script>
102
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv-security.min.js"></script>
103
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv-shamir.min.js"></script>
101
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv.min.js"></script>
102
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv-security.min.js"></script>
103
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv-shamir.min.js"></script>
104
104
  <script>
105
105
  // Threshold Cryptography
106
106
  const shares = bsv.splitSecret('my_secret_key', 5, 3);
@@ -114,18 +114,18 @@ Everything in one file:
114
114
 
115
115
  | Module | Size | Purpose | CDN Link |
116
116
  |--------|------|---------|----------|
117
- | **bsv.min.js** | 937KB | Core BSV + SmartContract | `unpkg.com/@smartledger/bsv@5.1.0/bsv.min.js` |
118
- | **bsv.bundle.js** | 937KB | Everything in one file | `unpkg.com/@smartledger/bsv@5.1.0/bsv.bundle.js` |
119
- | **bsv-smartcontract.min.js** | 937KB | Complete covenant framework | `unpkg.com/@smartledger/bsv@5.1.0/bsv-smartcontract.min.js` |
120
- | **bsv-ltp.min.js** | 1184KB | **Legal Token Protocol** | `unpkg.com/@smartledger/bsv@5.1.0/bsv-ltp.min.js` |
121
- | **bsv-gdaf.min.js** | 1184KB | **Digital Identity & Attestation** | `unpkg.com/@smartledger/bsv@5.1.0/bsv-gdaf.min.js` |
122
- | **bsv-shamir.min.js** | 432KB | **Threshold Cryptography** | `unpkg.com/@smartledger/bsv@5.1.0/bsv-shamir.min.js` |
123
- | **bsv-security.min.js** | 26KB | Security enhancements (opt-in helpers — see README › Security) | `unpkg.com/@smartledger/bsv@5.1.0/bsv-security.min.js` |
124
- | **bsv-mnemonic.min.js** | 681KB | HD wallets | `unpkg.com/@smartledger/bsv@5.1.0/bsv-mnemonic.min.js` |
125
- | **bsv-ecies.min.js** | 71KB | Encryption | `unpkg.com/@smartledger/bsv@5.1.0/bsv-ecies.min.js` |
126
- | **bsv-covenant.min.js** | 913KB | Covenant operations | `unpkg.com/@smartledger/bsv@5.1.0/bsv-covenant.min.js` |
127
- | **bsv-script-helper.min.js** | 26KB | Custom script tools | `unpkg.com/@smartledger/bsv@5.1.0/bsv-script-helper.min.js` |
128
- | **bsv-message.min.js** | 26KB | Message signing | `unpkg.com/@smartledger/bsv@5.1.0/bsv-message.min.js` |
117
+ | **bsv.min.js** | 937KB | Core BSV + SmartContract | `unpkg.com/@smartledger/bsv@5.2.0/bsv.min.js` |
118
+ | **bsv.bundle.js** | 937KB | Everything in one file | `unpkg.com/@smartledger/bsv@5.2.0/bsv.bundle.js` |
119
+ | **bsv-smartcontract.min.js** | 937KB | Complete covenant framework | `unpkg.com/@smartledger/bsv@5.2.0/bsv-smartcontract.min.js` |
120
+ | **bsv-ltp.min.js** | 1184KB | **Legal Token Protocol** | `unpkg.com/@smartledger/bsv@5.2.0/bsv-ltp.min.js` |
121
+ | **bsv-gdaf.min.js** | 1184KB | **Digital Identity & Attestation** | `unpkg.com/@smartledger/bsv@5.2.0/bsv-gdaf.min.js` |
122
+ | **bsv-shamir.min.js** | 432KB | **Threshold Cryptography** | `unpkg.com/@smartledger/bsv@5.2.0/bsv-shamir.min.js` |
123
+ | **bsv-security.min.js** | 26KB | Security enhancements (opt-in helpers — see README › Security) | `unpkg.com/@smartledger/bsv@5.2.0/bsv-security.min.js` |
124
+ | **bsv-mnemonic.min.js** | 681KB | HD wallets | `unpkg.com/@smartledger/bsv@5.2.0/bsv-mnemonic.min.js` |
125
+ | **bsv-ecies.min.js** | 71KB | Encryption | `unpkg.com/@smartledger/bsv@5.2.0/bsv-ecies.min.js` |
126
+ | **bsv-covenant.min.js** | 913KB | Covenant operations | `unpkg.com/@smartledger/bsv@5.2.0/bsv-covenant.min.js` |
127
+ | **bsv-script-helper.min.js** | 26KB | Custom script tools | `unpkg.com/@smartledger/bsv@5.2.0/bsv-script-helper.min.js` |
128
+ | **bsv-message.min.js** | 26KB | Message signing | `unpkg.com/@smartledger/bsv@5.2.0/bsv-message.min.js` |
129
129
 
130
130
  ## ⚙️ **Development Environment Setup**
131
131
 
@@ -14,10 +14,10 @@ npm install @smartledger/bsv
14
14
  ### Browser CDN (Instant)
15
15
  ```html
16
16
  <!-- Core library (937KB) -->
17
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv.min.js"></script>
17
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv.min.js"></script>
18
18
 
19
19
  <!-- Everything included (937KB) -->
20
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv.bundle.js"></script>
20
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv.bundle.js"></script>
21
21
  ```
22
22
 
23
23
  ## 💰 **Your First Transaction (60 seconds)**
@@ -127,19 +127,19 @@ SmartLedger-BSV offers 12 different loading options - use only what you need:
127
127
 
128
128
  ```html
129
129
  <!-- Core BSV only (937KB) -->
130
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv.min.js"></script>
130
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv.min.js"></script>
131
131
 
132
132
  <!-- Smart contracts (937KB) -->
133
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv-smartcontract.min.js"></script>
133
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv-smartcontract.min.js"></script>
134
134
 
135
135
  <!-- Legal tokens (1.16MB) -->
136
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv-ltp.min.js"></script>
136
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv-ltp.min.js"></script>
137
137
 
138
138
  <!-- Digital identity (1.16MB) -->
139
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv-gdaf.min.js"></script>
139
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv-gdaf.min.js"></script>
140
140
 
141
141
  <!-- Everything (937KB) -->
142
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv.bundle.js"></script>
142
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv.bundle.js"></script>
143
143
  ```
144
144
 
145
145
  ## ⚡ **Key Advantages**
@@ -159,17 +159,17 @@ const recovered = bsv.reconstructSecret([shares[0], shares[2], shares[4]]);
159
159
  ### **New Modular Options**
160
160
  ```html
161
161
  <!-- Core compatibility (same size as bsv@1.5.6) -->
162
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv.min.js"></script>
162
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv.min.js"></script>
163
163
 
164
164
  <!-- Add smart contracts when ready -->
165
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv-smartcontract.min.js"></script>
165
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv-smartcontract.min.js"></script>
166
166
 
167
167
  <!-- Add advanced features as needed -->
168
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv-ltp.min.js"></script>
169
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv-gdaf.min.js"></script>
168
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv-ltp.min.js"></script>
169
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv-gdaf.min.js"></script>
170
170
 
171
171
  <!-- Everything in one file -->
172
- <script src="https://unpkg.com/@smartledger/bsv@5.1.0/bsv.bundle.js"></script>
172
+ <script src="https://unpkg.com/@smartledger/bsv@5.2.0/bsv.bundle.js"></script>
173
173
  ```
174
174
 
175
175
  ## 🔍 **Testing Your Migration**
@@ -1,25 +1,38 @@
1
1
  'use strict'
2
2
 
3
- var bsv = require('../../')
3
+ // Bitcore ECIES, backed by the audited @noble suite (secp256k1 ECDH, SHA-2,
4
+ // HMAC, AES-CBC) instead of the elliptic/aes-js primitives. The wire format is
5
+ // unchanged and byte-compatible with prior versions (locked by the golden
6
+ // known-answer vector in test/ecies/bitcore-ecies.js). The scheme is unchanged:
7
+ //
8
+ // S = X coord of (senderPriv * recipientPub) (ECDH, secp256k1)
9
+ // kE||kM = SHA-512(S) (KDF)
10
+ // c = IV || AES-256-CBC_PKCS7(kE, IV, message)
11
+ // d = HMAC-SHA256(kM, c) (encrypt-then-MAC)
12
+ // out = [Rpub] || c || d
13
+ //
14
+ // Public API is identical to bitcore-ecies so it is a drop-in: it accepts the
15
+ // same bsv PrivateKey/PublicKey objects. Only key *decoding* touches bsv; all
16
+ // cryptography is @noble.
17
+
18
+ var { secp256k1 } = require('@noble/curves/secp256k1.js')
19
+ var { sha512, sha256 } = require('@noble/hashes/sha2.js')
20
+ var { hmac } = require('@noble/hashes/hmac.js')
21
+ var { cbc } = require('@noble/ciphers/aes.js')
4
22
 
23
+ var bsv = require('../../')
5
24
  var PublicKey = bsv.PublicKey
6
- var Hash = bsv.crypto.Hash
7
25
  var $ = bsv.util.preconditions
8
- var aesjs = require('aes-js')
9
- var CBC = aesjs.ModeOfOperation.cbc
10
26
  var Random = bsv.crypto.Random
11
27
 
12
- // Constant-time buffer comparison. Works in Node and browser bundles alike
13
- // (crypto.timingSafeEqual is not provided by crypto-browserify). Compares the
14
- // full length unconditionally so timing does not leak how many bytes matched.
28
+ function buf (u8) { return Buffer.from(u8.buffer, u8.byteOffset, u8.byteLength) }
29
+ function hmacSha256 (key, data) { return buf(hmac(sha256, key, data)) }
30
+
31
+ // Constant-time buffer comparison (see bitcore-ecies for rationale).
15
32
  function constantTimeEqual (a, b) {
16
- if (a.length !== b.length) {
17
- return false
18
- }
33
+ if (a.length !== b.length) return false
19
34
  var diff = 0
20
- for (var i = 0; i < a.length; i++) {
21
- diff |= a[i] ^ b[i]
22
- }
35
+ for (var i = 0; i < a.length; i++) diff |= a[i] ^ b[i]
23
36
  return diff === 0
24
37
  }
25
38
 
@@ -30,11 +43,8 @@ AESCBC.encrypt = function (messagebuf, cipherkeybuf, ivbuf) {
30
43
  $.checkArgument(cipherkeybuf)
31
44
  $.checkArgument(ivbuf)
32
45
  ivbuf = ivbuf || Random.getRandomBuffer(128 / 8)
33
- messagebuf = aesjs.padding.pkcs7.pad(messagebuf)
34
- var aesCbc = new CBC(cipherkeybuf, ivbuf)
35
- var ctbuf = aesCbc.encrypt(messagebuf)
36
- var encbuf = Buffer.concat([ivbuf, ctbuf])
37
- return encbuf
46
+ var ct = cbc(Uint8Array.from(cipherkeybuf), Uint8Array.from(ivbuf)).encrypt(Uint8Array.from(messagebuf))
47
+ return Buffer.concat([Buffer.from(ivbuf), buf(ct)])
38
48
  }
39
49
 
40
50
  AESCBC.decrypt = function (encbuf, cipherkeybuf) {
@@ -42,33 +52,24 @@ AESCBC.decrypt = function (encbuf, cipherkeybuf) {
42
52
  $.checkArgument(cipherkeybuf)
43
53
  var ivbuf = encbuf.slice(0, 128 / 8)
44
54
  var ctbuf = encbuf.slice(128 / 8)
45
- var aesCbc = new CBC(cipherkeybuf, ivbuf)
46
- var messagebuf = aesCbc.decrypt(ctbuf)
47
- messagebuf = aesjs.padding.pkcs7.strip(messagebuf)
48
- return Buffer.from(messagebuf)
55
+ var msg = cbc(Uint8Array.from(cipherkeybuf), Uint8Array.from(ivbuf)).decrypt(Uint8Array.from(ctbuf))
56
+ return buf(msg)
49
57
  }
50
58
 
51
- // http://en.wikipedia.org/wiki/Integrated_Encryption_Scheme
52
59
  var ECIES = function ECIES (opts) {
53
- if (!(this instanceof ECIES)) {
54
- return new ECIES()
55
- }
60
+ if (!(this instanceof ECIES)) return new ECIES()
56
61
  this.opts = opts || {}
57
62
  }
58
63
 
59
64
  ECIES.prototype.privateKey = function (privateKey) {
60
65
  $.checkArgument(privateKey, 'no private key provided')
61
-
62
66
  this._privateKey = privateKey || null
63
-
64
67
  return this
65
68
  }
66
69
 
67
70
  ECIES.prototype.publicKey = function (publicKey) {
68
71
  $.checkArgument(publicKey, 'no public key provided')
69
-
70
72
  this._publicKey = publicKey || null
71
-
72
73
  return this
73
74
  }
74
75
 
@@ -79,79 +80,53 @@ var cachedProperty = function (name, getter) {
79
80
  enumerable: true,
80
81
  get: function () {
81
82
  var value = this[cachedName]
82
- if (!value) {
83
- value = this[cachedName] = getter.apply(this)
84
- }
83
+ if (!value) value = this[cachedName] = getter.apply(this)
85
84
  return value
86
85
  }
87
86
  })
88
87
  }
89
88
 
89
+ // Sender's compressed public key (R), via @noble from the raw private scalar.
90
90
  cachedProperty('Rbuf', function () {
91
- return this._privateKey.publicKey.toDER(true)
91
+ var priv = this._privateKey.bn.toBuffer({ size: 32 })
92
+ return buf(secp256k1.getPublicKey(Uint8Array.from(priv), true))
92
93
  })
93
94
 
95
+ // ECDH + KDF: S = X(priv * pub); kE||kM = SHA-512(S).
94
96
  cachedProperty('kEkM', function () {
95
- var r = this._privateKey.bn
96
- var KB = this._publicKey.point
97
- var P = KB.mul(r)
98
- var S = P.getX()
99
- var Sbuf = S.toBuffer({ size: 32 })
100
- return Hash.sha512(Sbuf)
101
- })
102
-
103
- cachedProperty('kE', function () {
104
- return this.kEkM.slice(0, 32)
97
+ var priv = this._privateKey.bn.toBuffer({ size: 32 })
98
+ var pub = this._publicKey.toDER() // SEC-encoded; @noble accepts either form
99
+ var shared = secp256k1.getSharedSecret(Uint8Array.from(priv), Uint8Array.from(pub), true)
100
+ var Sbuf = shared.slice(1) // drop the 02/03 prefix -> 32-byte X coordinate
101
+ return buf(sha512(Sbuf))
105
102
  })
106
103
 
107
- cachedProperty('kM', function () {
108
- return this.kEkM.slice(32, 64)
109
- })
104
+ cachedProperty('kE', function () { return this.kEkM.slice(0, 32) })
105
+ cachedProperty('kM', function () { return this.kEkM.slice(32, 64) })
110
106
 
111
- // Encrypts the message (String or Buffer).
112
- // Optional `ivbuf` contains 16-byte Buffer to be used in AES-CBC.
113
- // By default, `ivbuf` is computed deterministically from message and private key using HMAC-SHA256.
114
- // Deterministic IV enables end-to-end test vectors for alternative implementations.
115
- // Note that identical messages have identical ciphertexts. If your protocol does not include some
116
- // kind of a sequence identifier inside the message *and* it is important to not allow attacker to learn
117
- // that message is repeated, then you should use custom IV.
118
- // For random IV, pass `Random.getRandomBuffer(16)` for the second argument.
119
107
  ECIES.prototype.encrypt = function (message, ivbuf) {
120
108
  if (!Buffer.isBuffer(message)) message = Buffer.from(message)
121
109
  if (ivbuf === undefined) {
122
- ivbuf = Hash.sha256hmac(message, this._privateKey.toBuffer()).slice(0, 16)
110
+ ivbuf = hmacSha256(this._privateKey.toBuffer(), message).slice(0, 16)
123
111
  }
124
112
  var c = AESCBC.encrypt(message, this.kE, ivbuf)
125
- var d = Hash.sha256hmac(c, this.kM)
113
+ var d = hmacSha256(this.kM, c)
126
114
  if (this.opts.shortTag) d = d.slice(0, 4)
127
- var encbuf
128
- if (this.opts.noKey) {
129
- encbuf = Buffer.concat([c, d])
130
- } else {
131
- encbuf = Buffer.concat([this.Rbuf, c, d])
132
- }
133
- return encbuf
115
+ if (this.opts.noKey) return Buffer.concat([c, d])
116
+ return Buffer.concat([this.Rbuf, c, d])
134
117
  }
135
118
 
136
119
  ECIES.prototype.decrypt = function (encbuf) {
137
120
  $.checkArgument(encbuf)
138
121
  var offset = 0
139
- var tagLength = 32
140
- if (this.opts.shortTag) {
141
- tagLength = 4
142
- }
122
+ var tagLength = this.opts.shortTag ? 4 : 32
143
123
  if (!this.opts.noKey) {
144
124
  var pub
145
125
  switch (encbuf[0]) {
146
- case 4:
147
- pub = encbuf.slice(0, 65)
148
- break
126
+ case 4: pub = encbuf.slice(0, 65); break
149
127
  case 3:
150
- case 2:
151
- pub = encbuf.slice(0, 33)
152
- break
153
- default:
154
- throw new Error('Invalid type: ' + encbuf[0])
128
+ case 2: pub = encbuf.slice(0, 33); break
129
+ default: throw new Error('Invalid type: ' + encbuf[0])
155
130
  }
156
131
  this._publicKey = PublicKey.fromDER(pub)
157
132
  offset += pub.length
@@ -160,13 +135,9 @@ ECIES.prototype.decrypt = function (encbuf) {
160
135
  var c = encbuf.slice(offset, encbuf.length - tagLength)
161
136
  var d = encbuf.slice(encbuf.length - tagLength, encbuf.length)
162
137
 
163
- var d2 = Hash.sha256hmac(c, this.kM)
138
+ var d2 = hmacSha256(this.kM, c)
164
139
  if (this.opts.shortTag) d2 = d2.slice(0, 4)
165
-
166
- // Constant-time MAC comparison (encrypt-then-MAC verification).
167
- if (!constantTimeEqual(d, d2)) {
168
- throw new Error('Invalid checksum')
169
- }
140
+ if (!constantTimeEqual(d, d2)) throw new Error('Invalid checksum')
170
141
 
171
142
  return AESCBC.decrypt(c, this.kE)
172
143
  }