@smartledger/bsv 5.0.1 → 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.
Files changed (44) hide show
  1. package/CHANGELOG.md +59 -0
  2. package/README.md +50 -50
  3. package/bsv-anchor.min.js +6 -7
  4. package/bsv-covenant.min.js +30 -18
  5. package/bsv-didweb.min.js +6 -7
  6. package/bsv-ecies.min.js +14 -6
  7. package/bsv-gdaf.min.js +27 -21
  8. package/bsv-ltp.min.js +27 -21
  9. package/bsv-message.min.js +4 -4
  10. package/bsv-mnemonic.min.js +6 -7
  11. package/bsv-script-helper.min.js +4 -4
  12. package/bsv-security.min.js +4 -4
  13. package/bsv-shamir.min.js +7 -8
  14. package/bsv-smartcontract.min.js +27 -21
  15. package/bsv-statuslist.min.js +8 -9
  16. package/bsv-vcjwt.min.js +6 -7
  17. package/bsv.bundle.js +27 -21
  18. package/bsv.min.js +27 -21
  19. package/build/webpack.anchor.config.js +4 -6
  20. package/build/webpack.base.js +99 -0
  21. package/build/webpack.bundle.config.js +6 -14
  22. package/build/webpack.config.js +7 -10
  23. package/build/webpack.covenant.config.js +5 -15
  24. package/build/webpack.didweb.config.js +4 -6
  25. package/build/webpack.ecies.config.js +15 -0
  26. package/build/webpack.gdaf.config.js +4 -6
  27. package/build/webpack.ltp.config.js +4 -6
  28. package/build/webpack.message.config.js +15 -0
  29. package/build/webpack.mnemonic.config.js +15 -0
  30. package/build/webpack.script-helper.config.js +5 -15
  31. package/build/webpack.security.config.js +7 -16
  32. package/build/webpack.shamir.config.js +15 -0
  33. package/build/webpack.smartcontract.config.js +5 -12
  34. package/build/webpack.statuslist.config.js +4 -6
  35. package/build/webpack.vcjwt.config.js +4 -6
  36. package/docs/MODULE_REFERENCE_COMPLETE.md +27 -27
  37. package/docs/advanced/UTXO_MANAGER_GUIDE.md +1 -1
  38. package/docs/getting-started/INSTALLATION.md +23 -23
  39. package/docs/getting-started/QUICK_START.md +7 -7
  40. package/docs/migration/FROM_BSV_1_5_6.md +5 -5
  41. package/lib/ecies/bitcore-ecies.js +53 -82
  42. package/lib/ecies/electrum-ecies.js +33 -73
  43. package/package.json +32 -19
  44. package/build/webpack.subproject.config.js +0 -6
