Skip to content

Commit

Permalink
Make PrivateKey and PublicKye methods sync
Browse files Browse the repository at this point in the history
  • Loading branch information
Comp0te committed Dec 23, 2024
1 parent b988b0e commit 636a7df
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 54 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ const transaction = new NativeTransferBuilder()
.payment(100_000_000)
.build();

await transaction.sign(privateKey);
transaction.sign(privateKey);

try {
const result = await rpcClient.putTransaction(transaction);
Expand Down Expand Up @@ -194,7 +194,7 @@ deployHeader.account = senderKey.publicKey;
deployHeader.chainName = 'casper-test';

const deploy = Deploy.makeDeploy(deployHeader, payment, session);
await deploy.sign(senderKey);
deploy.sign(senderKey);

const result = await rpcClient.putDeploy(deploy);

Expand Down Expand Up @@ -226,7 +226,7 @@ const deploy = makeCsprTransferDeploy({
transferAmount: '2500000000' // 2.5 CSPR
});

await deploy.sign(privateKey);
deploy.sign(privateKey);

const rpcHandler = new HttpHandler('http://<Node Address>:7777/rpc');
const rpcClient = new RpcClient(rpcHandler);
Expand Down Expand Up @@ -263,7 +263,7 @@ const deploy = makeAuctionManagerDeploy({
amount: '500000000000' // 500 CSPR
});

await deploy.sign(privateKey);
deploy.sign(privateKey);

const rpcHandler = new HttpHandler('http://<Node Address>:7777/rpc');
const rpcClient = new RpcClient(rpcHandler);
Expand Down Expand Up @@ -300,7 +300,7 @@ const deploy = await makeCep18TransferDeploy({
paymentAmount: '3000000000' // 3 CSPR
});

await deploy.sign(privateKey);
deploy.sign(privateKey);

const rpcHandler = new HttpHandler('http://<Node Address>:7777/rpc');
const rpcClient = new RpcClient(rpcHandler);
Expand Down Expand Up @@ -339,7 +339,7 @@ const deploy = await makeNftTransferDeploy({
tokenId: 234
});

await deploy.sign(privateKey);
deploy.sign(privateKey);

const rpcHandler = new HttpHandler('http://<Node Address>:7777/rpc');
const rpcClient = new RpcClient(rpcHandler);
Expand Down
4 changes: 2 additions & 2 deletions migration-guide-v2-v5.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ const transactionPayload = TransactionV1Payload.build({
const transaction = TransactionV1.makeTransactionV1(
transactionPayload
);
await transaction.sign(privateKey);
transaction.sign(privateKey);
```

6. **Submit Transaction**:
Expand Down Expand Up @@ -301,7 +301,7 @@ const transaction = new NativeTransferBuilder()
.payment(100_000_000)
.build();

await transaction.sign(privateKey);
transaction.sign(privateKey);

try {
const result = await rpcClient.putTransaction(transaction);
Expand Down
4 changes: 2 additions & 2 deletions src/types/Deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ describe('Deploy', () => {

const payment = ExecutableDeployItem.standardPayment(paymentAmount);
let deploy = Deploy.makeDeploy(deployHeader, payment, executableDeployItem);
await deploy.sign(senderKey);
await deploy.sign(recipientKey);
deploy.sign(senderKey);
deploy.sign(recipientKey);

const json = Deploy.toJson(deploy);

Expand Down
4 changes: 2 additions & 2 deletions src/types/Deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ export class Deploy {
*
* @param keys The private key used to sign the deploy.
*/
public async sign(keys: PrivateKey): Promise<void> {
const signatureBytes = await keys.signAndAddAlgorithmBytes(
public sign(keys: PrivateKey): void {
const signatureBytes = keys.signAndAddAlgorithmBytes(
this.hash.toBytes()
);
const signature = new HexBytes(signatureBytes);
Expand Down
4 changes: 2 additions & 2 deletions src/types/Transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('Test Transaction', () => {
});

const transaction = TransactionV1.makeTransactionV1(transactionPayload);
await transaction.sign(keys);
transaction.sign(keys);

const transactionPaymentAmount = transaction.payload.fields.args.args
.get('amount')!
Expand Down Expand Up @@ -108,7 +108,7 @@ describe('Test Transaction', () => {
.payment(100_000_000)
.build();

await transaction.sign(sender);
transaction.sign(sender);

const transactionV1 = transaction.getTransactionV1()!;
const transactionPaymentAmount = transactionV1.payload.fields.args.args
Expand Down
8 changes: 4 additions & 4 deletions src/types/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ export class TransactionV1 {
* Signs the transaction using the provided private key.
* @param keys The private key to sign the transaction.
*/
async sign(keys: PrivateKey): Promise<void> {
const signatureBytes = await keys.signAndAddAlgorithmBytes(
sign(keys: PrivateKey): void {
const signatureBytes = keys.signAndAddAlgorithmBytes(
this.hash.toBytes()
);
const signature = new HexBytes(signatureBytes);
Expand Down Expand Up @@ -493,8 +493,8 @@ export class Transaction {
* Signs the transaction using the provided private key.
* @param key The private key to sign the transaction.
*/
async sign(key: PrivateKey): Promise<void> {
const signatureBytes = await key.signAndAddAlgorithmBytes(
sign(key: PrivateKey): void {
const signatureBytes = key.signAndAddAlgorithmBytes(
this.hash.toBytes()
);
this.setSignature(signatureBytes, key.publicKey);
Expand Down
4 changes: 2 additions & 2 deletions src/types/TransactionBuilder.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ const transaction = new NativeTransferBuilder()
.payment(100_000_000)
.build();

await transaction.sign(sender);
transaction.sign(sender);

// Create a contract call
const contractCallTransaction = new ContractCallBuilder()
Expand All @@ -169,5 +169,5 @@ const contractCallTransaction = new ContractCallBuilder()
.chainName('casper-net-1')
.build();

await contractCallTransaction.sign(sender);
contractCallTransaction.sign(sender);
```
34 changes: 17 additions & 17 deletions src/types/keypair/PrivateKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import { KeyAlgorithm } from './Algorithm';
*/
export interface PrivateKeyInternal {
/** Retrieves the public key bytes. */
publicKeyBytes(): Promise<Uint8Array>;
publicKeyBytes(): Uint8Array;
toBytes(): Uint8Array;

/**
* Signs a message using the private key.
* @param message - The message to sign.
* @returns A promise resolving to the signature bytes.
*/
sign(message: Uint8Array): Promise<Uint8Array>;
sign(message: Uint8Array): Uint8Array;

/** Converts the private key to PEM format. */
toPem(): string;
Expand Down Expand Up @@ -76,17 +76,17 @@ export class PrivateKey {
* @param msg - The message to sign.
* @returns A promise resolving to the signature bytes.
*/
public async sign(msg: Uint8Array): Promise<Uint8Array> {
return await this.priv.sign(msg);
public sign(msg: Uint8Array): Uint8Array {
return this.priv.sign(msg);
}

/**
* Signs a message using the private key and includes the algorithm byte in the signature.
* @param msg - The message to sign.
* @returns A promise resolving to the signature bytes with the algorithm byte.
*/
public async signAndAddAlgorithmBytes(msg: Uint8Array): Promise<Uint8Array> {
const signature = await this.priv.sign(msg);
public signAndAddAlgorithmBytes(msg: Uint8Array): Uint8Array {
const signature = this.priv.sign(msg);
const algBytes = Uint8Array.of(this.alg);
return concat([algBytes, signature]);
}
Expand All @@ -105,9 +105,9 @@ export class PrivateKey {
* @param algorithm - The cryptographic algorithm to use.
* @returns A promise resolving to a new PrivateKey instance.
*/
public static async generate(algorithm: KeyAlgorithm): Promise<PrivateKey> {
const priv = await PrivateKeyFactory.createPrivateKey(algorithm);
const pubBytes = await priv.publicKeyBytes();
public static generate(algorithm: KeyAlgorithm): PrivateKey {
const priv = PrivateKeyFactory.createPrivateKey(algorithm);
const pubBytes = priv.publicKeyBytes();
const algBytes = Uint8Array.of(algorithm);
const pub = PublicKey.fromBuffer(concat([algBytes, pubBytes]));
return new PrivateKey(algorithm, pub, priv);
Expand Down Expand Up @@ -139,15 +139,15 @@ export class PrivateKey {
* @param algorithm - The cryptographic algorithm to use.
* @returns A promise resolving to a PrivateKey instance.
*/
public static async fromHex(
public static fromHex(
key: string,
algorithm: KeyAlgorithm
): Promise<PrivateKey> {
const priv = await PrivateKeyFactory.createPrivateKeyFromHex(
): PrivateKey {
const priv = PrivateKeyFactory.createPrivateKeyFromHex(
key,
algorithm
);
const pubBytes = await priv.publicKeyBytes();
const pubBytes = priv.publicKeyBytes();
const algBytes = Uint8Array.of(algorithm);
const pub = PublicKey.fromBuffer(concat([algBytes, pubBytes]));
return new PrivateKey(algorithm, pub, priv);
Expand All @@ -165,9 +165,9 @@ class PrivateKeyFactory {
* @returns A promise resolving to a PrivateKeyInternal instance.
* @throws Error if the algorithm is unsupported.
*/
public static async createPrivateKey(
public static createPrivateKey(
algorithm: KeyAlgorithm
): Promise<PrivateKeyInternal> {
): PrivateKeyInternal {
switch (algorithm) {
case KeyAlgorithm.ED25519:
return Ed25519PrivateKey.generate();
Expand Down Expand Up @@ -206,10 +206,10 @@ class PrivateKeyFactory {
* @returns A promise resolving to a PrivateKeyInternal instance.
* @throws Error if the algorithm is unsupported.
*/
public static async createPrivateKeyFromHex(
public static createPrivateKeyFromHex(
key: string,
algorithm: KeyAlgorithm
): Promise<PrivateKeyInternal> {
): PrivateKeyInternal {
switch (algorithm) {
case KeyAlgorithm.ED25519:
return Ed25519PrivateKey.fromHex(key);
Expand Down
8 changes: 4 additions & 4 deletions src/types/keypair/PublicKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ interface PublicKeyInternal {
* @param sig - The signature to verify.
* @returns A promise that resolves to a boolean indicating the validity of the signature.
*/
verifySignature(message: Uint8Array, sig: Uint8Array): Promise<boolean>;
verifySignature(message: Uint8Array, sig: Uint8Array): boolean;
}

/**
Expand Down Expand Up @@ -233,15 +233,15 @@ export class PublicKey {
* @returns A promise that resolves to a boolean indicating the validity of the signature.
* @throws Error if the signature or public key is empty, or if the signature is invalid.
*/
async verifySignature(
verifySignature(
message: Uint8Array,
sig: Uint8Array
): Promise<boolean> {
): boolean {
if (sig.length <= 1) throw ErrEmptySignature;
if (!this.key) throw ErrEmptyPublicKey;

const sigWithoutAlgByte = sig.slice(1);
const signature = await this.key.verifySignature(
const signature = this.key.verifySignature(
message,
sigWithoutAlgByte
);
Expand Down
13 changes: 8 additions & 5 deletions src/types/keypair/ed25519/PrivateKey.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import * as ed25519 from '@noble/ed25519';
import { PrivateKeyInternal } from "../PrivateKey";
import { sha512 } from '@noble/hashes/sha512';

ed25519.utils.sha512Sync = (...m) => sha512(ed25519.utils.concatBytes(...m));

/**
* Represents an Ed25519 private key, supporting key generation, signing, and PEM encoding.
Expand All @@ -24,7 +27,7 @@ export class PrivateKey implements PrivateKeyInternal {
* Generates a new random Ed25519 private key.
* @returns A promise that resolves to a new PrivateKey instance.
*/
static async generate(): Promise<PrivateKey> {
static generate(): PrivateKey {
const keyPair = ed25519.utils.randomPrivateKey();
return new PrivateKey(keyPair);
}
Expand All @@ -33,8 +36,8 @@ export class PrivateKey implements PrivateKeyInternal {
* Retrieves the byte array of the associated public key.
* @returns A promise that resolves to the public key bytes.
*/
async publicKeyBytes(): Promise<Uint8Array> {
return ed25519.getPublicKey(this.key);
publicKeyBytes(): Uint8Array {
return ed25519.sync.getPublicKey(this.key);
}

toBytes(): Uint8Array {
Expand All @@ -46,8 +49,8 @@ export class PrivateKey implements PrivateKeyInternal {
* @param message - The message to sign.
* @returns A promise that resolves to the signature bytes.
*/
async sign(message: Uint8Array): Promise<Uint8Array> {
return ed25519.sign(message, this.key);
sign(message: Uint8Array): Uint8Array {
return ed25519.sync.sign(message, this.key);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/types/keypair/ed25519/PublicKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export class PublicKey {
verifySignature(
message: Uint8Array,
signature: Uint8Array
): Promise<boolean> {
return ed25519.verify(signature, message, this.key);
): boolean {
return ed25519.sync.verify(signature, message, this.key);
}

/**
Expand Down
12 changes: 8 additions & 4 deletions src/types/keypair/secp256k1/PrivateKey.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import * as secp256k1 from '@noble/secp256k1';
import { sha256 } from '@noble/hashes/sha256';
import { hmac } from '@noble/hashes/hmac';
import { PrivateKeyInternal } from "../PrivateKey";

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

/** PEM prefix for a private key. */
const PemPrivateKeyPrefix = '-----BEGIN PRIVATE KEY-----';

Expand All @@ -28,7 +32,7 @@ export class PrivateKey implements PrivateKeyInternal {
* Generates a new random secp256k1 private key.
* @returns A promise that resolves to a new PrivateKey instance.
*/
static async generate(): Promise<PrivateKey> {
static generate(): PrivateKey {
const privateKey = secp256k1.utils.randomPrivateKey();
return new PrivateKey(privateKey);
}
Expand All @@ -37,7 +41,7 @@ export class PrivateKey implements PrivateKeyInternal {
* Retrieves the byte array of the public key in compressed format.
* @returns A promise that resolves to the compressed public key bytes.
*/
async publicKeyBytes(): Promise<Uint8Array> {
publicKeyBytes(): Uint8Array {
return secp256k1.getPublicKey(this.key, true);
}

Expand All @@ -59,9 +63,9 @@ export class PrivateKey implements PrivateKeyInternal {
* @param message - The message to sign.
* @returns A promise that resolves to the signature bytes in compact format.
*/
async sign(message: Uint8Array): Promise<Uint8Array> {
sign(message: Uint8Array): Uint8Array {
const hash = sha256(message);
return await secp256k1.sign(hash, this.key, { der: false });
return secp256k1.signSync(hash, this.key, { der: false });
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/types/keypair/secp256k1/PublicKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ export class PublicKey {
* @param signature - The signature to verify. Supports both raw (64-byte R || S) and DER formats.
* @returns A promise that resolves to `true` if the signature is valid, or `false` otherwise.
*/
async verifySignature(
verifySignature(
message: Uint8Array,
signature: Uint8Array
): Promise<boolean> {
): boolean {
let compactSignature: Uint8Array;

if (signature.length === 64) {
Expand Down

0 comments on commit 636a7df

Please sign in to comment.