Skip to content

Change in the encoding of public key for hex string to be compatible with webwallet sdk version #2

@ivancho98

Description

@ivancho98

After generating the keypair in the src/keypair.js , the keypair.getPublic('hex') encodes the public key using the method getPublic for lib/elliptic/ec/key.js

KeyPair.prototype.getPublic = function getPublic(compact, enc) {
  // compact is optional argument
  if (typeof compact === 'string') {
    enc = compact;
    compact = null;
  }

  if (!this.pub)
    this.pub = this.ec.g.mul(this.priv);

  if (!enc)
    return this.pub;

  return this.pub.encode(enc, compact);
};

But in order to be compatible with https://github.com/minkainc/webwallet-sdk-java version it should use the lib/elliptic/eddsa/key.js

KeyPair.prototype.getPublic = function getPublic(enc) {
  return utils.encode(this.pubBytes(), enc);
};

So my proposal is to change the implementation of the keypair.js to :

'use strict'

const elliptic = require('elliptic')
const ed25519 = new elliptic.ec('ed25519')
const eddsa = elliptic.eddsa;

const schemes = {
  ed25519: {
    generate: ({ compressed = false } = {}) => {
      let keypair = ed25519.genKeyPair()

      let eddsaWithed25519 = new eddsa('ed25519');

      let keyPairEddsa = eddsaWithed25519.keyFromSecret(keypair.getPrivate());

      return {
        scheme: 'ed25519',
        public: keyPairEddsa.getPublic('hex'),
        secret: keypair.getPrivate('hex')
      }
    }
  }
}

class KeyPair {
  static generate(options = {}) {
    let {scheme = 'ed25519'} = options
    if (!schemes[scheme]) throw new Error('invalid-scheme')

    return schemes[scheme].generate(options)
  }
}

module.exports = KeyPair

Certainly there are some other changes and adequations that need to be made in Address and Signature.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions