Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to generate a random secp256k1 private key #1021

Merged
merged 3 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
15 changes: 15 additions & 0 deletions packages/core/src/signers/privatekey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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);
}
manu0466 marked this conversation as resolved.
Show resolved Hide resolved

constructor(
provider: PrivateKeyProvider,
signingMode: SigningMode,
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/signers/privatekey.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
manu0466 marked this conversation as resolved.
Show resolved Hide resolved

it("connect successfully", async () => {
const signer = new PrivateKeySigner(
mockSecp256k1KeyProvider(),
Expand Down
20 changes: 20 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4928,13 +4928,15 @@ __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
"@types/pino": ^7.0.5
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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
Loading