Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example JS program to verify Golem certificate signatures. #18

Merged
merged 1 commit into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/js/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
8 changes: 8 additions & 0 deletions examples/js/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
To use this example install dependencies via `npm install`.

Verifying a certificate signature (just the signature, not the certificate chain) can be accomplished by running the program:
`node . certificate-path` where certificate path is the path to a Golem certificate json.

Example:
`node . ../../tests/resources/certificate/happy_path.signed.json` - this should print that the signature is valid
`node . ../../tests/resources/certificate/invalid_signature.signed.json` - this should print that the verification failed
28 changes: 28 additions & 0 deletions examples/js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const fs = require('fs');
const canonicalize = require('canonicalize');
const elliptic = require('elliptic');

const filename = process.argv[process.argv.length - 1];
console.log("Reading certificate from file " + filename);

const certificate_data = fs.readFileSync(filename);
const certificate = JSON.parse(certificate_data);
const signing_certificate = certificate.signature.signer === "self" ? certificate : certificate.signature.signer;

if (certificate.signature.algorithm.hash !== "sha512"
|| certificate.signature.algorithm.encryption !== "EdDSA"
|| signing_certificate.certificate.publicKey.parameters.scheme !== "Ed25519") {
console.log("Unsupported signature type");
process.exit(1);
}

const encoder = new TextEncoder();
const signed_bytes = encoder.encode(canonicalize(certificate.certificate)); // encode the string into bytes with UTF-8 encoding

const result = elliptic.eddsa('ed25519').verify(signed_bytes, certificate.signature.value, signing_certificate.certificate.publicKey.key);

if (result) {
console.log("The signature is valid.");
} else {
console.log("Signature verification failed.");
}
83 changes: 83 additions & 0 deletions examples/js/package-lock.json

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

15 changes: 15 additions & 0 deletions examples/js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "golem-certificate",
"version": "0.1.0",
"description": "Example to verify golem-certificate signature",
"main": "index.js",
"bin": {
"golem-certificate": "index.js"
},
"author": "evik (https://github.com/evik42)",
"license": "GPLv3",
"dependencies": {
"canonicalize": "^2.0.0",
"elliptic": "^6.5.4"
}
}
4 changes: 2 additions & 2 deletions tests/resources/certificate/invalid_signature.signed.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"publicKey": {
"algorithm": "EdDSA",
"key": "c6cd286a2474d13ffc8dcd417a446df461751a78dec46d039603ca53a373ac52",
"key": "c6cd286a2474d13ffc8dcd417a446df461751a78dec46d039603ca53a373ac53",
"parameters": {
"scheme": "Ed25519"
}
Expand All @@ -30,7 +30,7 @@
"hash": "sha512",
"encryption": "EdDSA"
},
"value": "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
"value": "839a2d43d22690338f4fb282e7bcc790e7352dc99e42dc603d43e51e4e09f2c709f1b9f1f4c90b446107440f8ab13345fbbd1d64337acf70b2777be9a522be0b",
MrDarthShoe marked this conversation as resolved.
Show resolved Hide resolved
"signer": {
"$schema": "https://golem.network/schemas/v1/certificate.schema.json",
"certificate": {
Expand Down