Skip to content

Commit 959324f

Browse files
committed
Replace key-encoder lib with equal internal implementation
1 parent c1dbdb0 commit 959324f

File tree

7 files changed

+292
-66
lines changed

7 files changed

+292
-66
lines changed

package-lock.json

Lines changed: 67 additions & 44 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "casper-js-sdk",
3-
"version": "5.0.0",
3+
"version": "5.0.1",
44
"license": "Apache 2.0",
55
"description": "SDK to interact with the Casper blockchain",
66
"homepage": "https://github.com/casper-ecosystem/casper-js-sdk#README.md",
@@ -50,13 +50,12 @@
5050
"pre-commit": "lint-staged"
5151
}
5252
},
53-
"overrides": {
54-
"elliptic": "6.6.1"
55-
},
5653
"devDependencies": {
5754
"@jsdevtools/coverage-istanbul-loader": "^3.0.5",
55+
"@types/bn.js": "^5.1.6",
5856
"@types/chai": "^4.1.7",
5957
"@types/chai-as-promised": "^7.1.5",
58+
"@types/elliptic": "^6.4.18",
6059
"@types/eventsource": "^1.1.15",
6160
"@types/humanize-duration": "^3.18.1",
6261
"@types/lodash": "^4.14.191",
@@ -111,6 +110,9 @@
111110
"webpack-cli": "^4.5.0",
112111
"webpack-node-externals": "^2.5.2"
113112
},
113+
"overrides": {
114+
"elliptic": "6.6.1"
115+
},
114116
"dependencies": {
115117
"@ethersproject/bignumber": "^5.0.8",
116118
"@ethersproject/bytes": "^5.0.5",
@@ -123,11 +125,13 @@
123125
"@scure/bip32": "^1.1.5",
124126
"@scure/bip39": "^1.2.0",
125127
"@types/ws": "^8.2.2",
126-
"axios": "^1.7.9",
128+
"asn1.js": "^5.4.1",
129+
"axios": "^1.8.4",
130+
"bn.js": "^5.2.1",
131+
"elliptic": "6.6.1",
127132
"eventsource": "^2.0.2",
128133
"glob": "^7.1.6",
129134
"humanize-duration": "^3.24.0",
130-
"key-encoder": "^2.0.3",
131135
"lodash": "^4.17.21",
132136
"node-fetch": "2.6.13",
133137
"reflect-metadata": "^0.1.13",

src/tests/types/EraSummary.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,12 @@ describe('EraSummary', () => {
108108
expect(
109109
jsonRes?.blockHash.toHex(),
110110
'Block hash does not match expected value'
111+
// @ts-ignore IDE issue
111112
).to.deep.equal(eraSummaryJson.block_hash);
112113

113114
const allocations = jsonRes!.storedValue!.eraInfo!.seigniorageAllocations;
114115
const jsonAllocations =
116+
// @ts-ignore IDE issue
115117
eraSummaryJson.stored_value.EraInfo.seigniorage_allocations;
116118

117119
allocations.forEach((summary, index) => {

src/tests/types/keypair/PrivateKey.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ describe('PrivateKey', () => {
3232
).to.deep.eq(signKeyPair.publicKey.toHex());
3333
});
3434

35+
it('should generate correct account from Secp256K1 PEM file', () => {
36+
const privateKeyInPem = PrivateKey.fromPem(
37+
`-----BEGIN EC PRIVATE KEY-----
38+
MHQCAQEEIBxq/oiMjAURmCRMhOP/5g7WF33rIKaORGkRoc9wQgRGoAcGBSuBBAAK
39+
oUQDQgAEe8qaKb3KxL2VXpkwrUlNBAHPQwQeU3unZLV8CdMLp2aGaoLUfprph8Yt
40+
cs0BmUY0rz+KBwlNMWYL9fR/hoFl8Q==
41+
-----END EC PRIVATE KEY-----`,
42+
KeyAlgorithm.SECP256K1
43+
);
44+
45+
expect(privateKeyInPem.publicKey.toHex()).to.equal('02037bca9a29bdcac4bd955e9930ad494d0401cf43041e537ba764b57c09d30ba766');
46+
});
47+
3548
it('should generate PEM file for Ed25519 correctly', () => {
3649
const naclKeyPair = PrivateKey.generate(KeyAlgorithm.ED25519);
3750
const publicKeyInPem = naclKeyPair.publicKey.toPem();

src/types/keypair/secp256k1/PrivateKey.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ import * as secp256k1 from '@noble/secp256k1';
22
import { sha256 } from '@noble/hashes/sha256';
33
import { hmac } from '@noble/hashes/hmac';
44
import { PrivateKeyInternal } from '../PrivateKey';
5-
import KeyEncoder from 'key-encoder';
65
import { Conversions } from '../../Conversions';
76
import { readBase64WithPEM } from '../utils';
7+
import { encodePrivate } from './encoders';
88

99
secp256k1.utils.hmacSha256Sync = (k, ...m) =>
1010
hmac(sha256, k, secp256k1.utils.concatBytes(...m));
1111

12-
export const keyEncoder = new KeyEncoder('secp256k1');
13-
1412
/**
1513
* Represents a secp256k1 private key, supporting key generation, signing, and PEM encoding.
1614
* The class offers static methods to create instances from bytes, hex, and PEM formats.
@@ -110,11 +108,7 @@ export class PrivateKey implements PrivateKeyInternal {
110108
* @returns A PEM-encoded string of the private key.
111109
*/
112110
toPem(): string {
113-
return keyEncoder.encodePrivate(
114-
Conversions.encodeBase16(this.key),
115-
'raw',
116-
'pem'
117-
);
111+
return encodePrivate(Conversions.encodeBase16(this.key), 'raw', 'pem');
118112
}
119113

120114
/**
@@ -126,11 +120,7 @@ export class PrivateKey implements PrivateKeyInternal {
126120
static fromPem(content: string): PrivateKey {
127121
const privateKeyBytes = readBase64WithPEM(content);
128122

129-
const rawKeyHex = keyEncoder.encodePrivate(
130-
Buffer.from(privateKeyBytes),
131-
'der',
132-
'raw'
133-
);
123+
const rawKeyHex = encodePrivate(Buffer.from(privateKeyBytes), 'der', 'raw');
134124

135125
return new PrivateKey(new Uint8Array(Buffer.from(rawKeyHex, 'hex')));
136126
}

src/types/keypair/secp256k1/PublicKey.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as secp256k1 from '@noble/secp256k1';
22
import { sha256 } from '@noble/hashes/sha256';
3-
import { keyEncoder } from './PrivateKey';
43
import { Conversions } from '../../Conversions';
54
import { readBase64WithPEM } from '../utils';
5+
import { encodePublic } from "./encoders";
66

77
/** The expected size of a secp256k1 public key in bytes. */
88
const PublicKeySize = 33;
@@ -36,7 +36,7 @@ export class PublicKey {
3636
* @returns A PEM compliant string containing this instance's public key
3737
*/
3838
public toPem(): string {
39-
return keyEncoder.encodePublic(
39+
return encodePublic(
4040
Conversions.encodeBase16(this.key),
4141
'raw',
4242
'pem'
@@ -81,7 +81,7 @@ export class PublicKey {
8181
static fromPem(content: string): PublicKey {
8282
const publicKeyBytes = readBase64WithPEM(content);
8383

84-
const rawKeyHex = keyEncoder.encodePublic(
84+
const rawKeyHex = encodePublic(
8585
Buffer.from(publicKeyBytes),
8686
'der',
8787
'raw'

0 commit comments

Comments
 (0)