Konubinix' opinionated web of thoughts

Test Eddsa Signature With Deno and Noble Curves

Fleeting

test eddsa signature with deno and noble curves

import { ed25519 } from "https://esm.sh/@noble/curves@1.0.0/ed25519.js";

// Function to convert hex to Uint8Array
function hexToBytes(hex: string): Uint8Array {
  const bytes = new Uint8Array(hex.length / 2);
  for (let i = 0; i < bytes.length; i++) {
    bytes[i] = parseInt(hex.substr(i * 2, 2), 16);
  }
  return bytes;
}

// Function to verify the signature
async function verifySignature(publicKeyHex: string, messageHex: string, signatureHex: string): Promise<boolean> {
  try {
    // Convert the hex representations to bytes
    const publicKeyBytes = hexToBytes(publicKeyHex);
    const signatureBytes = hexToBytes(signatureHex);
    const messageBytes = hexToBytes(messageHex);

    // Verify the signature
    const isValid = await ed25519.verify(signatureBytes, messageBytes, publicKeyBytes);
    return isValid;
  } catch (error) {
    console.error("An error occurred:", error);
    return false;
  }
}

// Example usage
const publicKey = "13c55e00f1c8a0727804f6304d0d1b417a0ef48cd6613db2b3309b0451d1fd53";
const message = "something";
const signature = "2779de318f83627945a1d13ae8da45c49dfcbb50a37692aafc3df1f3a33324ed0b7e63318bae7d3f72f855e8020517e8f76610a254bc575d68e7ed4de9f01c0f";

// Convert message to its hex representation
const encoder = new TextEncoder();
const messageBytes = encoder.encode(message);
const messageHex = Array.from(messageBytes).map(byte => byte.toString(16).padStart(2, '0')).join('');

verifySignature(publicKey, messageHex, signature).then(isValid => {
  console.log(`Is the signature valid? ${isValid}`);
});
TMP="$(mktemp -d)"
trap "rm -rf '${TMP}'" 0

cat<<"EOF" > "${TMP}/index.ts"
import { ed25519 } from "https://esm.sh/@noble/curves@1.0.0/ed25519.js";

// Function to convert hex to Uint8Array
function hexToBytes(hex: string): Uint8Array {
  const bytes = new Uint8Array(hex.length / 2);
  for (let i = 0; i < bytes.length; i++) {
    bytes[i] = parseInt(hex.substr(i * 2, 2), 16);
  }
  return bytes;
}

// Function to verify the signature
async function verifySignature(publicKeyHex: string, messageHex: string, signatureHex: string): Promise<boolean> {
  try {
    // Convert the hex representations to bytes
    const publicKeyBytes = hexToBytes(publicKeyHex);
    const signatureBytes = hexToBytes(signatureHex);
    const messageBytes = hexToBytes(messageHex);

    // Verify the signature
    const isValid = await ed25519.verify(signatureBytes, messageBytes, publicKeyBytes);
    return isValid;
  } catch (error) {
    console.error("An error occurred:", error);
    return false;
  }
}

// Example usage
const publicKey = "13c55e00f1c8a0727804f6304d0d1b417a0ef48cd6613db2b3309b0451d1fd53";
const message = "something";
const signature = "2779de318f83627945a1d13ae8da45c49dfcbb50a37692aafc3df1f3a33324ed0b7e63318bae7d3f72f855e8020517e8f76610a254bc575d68e7ed4de9f01c0f";

// Convert message to its hex representation
const encoder = new TextEncoder();
const messageBytes = encoder.encode(message);
const messageHex = Array.from(messageBytes).map(byte => byte.toString(16).padStart(2, '0')).join('');

verifySignature(publicKey, messageHex, signature).then(isValid => {
  console.log(`Is the signature valid? ${isValid}`);
});
EOF
deno run "file://${TMP}/index.ts"
Is the signature valid? false