@bsv/sdk 1.2.7 → 1.2.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bsv/sdk",
3
- "version": "1.2.7",
3
+ "version": "1.2.8",
4
4
  "type": "module",
5
5
  "description": "BSV Blockchain Software Development Kit",
6
6
  "main": "dist/cjs/mod.js",
@@ -30,7 +30,7 @@ export default class PublicKey extends Point {
30
30
  * const myPrivKey = new PrivateKey(...)
31
31
  * const myPubKey = PublicKey.fromPrivateKey(myPrivKey)
32
32
  */
33
- static fromPrivateKey (key: PrivateKey): PublicKey {
33
+ static fromPrivateKey(key: PrivateKey): PublicKey {
34
34
  const c = new Curve()
35
35
  const p = c.g.mul(key)
36
36
  return new PublicKey(p.x, p.y)
@@ -46,7 +46,7 @@ export default class PublicKey extends Point {
46
46
  * @example
47
47
  * const myPubKey = PublicKey.fromString("03....")
48
48
  */
49
- static fromString (str: string): PublicKey {
49
+ static fromString(str: string): PublicKey {
50
50
  const p = Point.fromString(str)
51
51
  return new PublicKey(p.x, p.y)
52
52
  }
@@ -61,7 +61,7 @@ export default class PublicKey extends Point {
61
61
  * @example
62
62
  * const myPubKey = PublicKey.fromString("03....")
63
63
  */
64
- static fromDER (bytes: number[]): PublicKey {
64
+ static fromDER(bytes: number[]): PublicKey {
65
65
  const p = Point.fromDER(bytes)
66
66
  return new PublicKey(p.x, p.y)
67
67
  }
@@ -76,7 +76,7 @@ export default class PublicKey extends Point {
76
76
  * new PublicKey(point1);
77
77
  * new PublicKey('abc123', 'def456');
78
78
  */
79
- constructor (
79
+ constructor(
80
80
  x: Point | BigNumber | number | number[] | string | null,
81
81
  y: BigNumber | number | number[] | string | null = null,
82
82
  isRed: boolean = true
@@ -84,6 +84,12 @@ export default class PublicKey extends Point {
84
84
  if (x instanceof Point) {
85
85
  super(x.getX(), x.getY())
86
86
  } else {
87
+ // Common gotcha: constructing PublicKey with a DER value when you should use .fromString()
88
+ if (y === null && isRed === true && typeof x === 'string') {
89
+ if (x.length === 66 || x.length === 130) {
90
+ throw new Error('You are using the "new PublicKey()" constructor with a DER hex string. You need to use "PublicKey.fromString()" instead.')
91
+ }
92
+ }
87
93
  super(x, y, isRed)
88
94
  }
89
95
  }
@@ -102,7 +108,7 @@ export default class PublicKey extends Point {
102
108
  * const myPrivKey = new PrivateKey(...)
103
109
  * const sharedSecret = myPubKey.deriveSharedSecret(myPrivKey)
104
110
  */
105
- deriveSharedSecret (priv: PrivateKey): Point {
111
+ deriveSharedSecret(priv: PrivateKey): Point {
106
112
  if (!this.validate()) {
107
113
  throw new Error('Public key not valid for ECDH secret derivation')
108
114
  }
@@ -123,7 +129,7 @@ export default class PublicKey extends Point {
123
129
  * const mySignature = new Signature(...)
124
130
  * const isVerified = myPubKey.verify(myMessage, mySignature)
125
131
  */
126
- verify (msg: number[] | string, sig: Signature, enc?: 'hex' | 'utf8'): boolean {
132
+ verify(msg: number[] | string, sig: Signature, enc?: 'hex' | 'utf8'): boolean {
127
133
  const msgHash = new BigNumber(sha256(msg, enc), 16)
128
134
  return verify(msgHash, sig, this)
129
135
  }
@@ -138,7 +144,7 @@ export default class PublicKey extends Point {
138
144
  * @example
139
145
  * const derPublicKey = myPubKey.toDER()
140
146
  */
141
- toDER (enc?: 'hex' | undefined): number[] | string {
147
+ toDER(enc?: 'hex' | undefined): number[] | string {
142
148
  if (enc === 'hex') return this.encode(true, enc) as string
143
149
  return this.encode(true) as number[]
144
150
  }
@@ -151,7 +157,7 @@ export default class PublicKey extends Point {
151
157
  * @example
152
158
  * const publicKeyHash = pubkey.toHash()
153
159
  */
154
- toHash (enc?: 'hex'): number[] | string {
160
+ toHash(enc?: 'hex'): number[] | string {
155
161
  const pkh = hash160(this.encode(true))
156
162
  if (enc === 'hex') {
157
163
  return toHex(pkh)
@@ -173,7 +179,7 @@ export default class PublicKey extends Point {
173
179
  * const testnetAddress = pubkey.toAddress([0x6f])
174
180
  * const testnetAddress = pubkey.toAddress('testnet')
175
181
  */
176
- toAddress (prefix: number[] | string = [0x00]): string {
182
+ toAddress(prefix: number[] | string = [0x00]): string {
177
183
  if (typeof prefix === 'string') {
178
184
  if (prefix === 'testnet' || prefix === 'test') {
179
185
  prefix = [0x6f]
@@ -192,7 +198,7 @@ export default class PublicKey extends Point {
192
198
  * @param invoiceNumber The invoice number used to derive the child key
193
199
  * @returns The derived child key.
194
200
  */
195
- deriveChild (privateKey: PrivateKey, invoiceNumber: string): PublicKey {
201
+ deriveChild(privateKey: PrivateKey, invoiceNumber: string): PublicKey {
196
202
  const sharedSecret = this.deriveSharedSecret(privateKey)
197
203
  const invoiceNumberBin = toArray(invoiceNumber, 'utf8')
198
204
  const hmac = sha256hmac(sharedSecret.encode(true), invoiceNumberBin)
@@ -219,7 +225,7 @@ export default class PublicKey extends Point {
219
225
  * @example
220
226
  * const publicKey = Signature.fromMsgHashAndCompactSignature(msgHash, 'IMOl2mVKfDgsSsHT4uIYBNN4e...', 'base64');
221
227
  */
222
- static fromMsgHashAndCompactSignature (msgHash: BigNumber, signature: number[] | string, enc?: 'hex' | 'base64'): PublicKey {
228
+ static fromMsgHashAndCompactSignature(msgHash: BigNumber, signature: number[] | string, enc?: 'hex' | 'base64'): PublicKey {
223
229
  const data = toArray(signature, enc)
224
230
  if (data.length !== 65) {
225
231
  throw new Error('Invalid Compact Signature')
@@ -27,6 +27,12 @@ describe('PublicKey', () => {
27
27
  })
28
28
  })
29
29
 
30
+ describe('Constructor', () => {
31
+ it('Should throw when accidentally passing in DER', () => {
32
+ expect(() => new PublicKey('036af279b60aa437d48bb0e2ec0b0c6b5cfaa976663f1f08ad456fd7fff149321d')).toThrow()
33
+ })
34
+ })
35
+
30
36
  describe('Instance methods', () => {
31
37
  test('deriveSharedSecret should derive a shared secret Point', () => {
32
38
  const sharedSecret = publicKey.deriveSharedSecret(privateKey)