Skip to content

Commit 1210884

Browse files
fix: correct secp256k1 key length detection in publicKeyFromRaw (#2697)
- Resolves #2696 --------- Co-authored-by: achingbrain <[email protected]>
1 parent c435271 commit 1210884

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

packages/crypto/src/keys/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export function publicKeyFromProtobuf (buf: Uint8Array): PublicKey {
8585
export function publicKeyFromRaw (buf: Uint8Array): PublicKey {
8686
if (buf.byteLength === 32) {
8787
return unmarshalEd25519PublicKey(buf)
88-
} else if (buf.byteLength === 34) {
88+
} else if (buf.byteLength === 33) {
8989
return unmarshalSecp256k1PublicKey(buf)
9090
} else {
9191
return pkixToRSAPublicKey(buf)

packages/crypto/test/keys/ed25519.spec.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Uint8ArrayList } from 'uint8arraylist'
44
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
55
import { randomBytes } from '../../src/index.js'
66
import { unmarshalEd25519PrivateKey, unmarshalEd25519PublicKey } from '../../src/keys/ed25519/utils.js'
7-
import { generateKeyPair, generateKeyPairFromSeed, privateKeyFromProtobuf, privateKeyFromRaw, publicKeyFromProtobuf } from '../../src/keys/index.js'
7+
import { generateKeyPair, generateKeyPairFromSeed, privateKeyFromProtobuf, privateKeyFromRaw, publicKeyFromProtobuf, publicKeyFromRaw } from '../../src/keys/index.js'
88
import fixtures from '../fixtures/go-key-ed25519.js'
99
import { testGarbage } from '../helpers/test-garbage-error-handling.js'
1010
import type { Ed25519PrivateKey } from '@libp2p/interface'
@@ -142,13 +142,20 @@ describe('ed25519', function () {
142142
testGarbage('unmarshalPrivateKey', privateKeyFromProtobuf)
143143
})
144144

145-
it('imports from raw', async () => {
145+
it('imports private key from raw', async () => {
146146
const key = await generateKeyPair('Ed25519')
147147
const imported = privateKeyFromRaw(key.raw)
148148

149149
expect(key.equals(imported)).to.be.true()
150150
})
151151

152+
it('imports public key from raw', async () => {
153+
const key = await generateKeyPair('Ed25519')
154+
const imported = publicKeyFromRaw(key.publicKey.raw)
155+
156+
expect(key.publicKey.equals(imported)).to.be.true()
157+
})
158+
152159
describe('go interop', () => {
153160
// @ts-check
154161
it('verifies with data from go', async () => {

packages/crypto/test/keys/rsa.spec.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { expect } from 'aegir/chai'
44
import { Uint8ArrayList } from 'uint8arraylist'
55
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
66
import { randomBytes } from '../../src/index.js'
7-
import { generateKeyPair, privateKeyFromProtobuf, privateKeyFromRaw, publicKeyFromProtobuf } from '../../src/keys/index.js'
7+
import { generateKeyPair, privateKeyFromProtobuf, privateKeyFromRaw, publicKeyFromProtobuf, publicKeyFromRaw } from '../../src/keys/index.js'
88
import { MAX_RSA_KEY_SIZE, pkcs1ToRSAPrivateKey, pkixToRSAPublicKey } from '../../src/keys/rsa/utils.js'
99
import fixtures from '../fixtures/go-key-rsa.js'
1010
import { testGarbage } from '../helpers/test-garbage-error-handling.js'
@@ -101,13 +101,20 @@ describe('RSA', function () {
101101
expect(privateKey.raw).to.equalBytes(pkcs1)
102102
})
103103

104-
it('imports from raw', async () => {
104+
it('imports private key from raw', async () => {
105105
const key = await generateKeyPair('RSA', 512)
106106
const imported = privateKeyFromRaw(key.raw)
107107

108108
expect(key.equals(imported)).to.be.true()
109109
})
110110

111+
it('imports public key from raw', async () => {
112+
const key = await generateKeyPair('RSA', 512)
113+
const imported = publicKeyFromRaw(key.publicKey.raw)
114+
115+
expect(key.publicKey.equals(imported)).to.be.true()
116+
})
117+
111118
describe('key equals', () => {
112119
it('equals itself', () => {
113120
expect(key.equals(key)).to.be.true()

packages/crypto/test/keys/secp256k1.spec.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { expect } from 'aegir/chai'
44
import { Uint8ArrayList } from 'uint8arraylist'
55
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
66
import { randomBytes } from '../../src/index.js'
7-
import { generateKeyPair, privateKeyFromRaw, privateKeyToProtobuf, publicKeyToProtobuf } from '../../src/keys/index.js'
7+
import { generateKeyPair, privateKeyFromRaw, privateKeyToProtobuf, publicKeyFromRaw, publicKeyToProtobuf } from '../../src/keys/index.js'
88
import { KeyType, PrivateKey, PublicKey } from '../../src/keys/keys.js'
99
import { hashAndSign, hashAndVerify } from '../../src/keys/secp256k1/index.js'
1010
import { unmarshalSecp256k1PrivateKey, unmarshalSecp256k1PublicKey, compressSecp256k1PublicKey, computeSecp256k1PublicKey, decompressSecp256k1PublicKey, generateSecp256k1PrivateKey, validateSecp256k1PrivateKey, validateSecp256k1PublicKey } from '../../src/keys/secp256k1/utils.js'
@@ -69,13 +69,20 @@ describe('secp256k1 keys', () => {
6969
expect(key.publicKey.toString()).to.equal('16Uiu2HAm5vpzEwJ41kQmnwDu9moFusdc16wV1oCUd1AHLgFgPpKY')
7070
})
7171

72-
it('imports from raw', async () => {
72+
it('imports private key from raw', async () => {
7373
const key = await generateKeyPair('secp256k1')
7474
const imported = privateKeyFromRaw(key.raw)
7575

7676
expect(key.equals(imported)).to.be.true()
7777
})
7878

79+
it('imports public key from raw', async () => {
80+
const key = await generateKeyPair('secp256k1')
81+
const imported = publicKeyFromRaw(key.publicKey.raw)
82+
83+
expect(key.publicKey.equals(imported)).to.be.true()
84+
})
85+
7986
describe('key equals', () => {
8087
it('equals itself', () => {
8188
expect(key.equals(key)).to.be.true()

0 commit comments

Comments
 (0)