Skip to content

Commit

Permalink
Merge pull request #462 from casper-ecosystem/feat-checksum-keys-and-…
Browse files Browse the repository at this point in the history
…fixes

Feat checksum keys and fixes
  • Loading branch information
Comp0te authored Dec 12, 2024
2 parents dc8a2af + 0e133aa commit 016ee84
Show file tree
Hide file tree
Showing 18 changed files with 697 additions and 75 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,14 @@ const transactionPayload = TransactionV1Payload.build({
pricingMode
});

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

const result = await rpcClient.putTransactionV1(transaction);
const tx = Transaction.fromTransactionV1(transactionV1);

const result = await rpcClient.putTransaction(tx);

console.log(`Transaction Hash: ${result.transactionHash}`);
```
Expand Down
25 changes: 9 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "casper-js-sdk",
"version": "5.0.0-rc6",
"version": "5.0.0-rc7",
"license": "Apache 2.0",
"description": "SDK to interact with the Casper blockchain",
"homepage": "https://github.com/casper-ecosystem/casper-js-sdk#README.md",
Expand Down Expand Up @@ -120,6 +120,7 @@
"@scure/bip32": "^1.1.5",
"@scure/bip39": "^1.2.0",
"@types/ws": "^8.2.2",
"axios": "^1.7.9",
"eventsource": "^2.0.2",
"glob": "^7.1.6",
"humanize-duration": "^3.24.0",
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
PurseIdentifier,
RpcRequest
} from './request';
import { TransactionV1, Deploy, PublicKey } from '../types';
import { Deploy, PublicKey, Transaction } from '../types';

export interface ClientPOS {
getLatestAuctionInfo(): Promise<StateGetAuctionInfoResult>;
Expand Down Expand Up @@ -226,7 +226,7 @@ export interface ClientInformational {

export interface ClientTransactional {
putDeploy(deploy: Deploy): Promise<PutDeployResult>;
putTransactionV1(transaction: TransactionV1): Promise<PutTransactionResult>;
putTransaction(transaction: Transaction): Promise<PutTransactionResult>;
}

export interface IClient
Expand Down
14 changes: 10 additions & 4 deletions src/rpc/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,30 @@ export class RpcError extends Error {
}

@jsonObject
export class HttpError extends Error {
export class HttpError<T extends Error = Error> extends Error {
@jsonMember({ constructor: Error })
sourceErr: Error;
sourceErr: T;

@jsonMember({ constructor: Number })
statusCode: number;

constructor(statusCode = 0, sourceErr: Error = new Error()) {
constructor(statusCode = 0, sourceErr: T) {
super(`Code: ${statusCode}, err: ${sourceErr.message}`);
this.sourceErr = sourceErr;
this.statusCode = statusCode;
}

unwrap(): Error {
unwrap(): T {
return this.sourceErr;
}

isNotFound(): boolean {
return this.statusCode === 404;
}

static isHttpError<E extends Error = Error>(
err: any | HttpError<E>
): err is HttpError<E> {
return err?.statusCode && err.sourceErr;
}
}
2 changes: 1 addition & 1 deletion src/rpc/http_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class HttpHandler implements IHandler {
};

try {
const response = await this.httpClient.request(config);
const response = await this.httpClient.request<RpcResponse>(config);

if (response.status < 200 || response.status >= 300) {
throw new HttpError(response.status, new Error(response.statusText));
Expand Down
5 changes: 4 additions & 1 deletion src/rpc/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,10 @@ export class PutDeployRequest {

@jsonObject
export class PutTransactionRequest {
@jsonMember({ constructor: TransactionWrapper })
@jsonMember({
constructor: TransactionWrapper,
serializer: val => TransactionWrapper.toJSON(val)
})
transaction: TransactionWrapper;

constructor(transaction: TransactionWrapper) {
Expand Down
20 changes: 8 additions & 12 deletions src/rpc/rpc_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ import {
import { IDValue } from './id_value';
import {
TransactionHash,
TransactionV1,
TransactionWrapper,
Deploy,
PublicKey,
Hash
Hash,
Transaction
} from '../types';
import { HttpError } from './error';

export class RpcClient implements IClient {
private handler: IHandler;
Expand Down Expand Up @@ -903,12 +903,12 @@ export class RpcClient implements IClient {
return result;
}

async putTransactionV1(
transaction: TransactionV1
async putTransaction(
transaction: Transaction
): Promise<PutTransactionResult> {
const serializer = new TypedJSON(PutTransactionRequest);
const transactionRequestParam = new PutTransactionRequest(
new TransactionWrapper(undefined, transaction)
transaction.getTransactionWrapper()
);

const resp = await this.processRequest<PutTransactionRequest>(
Expand Down Expand Up @@ -1284,13 +1284,9 @@ export class RpcClient implements IClient {
const resp = await this.handler.processCall(request);

if (resp.error) {
throw new Error(`RPC call failed, details: ${resp.error.message}`);
throw new HttpError(resp.error.code, resp.error);
}

try {
return resp;
} catch (err) {
throw new Error(`Error parsing result: ${err.message}`);
}
return resp;
}
}
8 changes: 4 additions & 4 deletions src/sse/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { jsonObject, jsonMember, jsonArrayMember, TypedJSON } from 'typedjson';

import {
Effect,
Effects,
ExecutionResult,
ExecutionResultV1,
Deploy,
Expand All @@ -17,7 +16,8 @@ import {
HexBytes,
Timestamp,
PublicKey,
Hash
Hash,
Transform
} from '../types';

export enum EventType {
Expand Down Expand Up @@ -758,8 +758,8 @@ export class StepPayload {
@jsonMember({ name: 'execution_effect', constructor: Effect })
executionEffect: Effect;

@jsonMember({ name: 'execution_effects', constructor: Effects })
executionEffects: Effects;
@jsonArrayMember(() => Transform, { name: 'execution_effects' })
executionEffects: Transform[];
}

@jsonObject
Expand Down
Loading

0 comments on commit 016ee84

Please sign in to comment.