How to use the elliptic.eddsa function in elliptic

To help you get started, we’ve selected a few elliptic examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github anvilresearch / webcrypto / src / algorithms / EDDSA.js View on Github external
// Ensure key.handle is hex string, array or Buffer
    if (!Array.isArray(key.handle) && !Buffer.isBuffer(key.handle) && typeof key.handle !== 'string' ){
      throw DataError('Key handle must be an Array, Buffer or hex string')
    }

    // Ensure signature is ArrayBuffer or hex string
    if (!signature instanceof ArrayBuffer && typeof signature !== 'string'){
      throw DataError('Signature must be an ArrayBuffer or hex string')
    }

    try {
      // Normalize curve
      let curveName = EddsaCurves.find(alg => alg.namedCurve === key.algorithm.namedCurve)
      // Create curve via elliptic
      let ec = new elEdDSA(curveName.name)
      
      // Generate keypair from key
      let ecKey 
      if (key.type === 'public'){
        if (typeof key.handle === 'string'){
          ecKey = ec.keyFromPublic(key.handle, 'hex')
        } else { 
          ecKey = ec.keyFromPublic(key.handle.toString('hex'),'hex')
        }
      } else {
        throw new OperationError("Invalid key type")
      }

      // Convert ArrayBuffer back to hex
      if (signature instanceof ArrayBuffer){
        signature = Buffer.from(signature).toString('hex')
github SocialXNetwork / socialx_react_native / packages / webcrypto / src / algorithms / EDDSA.js View on Github external
}
    // "pkcs8" format
    else if (format === 'pkcs8'){
      throw new CurrentlyNotSupportedError(format,"jwk' or 'raw")
    }
    // "jwk" format
    else if (format === 'jwk'){
      // Create new jwk
      let jwk = new JsonWebKey({
        "kty": "OKP",
      })
      // Assign 'crv' to jwk
      jwk.crv = EddsaCurves.find(alg => alg.namedCurve === key.algorithm.namedCurve).namedCurve

      // If the key is Private then derive 'd' and 'x'
      let ec = new elEdDSA('ed25519')
      if (key.type === 'private'){
        let ecKey = ec.keyFromSecret(key.handle)
        jwk.d = base64url(key.handle)
        jwk.x = base64url(ecKey.pubBytes())
      } 
      // If the key is Public then derive 'x'
      else if (key.type === 'public'){
        let ecKey = ec.keyFromPublic(key.handle)
        jwk.x = base64url(ecKey.pubBytes())
      } 
      // Otherwise throw error
      else {
        throw new DataError ("Unknown key type.")
      }

      // Set "key_ops" field
github SocialXNetwork / socialx_react_native / packages / webcrypto / src / algorithms / EDDSA.js View on Github external
if (!namedCurve) {
      throw new DataError('namedCurve is a required parameter for EDDSA')
    }

    if (!EddsaCurves.map(alg => alg.namedCurve).includes(namedCurve)) {
      throw new DataError('namedCurve is not valid')
    }

    // Generate random key for scret portion
    let secretBytes = crypto.randomBytes(32)

    // Normalize curve
    let curveName = EddsaCurves.find(alg => alg.namedCurve === namedCurve)
    // Create curve via elliptic
    let ec = new elEdDSA(curveName.name)
    let ecKey = ec.keyFromSecret(secretBytes)
    let pubKey = Buffer.from(ecKey.pubBytes())

    // Set algorithm be a new EDDSA
    let algorithm = new EDDSA(params)

    // Create private key object
    let privateKey = new CryptoKey({
      type: 'private',
      algorithm,
      extractable,
      usages: ['sign'],
      handle: secretBytes
    })
    
    // Create public key object
github orbs-network / orbs-network-typescript / client / client-contract-test / src / contract-adapter.ts View on Github external
function testTransactionObjectSignature(sendTransactionObject: OrbsAPISendTransactionRequest) {
  const signature = Buffer.from(sendTransactionObject.signatureData.signatureHex, "hex");

  const ec = new eddsa("ed25519");
  const message = `
    {
      "header":{
        "contractAddressBase58":"${sendTransactionObject.header.contractAddressBase58}",
        "senderAddressBase58":"${sendTransactionObject.header.senderAddressBase58}",
        "timestamp":"${sendTransactionObject.header.timestamp}",
        "version":${sendTransactionObject.header.version}
      },
      "payload":${stringify(sendTransactionObject.payload)}
    }`.replace(/\s/g, "");

  const hasher = createHash("sha256");
  hasher.update(message);
  const hash = hasher.digest();
  const key = ec.keyFromPublic(sendTransactionObject.signatureData.publicKeyHex);
  expect(key.verify([...hash], [...signature])).to.be.true;
github bandprotocol / libra-web / packages / libra-web-core-utils / crypto / Eddsa.ts View on Github external
public static fromSecretKey(secretKey: Uint8Array): KeyPair {
    const eddsa = new Eddsa('ed25519')
    const eddsaPair = eddsa.keyFromSecret(Buffer.from(secretKey))
    return new KeyPair(eddsaPair)
  }
  private eddsaPair: Eddsa.KeyPair
github ontio / ontology-ts-sdk / src / crypto / PublicKey.ts View on Github external
verifyEdDSASignature(hash: string, signature: string): boolean {
        const r = signature.substr(0, 64);
        const s = signature.substr(64, 64);

        const eddsa = new elliptic.eddsa(this.parameters.curve.preset);
        return eddsa.verify(hash, { r, s }, this.key, 'hex');
    }
github ripple / rippled-historical-database / lib / validations / reports.js View on Github external
'use strict';

const smoment = require('../smoment');
const Promise = require('bluebird');
const Logger = require('../logger');
const log = new Logger({scope : 'validator reports'});
const colors = require('colors');
const hbase = require('../hbase');
const request = require('request-promise');
const addressCodec = require('ripple-address-codec');
const binary = require('ripple-binary-codec');
const elliptic = require('elliptic');
const Ed25519 = elliptic.eddsa('ed25519');

let clusters = {
  alt: {
    pubkey: 'ED264807102805220DA0F312E71FC2C69E1552C9C5790F6C25E3729DEB573D5860',
    site: 'https://vl.altnet.rippletest.net',
    validators: [],
    quorum: 0
  },
  main: {
    pubkey: 'ED2677ABFFD1B33AC6FBC3062B71F1E8397C1505E1C42C64D11AD1B28FF73F4734',
    site: 'https://vl.ripple.com',
    validators: [],
    quorum: 0
  }
};
github ontio / ontology-ts-sdk / src / crypto / PrivateKey.ts View on Github external
getEdDSAPublicKey(): PublicKey {
        const eddsa = new elliptic.eddsa(this.parameters.curve.preset);
        const keyPair = eddsa.keyFromSecret(this.key, 'hex');
        const pk = keyPair.getPublic(true, 'hex');

        return new PublicKey(pk, this.algorithm, this.parameters);
    }
github ripple / ripple-keypairs / src / ed25519.js View on Github external
'use strict';

const elliptic = require('elliptic');
const {utils: {parseBytes}} = elliptic;
const Ed25519 = elliptic.eddsa('ed25519');
const {KeyPair, KeyType} = require('./keypair');
const {Sha512, cached} = require('./utils');

/*
@param {Array} seed bytes
 */
function deriveEdKeyPairSecret(seed) {
  return new Sha512().add(seed).first256();
}

class Ed25519Pair extends KeyPair {
  constructor(options) {
    super(options);
    this.type = KeyType.ed25519;
  }
github ontio / ontology-ts-sdk / src / crypto / PrivateKey.ts View on Github external
computeEdDSASignature(hash: string): string {
        const eddsa = new elliptic.eddsa(this.parameters.curve.preset);
        const signed = eddsa.sign(hash, this.key);
        return signed.toHex().toLowerCase();
    }