Skip to content

Commit

Permalink
Merge pull request #100 from casper-ecosystem/develop-next
Browse files Browse the repository at this point in the history
casper-js-sdk 2.5.1
  • Loading branch information
hoffmannjan authored Sep 7, 2021
2 parents 6ca3d2b + e9b7378 commit 7d33be4
Show file tree
Hide file tree
Showing 11 changed files with 234 additions and 75 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ 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).

## 2.5.1

### Fixed

- Added stronger validation to `PublicKey.fromHex` method
- Fix for deploy's `execution_result` type signatures
- Fix instanceof problem in `CLValueParser` which caused problems when two different versions of SDK was used in one project
- Signer methods fixes

## 2.5.0

### Added
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "casper-js-sdk",
"version": "2.5.0",
"version": "2.5.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
17 changes: 15 additions & 2 deletions src/@types/casperlabsSigner.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { JsonTypes } from 'typedjson';

interface CasperLabsHelper {
/**
* Returns Signer version
Expand All @@ -22,10 +24,21 @@ interface CasperLabsHelper {
* @param targetPublicKeyHex public key in hex format with algorithm prefix. Used to display hex-formatted address on the UI
*/
sign: (
deploy: any,
deploy: { deploy: JsonTypes },
sourcePublicKeyHex: string,
targetPublicKeyHex: string
) => Promise<JSON>;
) => Promise<{ deploy: JsonTypes }>;

/**
* Send raw string message to Signer for signing.
* @param message string to be signed.
* @param signingPublicKey public key in hex format, the corresponding secret key (from the vault) will be used to sign.
* @returns `Base16` signature
*/
signMessage: (
rawMessage: string,
signingPublicKey: string
) => Promise<string>;

