forked from unstoppabledomains/dot-crypto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateSig.js
47 lines (36 loc) · 1.16 KB
/
createSig.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
const Web3 = require('web3')
const {isAbsolute, join} = require('path')
const {readFileSync, existsSync} = require('fs')
const config = require('./.cli-config.json')
const web3 = new Web3(config.url)
const pkPath = isAbsolute(config.privateKey)
? config.privateKey
: join(__dirname, config.privateKey)
if (existsSync(pkPath)) {
config.privateKey = readFileSync(pkPath, 'utf8')
}
if (!/^(?:0x)?[a-f\d]{64}$/.test(config.privateKey)) {
throw new Error('Bad private key')
}
const account = web3.eth.accounts.privateKeyToAccount(
config.privateKey.replace(/^(?:0x)?/, '0x'),
)
function createSig(nonce, method, ...args) {
const signatureControllerAbi = require('./abi/json/SignatureController.json')
const signature = account.sign(
Web3.utils.keccak256(
Web3.utils.keccak256(
web3.eth.abi
.encodeFunctionCall(
signatureControllerAbi.find(v => v.name === method),
args.concat('0x'),
)
.concat('0'.repeat(64)),
) + nonce.toString(16).padStart(64, '0'),
),
)
return signature.signature
}
if (require.main === module) {
process.stdout.write(createSig(...process.argv.slice(2)))
}