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 sync keys and fixes #480

Merged
merged 4 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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 @@ -59,8 +59,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
21 changes: 14 additions & 7 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 Expand Up @@ -566,9 +566,16 @@ export class Transaction {
throw new Error("The JSON can't be parsed as a Transaction.");
}

static toJSON(tx: Transaction) {
const serializer = new TypedJSON(Transaction);
return serializer.toPlainJson(tx);
toJSON() {
if (this.originTransactionV1) {
return TransactionV1.toJSON(this.originTransactionV1);
}

if (this.originDeployV1) {
return Deploy.toJSON(this.originDeployV1)
}

throw new Error('Incorrect Transaction instance. Missing origin value');
}
}

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);
```
4 changes: 4 additions & 0 deletions src/types/clvalue/List.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ export class CLValueList {
bytes: innerBytes
} = CLValueParser.fromBytesByType(remainder, clType.elementsType);

if (!inner) {
continue;
}

elements.push(inner);
remainder = innerBytes;
}
Expand Down
10 changes: 7 additions & 3 deletions src/types/clvalue/Map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export class CLValueMap {

const { result: u32, bytes: u32Bytes } = CLValueUInt32.fromBytes(bytes);
const size = u32.toNumber();
let remainder = u32Bytes;
const remainder = u32Bytes;

if (size === 0) {
return { result: mapResult, bytes: remainder };
Expand All @@ -205,11 +205,15 @@ export class CLValueMap {
if (remainder.length) {
const keyVal = CLValueParser.fromBytesByType(remainder, mapType.key);

remainder = keyVal?.bytes;
if (!keyVal.result) {
continue;
}

const valVal = CLValueParser.fromBytesByType(remainder, mapType.val);

remainder = valVal.bytes;
if (!valVal.result) {
continue;
}

mapResult.append(keyVal?.result, valVal?.result);
}
Expand Down
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
Loading
Loading