Skip to content

Commit

Permalink
Merge pull request #256 from MeshJS/v1.6.4
Browse files Browse the repository at this point in the history
  • Loading branch information
jinglescode authored Aug 11, 2024
2 parents 24427cb + 1b80f7d commit cfaf210
Show file tree
Hide file tree
Showing 20 changed files with 116 additions and 143 deletions.
2 changes: 1 addition & 1 deletion packages/mesh-common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@meshsdk/common",
"version": "1.6.3",
"version": "1.6.4",
"description": "",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/mesh-contract/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@meshsdk/contract",
"version": "1.6.3",
"version": "1.6.4",
"description": "",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/mesh-core-csl/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@meshsdk/core-csl",
"version": "1.6.3",
"version": "1.6.4",
"description": "",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
Expand Down
4 changes: 2 additions & 2 deletions packages/mesh-core-cst/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@meshsdk/core-cst",
"version": "1.6.3",
"version": "1.6.4",
"description": "",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
Expand Down Expand Up @@ -56,4 +56,4 @@
"blockchain",
"sdk"
]
}
}
21 changes: 1 addition & 20 deletions packages/mesh-core-cst/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,14 @@
import { Cardano, Serialization } from "@cardano-sdk/core";

import {
getCoseKeyFromPublicKey,
getPublicKeyFromCoseKey,
StricaBip32PrivateKey,
StricaBip32PublicKey,
StricaCoseSign1,
StricaPrivateKey,
StricaPublicKey,
} from "./stricahq";

export * from "./types";
export * from "./message-signing";
export * from "./resolvers";
export * from "./serializer";
export * from "./stricahq";
export * from "./utils";

export * as CardanoSDKUtil from "@cardano-sdk/util";
export * as Crypto from "@cardano-sdk/crypto";
export * as CardanoSDK from "@cardano-sdk/core";

export { Cardano, Serialization };

export {
StricaPrivateKey,
StricaPublicKey,
StricaBip32PrivateKey,
StricaBip32PublicKey,
StricaCoseSign1,
getPublicKeyFromCoseKey,
getCoseKeyFromPublicKey,
};
22 changes: 22 additions & 0 deletions packages/mesh-core-cst/src/message-signing/check-signature.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { DataSignature } from "@meshsdk/common";

import { CoseSign1, getPublicKeyFromCoseKey } from "./cose-sign1";

export const checkSignature = (
data: string,
{ key, signature }: DataSignature,
) => {
const builder = CoseSign1.fromCbor(signature);

if (builder.getPayload() === null) {
return false;
}

if (Buffer.from(data, "hex").compare(builder.getPayload()!) !== 0) {
return false;
}

return builder.verifySignature({
publicKeyBuffer: getPublicKeyFromCoseKey(key),
});
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Buffer } from "buffer";
import { PublicKey } from "@stricahq/bip32ed25519";
import { Decoder, Encoder } from "@stricahq/cbors";
import { blake2b } from "blakejs";

import { StricaDecoder, StricaEncoder, StricaPublicKey } from "../stricahq";