/*
* Returns base64 encoded public key of user current selected account.
Expand Down
3 changes: 3 additions & 0 deletions src/lib/CLValue/PublicKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ export class CLPublicKey extends CLValue {
if (publicKeyHex.length < 2) {
throw new Error('Asymmetric key error: too short');
}
if (!/^0(1[0-9a-f]{64}|2[0-9a-f]{66})$/.test(publicKeyHex)) {
throw new Error('Invalid public key');
}
const publicKeyHexBytes = decodeBase16(publicKeyHex);

return new CLPublicKey(publicKeyHexBytes.subarray(1), publicKeyHexBytes[0]);
Expand Down
48 changes: 24 additions & 24 deletions src/lib/CLValue/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import {
CLOptionBytesParser,
CLResultType,
CLResultBytesParser,
CLTupleType,
CLTuple1Type,
CLTuple2Type,
CLTuple3Type,
Expand Down Expand Up @@ -157,67 +156,68 @@ export const matchTypeToCLType = (type: any): CLType => {
export const matchByteParserByCLType = (
val: CLType
): Result<CLValueBytesParsers, string> => {
if (val instanceof CLBoolType) {
if (val.tag === CLTypeTag.Bool) {
return Ok(new CLBoolBytesParser());
}
if (val instanceof CLI32Type) {
if (val.tag === CLTypeTag.I32) {
return Ok(new CLI32BytesParser());
}
if (val instanceof CLI64Type) {
if (val.tag === CLTypeTag.I64) {
return Ok(new CLI64BytesParser());
}
if (val instanceof CLU8Type) {
if (val.tag === CLTypeTag.U8) {
return Ok(new CLU8BytesParser());
}
if (val instanceof CLU32Type) {
if (val.tag === CLTypeTag.U32) {
return Ok(new CLU32BytesParser());
}
if (val instanceof CLU64Type) {
if (val.tag === CLTypeTag.U64) {
return Ok(new CLU64BytesParser());
}
if (val instanceof CLU128Type) {
if (val.tag === CLTypeTag.U128) {
return Ok(new CLU128BytesParser());
}
if (val instanceof CLU256Type) {
if (val.tag === CLTypeTag.U256) {
return Ok(new CLU256BytesParser());
}
if (val instanceof CLU512Type) {
if (val.tag === CLTypeTag.U512) {
return Ok(new CLU512BytesParser());
}
if (val instanceof CLByteArrayType) {
if (val.tag === CLTypeTag.ByteArray) {
return Ok(new CLByteArrayBytesParser());
}
if (val instanceof CLByteArrayType) {
return Ok(new CLByteArrayBytesParser());
}
if (val instanceof CLURefType) {
if (val.tag === CLTypeTag.URef) {
return Ok(new CLURefBytesParser());
}
if (val instanceof CLKeyType) {
if (val.tag === CLTypeTag.Key) {
return Ok(new CLKeyBytesParser());
}
if (val instanceof CLPublicKeyType) {
if (val.tag === CLTypeTag.PublicKey) {
return Ok(new CLPublicKeyBytesParser());
}
if (val instanceof CLListType) {
if (val.tag === CLTypeTag.List) {
return Ok(new CLListBytesParser());
}
if (val instanceof CLMapType) {
if (val.tag === CLTypeTag.Map) {
return Ok(new CLMapBytesParser());
}
if (val instanceof CLTupleType) {
if (
val.tag === CLTypeTag.Tuple1 ||
val.tag === CLTypeTag.Tuple2 ||
val.tag === CLTypeTag.Tuple3
) {
return Ok(new CLTupleBytesParser());
}
if (val instanceof CLOptionType) {
if (val.tag === CLTypeTag.Option) {
return Ok(new CLOptionBytesParser());
}
if (val instanceof CLResultType) {
if (val.tag === CLTypeTag.Result) {
return Ok(new CLResultBytesParser());
}
if (val instanceof CLStringType) {
if (val.tag === CLTypeTag.String) {
return Ok(new CLStringBytesParser());
}
if (val instanceof CLUnitType) {
if (val.tag === CLTypeTag.Unit) {
return Ok(new CLUnitBytesParser());
}
return Err('Unknown type');
Expand Down
32 changes: 27 additions & 5 deletions src/lib/SignedMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,40 @@ import { sha256 } from 'ethereum-cryptography/sha256';
import { CLPublicKey } from './CLValue/';
import { AsymmetricKey } from './Keys';

/**
* Method for formatting messages with Casper header.
* @param message The string to be formatted.
* @returns The bytes of the formatted message
*/
export const formatMessageWithHeaders = (message: string): Uint8Array => {
// Avoiding usage of Text Encoder lib to support legacy nodejs versions.
return Uint8Array.from(Buffer.from(`Casper Message:\n${message}`));
};

/**
* Method for signing string message.
* @param key AsymmetricKey used to sign the message
* @param message Message that will be signed
* @return Uint8Array Signature in byte format
*/
export const signMessage = (
export const signRawMessage = (
key: AsymmetricKey,
message: string
): Uint8Array => {
const messageWithHeader = Buffer.from(`Casper Message:\n${message}`);
return key.sign(messageWithHeader);
return key.sign(formatMessageWithHeaders(message));
};

/**
* Method for signing formatted message in bytes format.
* @param key AsymmetricKey used to sign the message
* @param formattedMessageBytes Bytes of the formatted message. (Strings can be formatted using the `formatMessageWithHeaders()` method)
* @returns Uint8Array Signature in byte format
*/
export const signFormattedMessage = (
key: AsymmetricKey,
formattedMessageBytes: Uint8Array
): Uint8Array => {
return key.sign(formattedMessageBytes);
};

/**
Expand All @@ -31,14 +53,14 @@ export const verifyMessageSignature = (
message: string,
signature: Uint8Array
): boolean => {
const messageWithHeader = Buffer.from(`Casper Message:\n${message}`);
const messageWithHeader = formatMessageWithHeaders(message);
if (key.isEd25519()) {
return nacl.sign_detached_verify(messageWithHeader, signature, key.value());
}
if (key.isSecp256K1()) {
return secp256k1.ecdsaVerify(
signature,
sha256(messageWithHeader),
sha256(Buffer.from(messageWithHeader)),
key.value()
);
}
Expand Down
Loading

0 comments on commit 7d33be4

Please sign in to comment.