-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathsign.ts
42 lines (40 loc) · 1.21 KB
/
sign.ts
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
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))
)
}