class CoseSign1 {
private protectedMap: Map<any, any>;

Expand Down Expand Up @@ -30,7 +30,7 @@ class CoseSign1 {
}

static fromCbor(cbor: string) {
const decoded = Decoder.decode(Buffer.from(cbor, "hex"));
const decoded = StricaDecoder.decode(Buffer.from(cbor, "hex"));

if (!(decoded.value instanceof Array)) throw Error("Invalid CBOR");
if (decoded.value.length !== 4) throw Error("Invalid COSE_SIGN1");
Expand All @@ -39,7 +39,7 @@ class CoseSign1 {
// Decode and Set ProtectedMap
const protectedSerialized = decoded.value[0];
try {
protectedMap = Decoder.decode(protectedSerialized).value;
protectedMap = StricaDecoder.decode(protectedSerialized).value;
if (!(protectedMap instanceof Map)) {
throw Error();
}
Expand Down Expand Up @@ -69,7 +69,7 @@ class CoseSign1 {
let protectedSerialized = Buffer.alloc(0);

if (this.protectedMap.size !== 0) {
protectedSerialized = Encoder.encode(this.protectedMap);
protectedSerialized = StricaEncoder.encode(this.protectedMap);
}

const structure = [
Expand All @@ -79,15 +79,15 @@ class CoseSign1 {
this.payload,
];

return Encoder.encode(structure);
return StricaEncoder.encode(structure);
}

buildMessage(signature: Buffer): Buffer {
this.signature = signature;

let protectedSerialized = Buffer.alloc(0);
if (this.protectedMap.size !== 0) {
protectedSerialized = Encoder.encode(this.protectedMap);
protectedSerialized = StricaEncoder.encode(this.protectedMap);
}

const coseSign1 = [
Expand All @@ -97,7 +97,7 @@ class CoseSign1 {
this.signature,
];

return Encoder.encode(coseSign1);
return StricaEncoder.encode(coseSign1);
}

verifySignature({
Expand All @@ -114,7 +114,7 @@ class CoseSign1 {
if (!publicKeyBuffer) throw Error("Public key not found");
if (!this.signature) throw Error("Signature not found");

const publicKey = new PublicKey(publicKeyBuffer);
const publicKey = new StricaPublicKey(publicKeyBuffer);

return publicKey.verify(
this.signature,
Expand Down Expand Up @@ -155,7 +155,7 @@ class CoseSign1 {
}

const getPublicKeyFromCoseKey = (cbor: string): Buffer => {
const decodedCoseKey = Decoder.decode(Buffer.from(cbor, "hex"));
const decodedCoseKey = StricaDecoder.decode(Buffer.from(cbor, "hex"));
const publicKeyBuffer = decodedCoseKey.value.get(-2);

if (publicKeyBuffer) {
Expand All @@ -171,7 +171,7 @@ const getCoseKeyFromPublicKey = (cbor: string): Buffer => {
coseKeyMap.set(3, -8);
coseKeyMap.set(6, -2);
coseKeyMap.set(-2, Buffer.from(cbor, "hex"));
return Encoder.encode(coseKeyMap);
return StricaEncoder.encode(coseKeyMap);
};

export { CoseSign1, getPublicKeyFromCoseKey, getCoseKeyFromPublicKey };
14 changes: 14 additions & 0 deletions packages/mesh-core-cst/src/message-signing/generate-nonce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { customAlphabet } from "nanoid";

import { stringToHex } from "@meshsdk/common";

export const generateNonce = (label = "", length = 32) => {
if (length <= 0 || length > 2048) {
throw new Error("Length must be bewteen 1 and 2048");
}
const randomString = customAlphabet(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
);
const payload = randomString(length);
return stringToHex(`${label}${payload}`);
};
75 changes: 4 additions & 71 deletions packages/mesh-core-cst/src/message-signing/index.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,4 @@
import { customAlphabet } from "nanoid";

import { DataSignature, stringToHex } from "@meshsdk/common";

import {
getCoseKeyFromPublicKey,
getPublicKeyFromCoseKey,
Signer,
StricaCoseSign1,
} from "../";

export const signData = (data: string, signer: Signer): DataSignature => {
const payload = Buffer.from(data, "hex");
const publicKey = signer.key.toPublicKey().toBytes();

const protectedMap = new Map();
// Set protected headers as per CIP08
// Set Algorthm used by Cardano keys
protectedMap.set(1, -8);
// Set PublicKey
protectedMap.set(4, publicKey);
// Set Address
protectedMap.set("address", Buffer.from(signer.address.toBytes(), "hex"));

const coseSign1Builder = new StricaCoseSign1({
protectedMap,
unProtectedMap: new Map(),
payload: payload,
});

const signature = signer.key.sign(coseSign1Builder.createSigStructure());

const coseSignature = coseSign1Builder
.buildMessage(signature)
.toString("hex");

return {
key: getCoseKeyFromPublicKey(publicKey.toString("hex")).toString("hex"),
signature: coseSignature,
};
};

export const checkSignature = (
data: string,
{ key, signature }: DataSignature,
) => {
const builder = StricaCoseSign1.fromCbor(signature);

if (builder.getPayload() === null) {
return false;
}

if (Buffer.from(data, "hex").compare(builder.getPayload()!) !== 0) {
return false;
}

return builder.verifySignature({
publicKeyBuffer: getPublicKeyFromCoseKey(key),
});
};

export const generateNonce = (label = "", length = 32) => {
if (length <= 0 || length > 2048) {
throw new Error("Length must be bewteen 1 and 2048");
}
const randomString = customAlphabet(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
);
const payload = randomString(length);
return stringToHex(`${label}${payload}`);
};
export * from "./check-signature";
export * from "./cose-sign1";
export * from "./generate-nonce";
export * from "./sign-data";
35 changes: 35 additions & 0 deletions packages/mesh-core-cst/src/message-signing/sign-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { DataSignature } from "@meshsdk/common";

import { Signer } from "../types";
import { CoseSign1, getCoseKeyFromPublicKey } from "./cose-sign1";

export const signData = (data: string, signer: Signer): DataSignature => {
const payload = Buffer.from(data, "hex");
const publicKey = signer.key.toPublicKey().toBytes();

const protectedMap = new Map();
// Set protected headers as per CIP08
// Set Algorthm used by Cardano keys
protectedMap.set(1, -8);
// Set PublicKey
protectedMap.set(4, publicKey);
// Set Address
protectedMap.set("address", Buffer.from(signer.address.toBytes(), "hex"));

const coseSign1Builder = new CoseSign1({
protectedMap,
unProtectedMap: new Map(),
payload: payload,
});

const signature = signer.key.sign(coseSign1Builder.createSigStructure());

const coseSignature = coseSign1Builder
.buildMessage(signature)
.toString("hex");

return {
key: getCoseKeyFromPublicKey(publicKey.toString("hex")).toString("hex"),
signature: coseSignature,
};
};
12 changes: 12 additions & 0 deletions packages/mesh-core-cst/src/stricahq/bip32ed25519/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import strica from "@stricahq/bip32ed25519";

import { PrivateKey } from "./privateKey";

const { PublicKey, Bip32PrivateKey, Bip32PublicKey } = strica;

export {
PrivateKey as StricaPrivateKey,
PublicKey as StricaPublicKey,
Bip32PrivateKey as StricaBip32PrivateKey,
Bip32PublicKey as StricaBip32PublicKey,
};
5 changes: 5 additions & 0 deletions packages/mesh-core-cst/src/stricahq/cbors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import strica from "@stricahq/cbors";

const { Encoder, Decoder } = strica;

export { Encoder as StricaEncoder, Decoder as StricaDecoder };
22 changes: 2 additions & 20 deletions packages/mesh-core-cst/src/stricahq/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,2 @@
import strica from "@stricahq/bip32ed25519";

import {
CoseSign1,
getCoseKeyFromPublicKey,
getPublicKeyFromCoseKey,
} from "./coseSign1";
import { PrivateKey } from "./privateKey";

const { PublicKey, Bip32PrivateKey, Bip32PublicKey } = strica;

export {
PrivateKey as StricaPrivateKey,
PublicKey as StricaPublicKey,
Bip32PrivateKey as StricaBip32PrivateKey,
Bip32PublicKey as StricaBip32PublicKey,
CoseSign1 as StricaCoseSign1,
getPublicKeyFromCoseKey,
getCoseKeyFromPublicKey,
};
export * from "./bip32ed25519";
export * from "./cbors";
11 changes: 0 additions & 11 deletions packages/mesh-core-cst/test/message-signing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,4 @@ describe("MessageSigning", () => {
const result = checkSignature(config.nonce, config.signature);
expect(result).toBe(true);
});
it("checkSignature2", () => {
const data =
"stake_test1up64x8a7re5tz856zrdmch0c38k74y3jt2zmwk9mh7rntkgs6zxjp";
const dataSignature = {
signature:
"84582aa201276761646472657373581de075531fbe1e68b11e9a10dbbc5df889edea92325a85b758bbbf8735d9a166686173686564f4583a5369676e20746f206c6f67696e20696e20746f204d6573683a20564569743130766d554f3645794539364e4f48634c4b5239576253435856695158404e77094ec3320fb253ca0f4844206e58a1e96ead1a00adc2c15c9e2364c5199061422b31e7bbf580c737f029fdcf93d4b7c7a6c221a9dab93e34114c3d15fc06",
key: "a40101032720062158201220e6aa326f24f12d644a1011dad9d138965c84566d2b7e20b79db7cf2aa73f",
};
const result = checkSignature(data, dataSignature);
expect(result).toBe(true);
});
});
2 changes: 1 addition & 1 deletion packages/mesh-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@meshsdk/core",
"version": "1.6.3",
"version": "1.6.4",
"description": "",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/mesh-provider/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@meshsdk/provider",
"version": "1.6.3",
"version": "1.6.4",
"description": "",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/mesh-react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@meshsdk/react",
"version": "1.6.3",
"version": "1.6.4",
"description": "",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
Expand Down
Loading

0 comments on commit cfaf210

Please sign in to comment.