-
Notifications
You must be signed in to change notification settings - Fork 8
/
miner-id.js
71 lines (57 loc) Β· 2.07 KB
/
miner-id.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
require('dotenv').config()
const bsv = require('bsv')
const protocolName = 'ac1eed88'
const cbdVersion = '0.1'
function createCoinbaseDocument (height, minerId, prevMinerIdPrivKey, vcTx, optionalData) {
let prevMinerId = prevMinerIdPrivKey.toPublicKey().toString()
prevMinerId = prevMinerId || minerId
const minerIdSigPayload = Buffer.concat([
Buffer.from(prevMinerId, 'hex'),
Buffer.from(minerId, 'hex'),
Buffer.from(vcTx, 'hex')
])
const hash = bsv.crypto.Hash.sha256(minerIdSigPayload)
const prevMinerIdSig = bsv.crypto.ECDSA.sign(hash, prevMinerIdPrivKey).toString()
const doc = {
version: cbdVersion,
height: height,
prevMinerId: prevMinerId,
prevMinerIdSig: prevMinerIdSig,
minerId: minerId,
vctx: {
txId: vcTx,
vout: 0
}
}
if (optionalData) {
doc.minerContact = optionalData
}
return doc
}
function createMinerIdOpReturn (height, minerIdPrivKey, prevMinerIdPrivKey, vcTx, mc) {
const minerId = minerIdPrivKey.toPublicKey().toString()
const doc = createCoinbaseDocument(height, minerId, prevMinerIdPrivKey, vcTx, mc)
const payload = JSON.stringify(doc)
const hash = bsv.crypto.Hash.sha256(Buffer.from(payload))
const signature = bsv.crypto.ECDSA.sign(hash, minerIdPrivKey).toString()
const opReturnScript = bsv.Script.buildSafeDataOut([protocolName, payload, signature]).toBuffer()
return opReturnScript
}
exports.generate = function (height) {
try {
const v = process.env.VCTX
const mc = {
name: process.env.MINERID_NAME.toString(),
email: process.env.MINERID_EMAIL.toString(),
merchantAPIEndPoint: process.env.MINERID_MAPI.toString()
}
const prevMinerIdPrivKey = bsv.PrivateKey.fromWIF(process.env.PREV_MINERID_PK.toString())
const minerIdPrivKey = bsv.PrivateKey.fromWIF(process.env.MINERID_PK.toString())
const minerIdPayload = createMinerIdOpReturn(height, minerIdPrivKey, prevMinerIdPrivKey, v, mc)
return minerIdPayload
} catch (e) {
console.log(
'Incorrect miner-id parameters, please make sure .env file is setup correctly: ', e
)
}
}