wharfkit / antelope

Core types, client interfaces, and other tools for working with Antelope-based blockchains.
Other
43 stars 23 forks source link

Missing uncompressed public keys #27

Open jafri opened 2 years ago

jafri commented 2 years ago

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 commented 2 years ago

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
}