Skip to content

Commit

Permalink
Merge pull request #308 from casper-ecosystem/time-feature-2
Browse files Browse the repository at this point in the history
Time feature 2
  • Loading branch information
hoffmannjan authored Jun 1, 2023
2 parents 08d9996 + 4ee0c95 commit 958a065
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 20 deletions.
15 changes: 14 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,25 @@ All notable changes to casper-js-sdk.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
## 2.13.1

### Added

- New method `makeDeployWithAutoTimestamp` that fetches timestamp and then uses it to create deploy header. Recommended to use it in browsers env.

## 2.13.0

### Added

- Added `getEraSummary`
- Added `getEraSummaryByBlockHeight`
- Added Ed25519 HD Wallet support
- Added `timeout` param to all CasperServiceByJsonRPC methods
- Deterministic Asymmetric keys can be generated by using HD Wallet following [BIP-0032](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki)

### Changed

- Switched from `tweetnacl-ts` and `eccrypto` to `noble` libraries

## 2.12.1

Expand Down
123 changes: 109 additions & 14 deletions package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "casper-js-sdk",
"version": "2.12.1",
"version": "2.13.1",
"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 @@ -58,6 +58,7 @@
"@types/lodash": "^4.14.191",
"@types/mocha": "^5.2.7",
"@types/node": "^14.14.31",
"@types/node-fetch": "^2.6.4",
"@types/sinon": "^10.0.13",
"@typescript-eslint/eslint-plugin": "^5.52.0",
"@typescript-eslint/parser": "^5.52.0",
Expand Down Expand Up @@ -123,6 +124,7 @@
"humanize-duration": "^3.24.0",
"key-encoder": "^2.0.3",
"lodash": "^4.17.21",
"node-fetch": "^2.6.11",
"reflect-metadata": "^0.1.13",
"ts-results": "npm:@casperlabs/ts-results@^3.3.4",
"typedjson": "^1.6.0-rc2"
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const TIME_API_URL = `worldtimeapi.org/api/timezone/UTC`;
36 changes: 32 additions & 4 deletions src/lib/DeployUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ import { RuntimeArgs } from './RuntimeArgs';
import { DeployUtil, Keys } from './index';
import { AsymmetricKey, SignatureAlgorithm, validateSignature } from './Keys';
import { CasperClient } from './CasperClient';
import { TimeService } from '../services/TimeService';
import { DEFAULT_DEPLOY_TTL } from '../constants';
import { TIME_API_URL } from '../config';

const shortEnglishHumanizer = humanizeDuration.humanizer({
spacer: '',
Expand Down Expand Up @@ -1323,7 +1325,7 @@ export class DeployParams {
* @param gasPrice Conversion rate between the cost of Wasm opcodes and the motes sent by the payment code, where 1 mote = 1 * 10^-9 CSPR
* @param ttl Time that the `Deploy` will remain valid for, in milliseconds. The default value is 1800000, which is 30 minutes
* @param dependencies Hex-encoded `Deploy` hashes of deploys which must be executed before this one.
* @param timestamp If `timestamp` is empty, the current time will be used. Note that timestamp is UTC, not local.
* @param timestamp Note that timestamp is UTC, not local.
*/
constructor(
public accountPublicKey: CLPublicKey,
Expand All @@ -1337,9 +1339,6 @@ export class DeployParams {
d =>
dependencies.filter(t => encodeBase16(d) === encodeBase16(t)).length < 2
);
if (!timestamp) {
this.timestamp = Date.now();
}
}
}

Expand All @@ -1358,6 +1357,10 @@ export function makeDeploy(
const serializedBody = serializeBody(payment, session);
const bodyHash = byteHash(serializedBody);

if (!deployParam.timestamp) {
deployParam.timestamp = Date.now();
}

const header: DeployHeader = new DeployHeader(
deployParam.accountPublicKey,
deployParam.timestamp!,
Expand All @@ -1372,6 +1375,31 @@ export function makeDeploy(
return new Deploy(deployHash, header, payment, session, []);
}

/**
* Builds a `Deploy` object from `DeployParams`, session logic, and payment logic.
* If there is no timestamp in `DeployParams` it fetches it from the TimeService.
* Recommened to use in browser environment.
* @param deployParam The parameters of the deploy, see [DeployParams](#L1323)
* @param session The session logic of the deploy
* @param payment The payment logic of the deploy
* @returns A new `Deploy` object
*/
export async function makeDeployWithAutoTimestamp(
deployParam: DeployParams,
session: ExecutableDeployItem,
payment: ExecutableDeployItem
): Promise<Deploy> {
if (!deployParam.timestamp && typeof window !== 'undefined') {
const timeService = new TimeService(
`${location.protocol}//${TIME_API_URL}`
);
const { unixtime } = await timeService.getTime()
deployParam.timestamp = unixtime;
}

return makeDeploy(deployParam, session, payment);
}

/**
* Uses the provided key pair to sign the Deploy message
* @param deploy Either an unsigned `Deploy` object or one with other signatures
Expand Down
16 changes: 16 additions & 0 deletions src/services/TimeService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import fetch from 'node-fetch';

type TimeJSON = {
unixtime: number;
}

export class TimeService {
constructor(public url: string) {}

async getTime(): Promise<TimeJSON> {
const result = await fetch(this.url);
const json = await result.json();

return json as TimeJSON;
}
}

0 comments on commit 958a065

Please sign in to comment.