@@ -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.0.1/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.0.1/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.0.1/bsv.min.js"></script>
75
- <script src="https://unpkg.com/@smartledger/bsv@5.0.1/bsv-covenant.min.js"></script>
76
- <script src="https://unpkg.com/@smartledger/bsv@5.0.1/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.0.1/bsv.min.js"></script>
86
- <script src="https://unpkg.com/@smartledger/bsv@5.0.1/bsv-ltp.min.js"></script>
87
- <script src="https://unpkg.com/@smartledger/bsv@5.0.1/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.0.1/bsv.min.js"></script>
102
- <script src="https://unpkg.com/@smartledger/bsv@5.0.1/bsv-security.min.js"></script>
103
- <script src="https://unpkg.com/@smartledger/bsv@5.0.1/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.0.1/bsv.min.js` |
118
- | **bsv.bundle.js** | 937KB | Everything in one file | `unpkg.com/@smartledger/bsv@5.0.1/bsv.bundle.js` |
119
- | **bsv-smartcontract.min.js** | 937KB | Complete covenant framework | `unpkg.com/@smartledger/bsv@5.0.1/bsv-smartcontract.min.js` |
120
- | **bsv-ltp.min.js** | 1184KB | **Legal Token Protocol** | `unpkg.com/@smartledger/bsv@5.0.1/bsv-ltp.min.js` |
121
- | **bsv-gdaf.min.js** | 1184KB | **Digital Identity & Attestation** | `unpkg.com/@smartledger/bsv@5.0.1/bsv-gdaf.min.js` |
122
- | **bsv-shamir.min.js** | 432KB | **Threshold Cryptography** | `unpkg.com/@smartledger/bsv@5.0.1/bsv-shamir.min.js` |
123
- | **bsv-security.min.js** | 26KB | Security enhancements (opt-in helpers — see README › Security) | `unpkg.com/@smartledger/bsv@5.0.1/bsv-security.min.js` |
124
- | **bsv-mnemonic.min.js** | 681KB | HD wallets | `unpkg.com/@smartledger/bsv@5.0.1/bsv-mnemonic.min.js` |
125
- | **bsv-ecies.min.js** | 71KB | Encryption | `unpkg.com/@smartledger/bsv@5.0.1/bsv-ecies.min.js` |
126
- | **bsv-covenant.min.js** | 913KB | Covenant operations | `unpkg.com/@smartledger/bsv@5.0.1/bsv-covenant.min.js` |
127
- | **bsv-script-helper.min.js** | 26KB | Custom script tools | `unpkg.com/@smartledger/bsv@5.0.1/bsv-script-helper.min.js` |
128
- | **bsv-message.min.js** | 26KB | Message signing | `unpkg.com/@smartledger/bsv@5.0.1/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.0.1/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.0.1/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.0.1/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.0.1/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.0.1/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.0.1/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.0.1/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.0.1/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.0.1/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.0.1/bsv-ltp.min.js"></script>
169
- <script src="https://unpkg.com/@smartledger/bsv@5.0.1/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.0.1/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
  }
@@ -1,15 +1,24 @@
1
1
  'use strict'
2
2
 
3
+ // Electrum BIE1 ECIES, backed by the audited @noble suite. Wire format unchanged
4
+ // and byte-compatible with prior versions (locked by the golden known-answer
5
+ // vector in test/ecies/electrum-ecies.js).
6
+
3
7
  var bsv = require('../../')
4
8
 
5
9
  var PublicKey = bsv.PublicKey
6
10
  var PrivateKey = bsv.PrivateKey
7
- var Hash = bsv.crypto.Hash
8
11
  var $ = bsv.util.preconditions
9
12
  var bitcoreECIES = require('./bitcore-ecies')
10
13
  var errors = require('./errors')
11
- var aesjs = require('aes-js')
12
- var CBC = aesjs.ModeOfOperation.cbc
14
+
15
+ var { secp256k1 } = require('@noble/curves/secp256k1.js')
16
+ var { sha512, sha256 } = require('@noble/hashes/sha2.js')
17
+ var { hmac } = require('@noble/hashes/hmac.js')
18
+ var { cbc } = require('@noble/ciphers/aes.js')
19
+
20
+ function buf (u8) { return Buffer.from(u8.buffer, u8.byteOffset, u8.byteLength) }
21
+ function sha256hmac (data, key) { return buf(hmac(sha256, key, data)) }
13
22
 
14
23
  var AESCBC = function AESCBC () {}
15
24
 
@@ -19,10 +28,7 @@ AESCBC.encrypt = function (messagebuf, keybuf, ivbuf) {
19
28
  $.checkArgument(ivbuf)
20
29
  $.checkArgument(keybuf.length === 16, 'keybuf length must be 16')
21
30
  $.checkArgument(ivbuf.length === 16, 'ivbuf length must be 16')
22
- messagebuf = aesjs.padding.pkcs7.pad(messagebuf)
23
- var aesCbc = new CBC(keybuf, ivbuf)
24
- var encryptedBytes = aesCbc.encrypt(messagebuf)
25
- return Buffer.from(encryptedBytes)
31
+ return buf(cbc(Uint8Array.from(keybuf), Uint8Array.from(ivbuf)).encrypt(Uint8Array.from(messagebuf)))
26
32
  }
27
33
 
28
34
  AESCBC.decrypt = function (encbuf, keybuf, ivbuf) {
@@ -31,35 +37,14 @@ AESCBC.decrypt = function (encbuf, keybuf, ivbuf) {
31
37
  $.checkArgument(ivbuf)
32
38
  $.checkArgument(keybuf.length === 16, 'keybuf length must be 16')
33
39
  $.checkArgument(ivbuf.length === 16, 'ivbuf length must be 16')
34
- var aesCbc = new CBC(keybuf, ivbuf)
35
- var decryptedBytes = aesCbc.decrypt(encbuf)
36
- return Buffer.from(aesjs.padding.pkcs7.strip(decryptedBytes))
40
+ return buf(cbc(Uint8Array.from(keybuf), Uint8Array.from(ivbuf)).decrypt(Uint8Array.from(encbuf)))
37
41
  }
38
42
 
39
- // Electrum BIE1 ECIES
40
- // Difference from Original Bitcore ECIES:
41
- // BIE1:
42
- // 1.Secret S is compressed P(33 bytes), while Bitcore's Secret is Px(32 bytes). See ivkEkM.
43
- // 2.use AES-128-CBC instead of AES-256-CBC.
44
- // 3.IV is retrived from first 16 bytes of Hashed Secret Buffer. See iv
45
- // 4.key Encryption(kE) is retrived from 17-32 bytes of Hashed Secret Buffer, instead of 1-32 bytes. See kE
46
- // 5.a magic word 'BIE1' is used to identify message type.
47
- // 6.ephemeral key is introduced, so you can encrypt messages only with public key.
48
- // 7.HMAC is whole message.
49
- // Notice:
50
- // Electrum does not support shortTag nor noKey
51
- // Do NOT use those 2 options if you are to send a message to Electrum
52
- // Encrypted message is NOT recoverable if you use ephemeral key
53
- // Security:
54
- //
55
-
56
- // Default algorithm is set to BIE1, however original Bitcore ECIES is still preserved.
57
43
  var ECIES = function ECIES (opts, algorithm = 'BIE1') {
58
44
  if (algorithm !== 'BIE1') throw new errors.UnsupportAlgorithm(algorithm)
59
45
  if (!(this instanceof ECIES)) {
60
46
  return new ECIES(opts, algorithm)
61
47
  }
62
- // use ephemeral key if privateKey is not set.
63
48
  this._privateKey = new bsv.PrivateKey()
64
49
  this.opts = opts || {}
65
50
  this.opts.ephemeralKey = true
@@ -67,19 +52,15 @@ var ECIES = function ECIES (opts, algorithm = 'BIE1') {
67
52
 
68
53
  ECIES.prototype.privateKey = function (privateKey) {
69
54
  $.checkArgument(PrivateKey.isValid(privateKey), 'no private key provided')
70
-
71
55
  this._privateKey = PrivateKey(privateKey.toHex()) || null
72
56
  this.opts.ephemeralKey = false
73
-
74
57
  return this
75
58
  }
76
59
 
77
60
  ECIES.prototype.publicKey = function (publicKey) {
78
61
  $.checkArgument(PublicKey.isValid(publicKey), 'no public key provided')
79
-
80
62
  this._publicKey = PublicKey(publicKey.toString()) || null
81
63
  if (this._publicKey != null) this.opts.fixedPublicKey = true
82
-
83
64
  return this
84
65
  }
85
66
 
@@ -97,31 +78,21 @@ var defineProperty = function (name, getter) {
97
78
  }
98
79
 
99
80
  defineProperty('Rbuf', function () {
100
- return this._privateKey.publicKey.toDER(true)
81
+ var priv = this._privateKey.bn.toBuffer({ size: 32 })
82
+ return buf(secp256k1.getPublicKey(Uint8Array.from(priv), true))
101
83
  })
102
84
 
103
85
  defineProperty('ivkEkM', function () {
104
- var r = this._privateKey.bn
105
- var KB = this._publicKey.point
106
- var P = KB.mul(r)
107
- var S = PublicKey(P)
108
- var Sbuf = S.toBuffer()
109
- return Hash.sha512(Sbuf)
86
+ var priv = this._privateKey.bn.toBuffer({ size: 32 })
87
+ var pub = this._publicKey.toDER()
88
+ var Sbuf = secp256k1.getSharedSecret(Uint8Array.from(priv), Uint8Array.from(pub), true)
89
+ return buf(sha512(Sbuf))
110
90
  })
111
91
 
112
- defineProperty('iv', function () {
113
- return this.ivkEkM.slice(0, 16)
114
- })
115
-
116
- defineProperty('kE', function () {
117
- return this.ivkEkM.slice(16, 32)
118
- })
92
+ defineProperty('iv', function () { return this.ivkEkM.slice(0, 16) })
93
+ defineProperty('kE', function () { return this.ivkEkM.slice(16, 32) })
94
+ defineProperty('kM', function () { return this.ivkEkM.slice(32, 64) })
119
95
 
120
- defineProperty('kM', function () {
121
- return this.ivkEkM.slice(32, 64)
122
- })
123
-
124
- // Encrypts the message (String or Buffer).
125
96
  ECIES.prototype.encrypt = function (message) {
126
97
  if (!Buffer.isBuffer(message)) message = Buffer.from(message)
127
98
  var ciphertext = AESCBC.encrypt(message, this.kE, this.iv)
@@ -132,48 +103,37 @@ ECIES.prototype.encrypt = function (message) {
132
103
  } else {
133
104
  encbuf = Buffer.concat([BIE1, this.Rbuf, ciphertext])
134
105
  }
135
- var hmac = Hash.sha256hmac(encbuf, this.kM)
136
- if (this.opts.shortTag) hmac = hmac.slice(0, 4)
137
- return Buffer.concat([encbuf, hmac])
106
+ var tag = sha256hmac(encbuf, this.kM)
107
+ if (this.opts.shortTag) tag = tag.slice(0, 4)
108
+ return Buffer.concat([encbuf, tag])
138
109
  }
139
110
 
140
111
  ECIES.prototype.decrypt = function (encbuf) {
141
112
  $.checkArgument(Buffer.isBuffer(encbuf), 'ciphetext must be a buffer')
142
- var tagLength = 32
113
+ var tagLength = this.opts.shortTag ? 4 : 32
143
114
  var offset = 4
144
- if (this.opts.shortTag) {
145
- tagLength = 4
146
- }
147
115
  var magic = encbuf.slice(0, 4)
148
116
  if (!magic.equals(Buffer.from('BIE1'))) {
149
117
  throw new errors.DecryptionError('Invalid Magic')
150
118
  }
151
119
  if (!this.opts.noKey) {
152
- var pub
153
- // BIE1 use compressed public key, length is always 33.
154
- pub = encbuf.slice(4, 37)
120
+ var pub = encbuf.slice(4, 37)
155
121
  if (this.opts.fixedPublicKey) console.log('Notice: Overriding PublicKey in message. Consider use "noKey" option if you are not sending message to electrum and do not want to use ephemeral key')
156
122
  else this._publicKey = PublicKey.fromDER(pub)
157
123
  offset = 37
158
124
  }
159
125
 
160
126
  var ciphertext = encbuf.slice(offset, encbuf.length - tagLength)
161
- var hmac = encbuf.slice(encbuf.length - tagLength, encbuf.length)
127
+ var tag = encbuf.slice(encbuf.length - tagLength, encbuf.length)
162
128
 
163
- var hmac2 = Hash.sha256hmac(encbuf.slice(0, encbuf.length - tagLength), this.kM)
164
- if (this.opts.shortTag) hmac2 = hmac2.slice(0, 4)
129
+ var tag2 = sha256hmac(encbuf.slice(0, encbuf.length - tagLength), this.kM)
130
+ if (this.opts.shortTag) tag2 = tag2.slice(0, 4)
165
131
 
166
- // Constant-time tag comparison. Buffer.equals() short-circuits on the first
167
- // differing byte, which leaks how many leading bytes matched and enables a
168
- // forgery oracle on the MAC; compare every byte unconditionally instead
169
- // (mirrors the loop in bitcore-ecies.js).
170
- if (hmac.length !== hmac2.length) {
132
+ if (tag.length !== tag2.length) {
171
133
  throw new errors.DecryptionError('Invalid checksum')
172
134
  }
173
135
  var equal = 0
174
- for (var i = 0; i < hmac2.length; i++) {
175
- equal |= (hmac[i] ^ hmac2[i])
176
- }
136
+ for (var i = 0; i < tag2.length; i++) equal |= (tag[i] ^ tag2[i])
177
137
  if (equal !== 0) {
178
138
  throw new errors.DecryptionError('Invalid checksum')
179
139
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smartledger/bsv",
3
- "version": "5.0.1",
3
+ "version": "5.2.0",
4
4
  "description": "🚀 Complete Bitcoin SV development framework with legally-recognizable DID:web + W3C VC-JWT toolkit, Legal Token Protocol (LTP), Global Digital Attestation Framework (GDAF), StatusList2021 revocation, and 16 flexible loading options. Standards-based credentials with ES256/ES256K support, on-chain BSV anchoring, and comprehensive Bitcoin SV API. Perfect for legal tokens, verifiable credentials, DeFi, smart contracts, and secure Bitcoin applications.",
5
5
  "author": "SmartLedger Technology <hello@smartledger.technology> (https://smartledger.technology)",
6
6
  "homepage": "https://github.com/codenlighten/smartledger-bsv#readme",
@@ -40,22 +40,22 @@
40
40
  "smart-contracts": "node examples/smart_contract_templates.js",
41
41
  "smart-contracts:working": "node examples/final_working_contracts.js",
42
42
  "workflow": "node examples/complete_workflow_demo.js",
43
- "build-bsv": "NODE_OPTIONS=\"--openssl-legacy-provider\" webpack index.js --config build/webpack.config.js",
44
- "build-ecies": "NODE_OPTIONS=\"--openssl-legacy-provider\" webpack ecies/index.js --config build/webpack.subproject.config.js --output-library bsvEcies -o bsv-ecies.min.js",
45
- "build-message": "NODE_OPTIONS=\"--openssl-legacy-provider\" webpack message/index.js --config build/webpack.subproject.config.js --output-library bsvMessage -o bsv-message.min.js",
46
- "build-mnemonic": "NODE_OPTIONS=\"--openssl-legacy-provider\" webpack mnemonic/index.js --config build/webpack.subproject.config.js --output-library bsvMnemonic -o bsv-mnemonic.min.js",
47
- "build-shamir": "NODE_OPTIONS=\"--openssl-legacy-provider\" webpack shamir-entry.js --config build/webpack.subproject.config.js --output-library bsvShamir -o bsv-shamir.min.js",
48
- "build-ltp": "NODE_OPTIONS=\"--openssl-legacy-provider\" webpack ltp-entry.js --config build/webpack.ltp.config.js",
49
- "build-gdaf": "NODE_OPTIONS=\"--openssl-legacy-provider\" webpack gdaf-entry.js --config build/webpack.gdaf.config.js",
50
- "build-smartcontract": "NODE_OPTIONS=\"--openssl-legacy-provider\" webpack smartcontract-entry.js --config build/webpack.smartcontract.config.js",
51
- "build-covenant": "NODE_OPTIONS=\"--openssl-legacy-provider\" webpack covenant-entry.js --config build/webpack.covenant.config.js",
52
- "build-script-helper": "NODE_OPTIONS=\"--openssl-legacy-provider\" webpack script-helper-entry.js --config build/webpack.script-helper.config.js",
53
- "build-security": "NODE_OPTIONS=\"--openssl-legacy-provider\" webpack security-entry.js --config build/webpack.security.config.js",
54
- "build-didweb": "NODE_OPTIONS=\"--openssl-legacy-provider\" webpack didweb-entry.js --config build/webpack.didweb.config.js",
55
- "build-vcjwt": "NODE_OPTIONS=\"--openssl-legacy-provider\" webpack vcjwt-entry.js --config build/webpack.vcjwt.config.js",
56
- "build-statuslist": "NODE_OPTIONS=\"--openssl-legacy-provider\" webpack statuslist-entry.js --config build/webpack.statuslist.config.js",
57
- "build-anchor": "NODE_OPTIONS=\"--openssl-legacy-provider\" webpack anchor-entry.js --config build/webpack.anchor.config.js",
58
- "build-bundle": "NODE_OPTIONS=\"--openssl-legacy-provider\" webpack bundle-entry.js --config build/webpack.bundle.config.js",
43
+ "build-bsv": "webpack --config build/webpack.config.js",
44
+ "build-ecies": "webpack --config build/webpack.ecies.config.js",
45
+ "build-message": "webpack --config build/webpack.message.config.js",
46
+ "build-mnemonic": "webpack --config build/webpack.mnemonic.config.js",
47
+ "build-shamir": "webpack --config build/webpack.shamir.config.js",
48
+ "build-ltp": "webpack --config build/webpack.ltp.config.js",
49
+ "build-gdaf": "webpack --config build/webpack.gdaf.config.js",
50
+ "build-smartcontract": "webpack --config build/webpack.smartcontract.config.js",
51
+ "build-covenant": "webpack --config build/webpack.covenant.config.js",
52
+ "build-script-helper": "webpack --config build/webpack.script-helper.config.js",
53
+ "build-security": "webpack --config build/webpack.security.config.js",
54
+ "build-didweb": "webpack --config build/webpack.didweb.config.js",
55
+ "build-vcjwt": "webpack --config build/webpack.vcjwt.config.js",
56
+ "build-statuslist": "webpack --config build/webpack.statuslist.config.js",
57
+ "build-anchor": "webpack --config build/webpack.anchor.config.js",
58
+ "build-bundle": "webpack --config build/webpack.bundle.config.js",
59
59
  "build": "npm run build-bsv && npm run build-ecies && npm run build-message && npm run build-mnemonic && npm run build-shamir && npm run build-smartcontract",
60
60
  "build-specialized": "npm run build-covenant && npm run build-script-helper && npm run build-security",
61
61
  "build-credentials": "npm run build-didweb && npm run build-vcjwt && npm run build-statuslist && npm run build-anchor",
@@ -191,6 +191,9 @@
191
191
  "request": "browser-request"
192
192
  },
193
193
  "dependencies": {
194
+ "@noble/ciphers": "2.2.0",
195
+ "@noble/curves": "2.2.0",
196
+ "@noble/hashes": "2.2.0",
194
197
  "aes-js": "^3.1.2",
195
198
  "bn.js": "=4.11.9",
196
199
  "bs58": "=4.0.1",
@@ -202,15 +205,25 @@
202
205
  "unorm": "1.4.1"
203
206
  },
204
207
  "devDependencies": {
208
+ "assert": "^2.1.0",
205
209
  "brfs": "2.0.1",
210
+ "browserify-zlib": "^0.2.0",
211
+ "buffer": "^6.0.3",
206
212
  "chai": "4.2.0",
213
+ "crypto-browserify": "^3.12.1",
207
214
  "jose": "^5.10.0",
208
215
  "mocha": "^8.4.0",
209
216
  "nyc": "^14.1.1",
217
+ "path-browserify": "^1.0.1",
218
+ "process": "^0.11.10",
210
219
  "sinon": "7.2.3",
211
220
  "standard": "12.0.1",
212
- "webpack": "4.29.3",
213
- "webpack-cli": "^3.3.12"
221
+ "stream-browserify": "^3.0.0",
222
+ "terser-webpack-plugin": "^5.6.1",
223
+ "util": "^0.12.5",
224
+ "vm-browserify": "^1.1.2",
225
+ "webpack": "^5.107.2",
226
+ "webpack-cli": "^5.1.4"
214
227
  },
215
228
  "license": "MIT",
216
229
  "standard": {