From 87bafbf500eef2f0e61d6c0a84384e07525d4d7c Mon Sep 17 00:00:00 2001 From: Manuel Date: Fri, 9 Feb 2024 17:20:11 +0100 Subject: [PATCH 1/2] feat: allow to generate a random secp256k1 private key --- packages/core/package.json | 2 ++ packages/core/src/signers/privatekey.ts | 15 ++++++++++++++ .../core/src/signers/privatekey.unit.spec.ts | 5 +++++ yarn.lock | 20 +++++++++++++++++++ 4 files changed, 42 insertions(+) diff --git a/packages/core/package.json b/packages/core/package.json index 4d0736374..98d8ffc81 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -43,12 +43,14 @@ "@desmoslabs/desmjs-types": "workspace:packages/types", "cosmjs-types": "0.8.0", "cosmos-wallet": "^1.2.0", + "elliptic": "^6.5.4", "long": "^4.0.0" }, "devDependencies": { "@babel/core": "^7.23.9", "@babel/preset-env": "^7.23.9", "@babel/preset-typescript": "^7.23.3", + "@types/elliptic": "^6.4.18", "@types/jest": "^29.5.10", "@types/long": "^4.0.1", "@types/node": "^20.8.8", diff --git a/packages/core/src/signers/privatekey.ts b/packages/core/src/signers/privatekey.ts index 277e3b125..2ef56c1ae 100644 --- a/packages/core/src/signers/privatekey.ts +++ b/packages/core/src/signers/privatekey.ts @@ -11,9 +11,13 @@ import { DirectSignResponse, } from "@cosmjs/proto-signing"; import { fromHex } from "@cosmjs/encoding"; +import elliptic from "elliptic"; import { Observer, ObserverManager } from "../utils"; import { Signer, SignerStatus, SigningMode } from "./signer"; +// eslint-disable-next-line new-cap +const secp256k1 = new elliptic.ec("secp256k1"); + /** * Enum that represents the connection status of a PrivateKeyProvider. */ @@ -202,6 +206,17 @@ export class PrivateKeySigner extends Signer { ); } + /** + * Build the signer with a randomly generated secp256k1 private key. + * @param signingMode - Hex encoded private key, or raw private key bytes. + * @param options - Signer options. + */ + static generate(signMode: SigningMode, options?: PrivateKeySignerOptions) { + const keyPair = secp256k1.genKeyPair(); + const privateKey = keyPair.getPrivate("hex"); + return PrivateKeySigner.fromSecp256k1(privateKey, signMode, options); + } + constructor( provider: PrivateKeyProvider, signingMode: SigningMode, diff --git a/packages/core/src/signers/privatekey.unit.spec.ts b/packages/core/src/signers/privatekey.unit.spec.ts index 88d4c8a07..1440d6193 100644 --- a/packages/core/src/signers/privatekey.unit.spec.ts +++ b/packages/core/src/signers/privatekey.unit.spec.ts @@ -104,6 +104,11 @@ describe("PrivateKeySigner", () => { expect(signer.status).toBe(SignerStatus.NotConnected); }); + it("Generate random private key", async () => { + const signer = PrivateKeySigner.generate(SigningMode.DIRECT); + await signer.connect(); + }); + it("connect successfully", async () => { const signer = new PrivateKeySigner( mockSecp256k1KeyProvider(), diff --git a/yarn.lock b/yarn.lock index 03cd3b51f..586db90ac 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4928,6 +4928,7 @@ __metadata: "@cosmjs/tendermint-rpc": 0.31.3 "@cosmjs/utils": 0.31.3 "@desmoslabs/desmjs-types": "workspace:packages/types" + "@types/elliptic": ^6.4.18 "@types/jest": ^29.5.10 "@types/long": ^4.0.1 "@types/node": ^20.8.8 @@ -4935,6 +4936,7 @@ __metadata: babel-jest: ^29.7.0 cosmjs-types: 0.8.0 cosmos-wallet: ^1.2.0 + elliptic: ^6.5.4 jest: ^29.7.0 long: ^4.0.0 ts-jest: ^29.1.1 @@ -8833,6 +8835,15 @@ __metadata: languageName: node linkType: hard +"@types/bn.js@npm:*": + version: 5.1.5 + resolution: "@types/bn.js@npm:5.1.5" + dependencies: + "@types/node": "*" + checksum: c87b28c4af74545624f8a3dae5294b16aa190c222626e8d4b2e327b33b1a3f1eeb43e7a24d914a9774bca43d8cd6e1cb0325c1f4b3a244af6693a024e1d918e6 + languageName: node + linkType: hard + "@types/body-parser@npm:*": version: 1.19.2 resolution: "@types/body-parser@npm:1.19.2" @@ -8880,6 +8891,15 @@ __metadata: languageName: node linkType: hard +"@types/elliptic@npm:^6.4.18": + version: 6.4.18 + resolution: "@types/elliptic@npm:6.4.18" + dependencies: + "@types/bn.js": "*" + checksum: c27613c530fb95441e5e6b456c8c9bc26568ca14c546aae6d7c1d8d46869f79a2272feaef266ac00bdb68b2671e6351ed01b91b82266eac30ca9092720825d16 + languageName: node + linkType: hard + "@types/eslint-scope@npm:^3.7.3": version: 3.7.4 resolution: "@types/eslint-scope@npm:3.7.4" From a09d166cdcdd5686ed0d1e2c07b2c4fc953dea51 Mon Sep 17 00:00:00 2001 From: Manuel Date: Mon, 12 Feb 2024 10:50:46 +0100 Subject: [PATCH 2/2] docs: fix documentation --- packages/core/src/signers/privatekey.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/core/src/signers/privatekey.ts b/packages/core/src/signers/privatekey.ts index 2ef56c1ae..b740bbe09 100644 --- a/packages/core/src/signers/privatekey.ts +++ b/packages/core/src/signers/privatekey.ts @@ -208,7 +208,8 @@ export class PrivateKeySigner extends Signer { /** * Build the signer with a randomly generated secp256k1 private key. - * @param signingMode - Hex encoded private key, or raw private key bytes. + * @param signingMode - Signer sign mode, this can be {@link SigningMode.AMINO} + * or {@link SigningMode.DIRECT}. * @param options - Signer options. */ static generate(signMode: SigningMode, options?: PrivateKeySignerOptions) {