All files / src/crypto sign.ts

100% Statements 13/13
91.66% Branches 11/12
100% Functions 2/2
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43                11x 11x       11x 9x 9x 14x 14x 14x     2x 2x 2x   11x                   14x              
import {ec} from 'elliptic'
import {getCurve} from './curves'
 
/**
 * Sign digest using private key.
 * @internal
 */
export function sign(secret: Uint8Array, message: Uint8Array, type: string) {
    const curve = getCurve(type)
    const key = curve.keyFromPrivate(secret)
    let sig: ec.Signature
    let r: Uint8Array
    let s: Uint8Array
    if (type === 'K1') {
        let attempt = 1
        do {
            sig = key.sign(message, {canonical: true, pers: [attempt++]})
            r = sig.r.toArrayLike(Uint8Array as any, 'be', 32)
            s = sig.s.toArrayLike(Uint8Array as any, 'be', 32)
        } while (!isCanonical(r, s))
    } else {
        sig = key.sign(message, {canonical: true})
        r = sig.r.toArrayLike(Uint8Array as any, 'be', 32)
        s = sig.s.toArrayLike(Uint8Array as any, 'be', 32)
    }
    return {type, r, s, recid: sig.recoveryParam || 0}
}
 
/**
 * Here be dragons
 * - https://github.com/steemit/steem/issues/1944
 * - https://github.com/EOSIO/eos/issues/6699
 * @internal
 */
function isCanonical(r: Uint8Array, s: Uint8Array) {
    return (
        !(r[0] & 0x80) &&
        !(r[0] === 0 && !(r[1] & 0x80)) &&
        !(s[0] & 0x80) &&
        !(s[0] === 0 && !(s[1] & 0x80))
    )
}