forked from bitcoinjs/bitcoinjs-message
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
157 lines (144 loc) · 4.05 KB
/
index.js
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const bs58check = require('bs58check')
const bech32 = require('bech32')
const bufferEquals = require('buffer-equals')
const createHash = require('create-hash')
const secp256k1 = require('secp256k1')
const varuint = require('varuint-bitcoin')
const SEGWIT_TYPES = {
P2WPKH: 'p2wpkh',
P2SH_P2WPKH: 'p2sh(p2wpkh)'
}
function sha256 (b) {
return createHash('sha256')
.update(b)
.digest()
}
function hash256 (buffer) {
return sha256(sha256(buffer))
}
function hash160 (buffer) {
return createHash('ripemd160')
.update(sha256(buffer))
.digest()
}
function encodeSignature (signature, recovery, compressed, segwitType) {
if (segwitType !== undefined) {
recovery += 8
if (segwitType === SEGWIT_TYPES.P2WPKH) recovery += 4
} else {
if (compressed) recovery += 4
}
return Buffer.concat([Buffer.alloc(1, recovery + 27), signature])
}
function decodeSignature (buffer) {
if (buffer.length !== 65) throw new Error('Invalid signature length')
const flagByte = buffer.readUInt8(0) - 27
if (flagByte > 15 || flagByte < 0) {
throw new Error('Invalid signature parameter')
}
return {
compressed: !!(flagByte & 12),
segwitType: !(flagByte & 8)
? null
: !(flagByte & 4)
? SEGWIT_TYPES.P2SH_P2WPKH
: SEGWIT_TYPES.P2WPKH,
recovery: flagByte & 3,
signature: buffer.slice(1)
}
}
function magicHash (message, messagePrefix) {
messagePrefix = messagePrefix || '\u0018Wagerr Signed Message:\n'
if (!Buffer.isBuffer(messagePrefix)) {
messagePrefix = Buffer.from(messagePrefix, 'utf8')
}
if (!Buffer.isBuffer(message)) {
message = Buffer.from(message, 'utf8')
}
const messageVISize = varuint.encodingLength(message.length)
const buffer = Buffer.allocUnsafe(
messagePrefix.length + messageVISize + message.length
)
messagePrefix.copy(buffer, 0)
varuint.encode(message.length, buffer, messagePrefix.length)
message.copy(buffer, messagePrefix.length + messageVISize)
return hash256(buffer)
}
function sign (
message,
privateKey,
compressed,
messagePrefix,
sigOptions
) {
if (typeof messagePrefix === 'object' && sigOptions === undefined) {
sigOptions = messagePrefix
messagePrefix = undefined
}
let { segwitType, extraEntropy } = sigOptions || {}
if (
segwitType &&
(typeof segwitType === 'string' || segwitType instanceof String)
) {
segwitType = segwitType.toLowerCase()
}
if (
segwitType &&
segwitType !== SEGWIT_TYPES.P2SH_P2WPKH &&
segwitType !== SEGWIT_TYPES.P2WPKH
) {
throw new Error(
'Unrecognized segwitType: use "' +
SEGWIT_TYPES.P2SH_P2WPKH +
'" or "' +
SEGWIT_TYPES.P2WPKH +
'"'
)
}
const hash = magicHash(message, messagePrefix)
const sigObj = secp256k1.sign(hash, privateKey, { data: extraEntropy })
return encodeSignature(
sigObj.signature,
sigObj.recovery,
compressed,
segwitType
)
}
function verify (message, address, signature, messagePrefix) {
if (!Buffer.isBuffer(signature)) signature = Buffer.from(signature, 'base64')
const parsed = decodeSignature(signature)
const hash = magicHash(message, messagePrefix)
const publicKey = secp256k1.recover(
hash,
parsed.signature,
parsed.recovery,
parsed.compressed
)
const publicKeyHash = hash160(publicKey)
let actual, expected
if (parsed.segwitType) {
if (parsed.segwitType === SEGWIT_TYPES.P2SH_P2WPKH) {
const redeemScript = Buffer.concat([
Buffer.from('0014', 'hex'),
publicKeyHash
])
const redeemScriptHash = hash160(redeemScript)
actual = redeemScriptHash
expected = bs58check.decode(address).slice(1)
} else if (parsed.segwitType === SEGWIT_TYPES.P2WPKH) {
const result = bech32.decode(address)
const data = bech32.fromWords(result.words.slice(1))
actual = publicKeyHash
expected = Buffer.from(data)
}
} else {
actual = publicKeyHash
expected = bs58check.decode(address).slice(1)
}
return bufferEquals(actual, expected)
}
module.exports = {
magicHash: magicHash,
sign: sign,
verify: verify
}