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

Missing uncompressed public keys #27

Open
jafri opened this issue Jul 17, 2022 · 1 comment
Open

Missing uncompressed public keys #27

jafri opened this issue Jul 17, 2022 · 1 comment
Milestone

Comments

@jafri
Copy link

jafri commented Jul 17, 2022

With the new k1_recover intrinsics launching, I need a way to get uncompressed keys from @greymass/eosio

Seems recover and getPublic both compress the keys

Plus there is no uncompressed publickey type in eosio-core

@jafri
Copy link
Author

jafri commented Jul 18, 2022

For now, worked around using:

import { Checksum256, Checksum256Type, Signature } from '@greymass/eosio'
import { ec } from 'elliptic'

const curves: {[type: string]: ec} = {}

/**
 * Get curve for key type.
 * @internal
 */
export function getCurve(type: string): ec {
    let rv = curves[type]
    if (!rv) {
        if (type === 'K1') {
            rv = curves[type] = new ec('secp256k1')
        } else if (type === 'R1') {
            rv = curves[type] = new ec('p256')
        } else {
            throw new Error(`Unknown curve type: ${type}`)
        }
    }
    return rv
}


/**
 * Recover public key from signature and recovery id.
 * @internal
 */
 export function recoverUncompressed(signature: Uint8Array, message: Uint8Array, type: string) {
    const curve = getCurve(type)
    const recid = signature[0] - 31
    const r = signature.subarray(1, 33)
    const s = signature.subarray(33)
    const point = curve.recoverPubKey(message, {r, s}, recid)
    return new Uint8Array(point.encode())
}

export function recoverUncompressedDigest(signature: Signature, digest: Checksum256Type) {
    digest = Checksum256.from(digest)
    const uncompressed = recoverUncompressed(signature.data.array, digest.array, signature.type)
    return uncompressed
}

@aaroncox aaroncox added this to the Unscheduled milestone Aug 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants