-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #256 from MeshJS/v1.6.4
- Loading branch information
Showing
20 changed files
with
116 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
packages/mesh-core-cst/src/message-signing/check-signature.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
packages/mesh-core-cst/src/message-signing/generate-nonce.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.