-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
202 lines (164 loc) · 5.01 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const { TezosWalletUtil, TezosNodeWriter } = require("conseiljs");
const base58check = require("bs58check");
const { blake2b } = require("blakejs");
const libsodium = require("libsodium-wrappers");
const baseN = require("base-n");
const bigInt = require('big-integer');
const tezosNode = "https://tezos-dev.cryptonomic-infra.tech/";
const alphanetFaucetAccount = {
mnemonic: [
"fiction",
"legend",
"milk",
"various",
"fatigue",
"width",
"mad",
"suit",
"prepare",
"shrimp",
"attack",
"junk",
"story",
"radio",
"hammer"
],
secret: "8223e169a1a1ed5dc689c51a64d2e983ce071626",
amount: "28333240349",
pkh: "tz1YaB6TgSRg9YWNHKQYZFZs8XhBxm9akpvn",
password: "8bSEZmJXBO",
email: "[email protected]"
};
const generateKeystore = () =>
TezosWalletUtil.unlockFundraiserIdentity(
alphanetFaucetAccount.mnemonic.join(" "),
alphanetFaucetAccount.email,
alphanetFaucetAccount.password,
alphanetFaucetAccount.pkh
);
const signMessageNum = async num => {
const keystore = await generateKeystore();
const privateKeyBytes = base58check.decode(keystore.privateKey).slice(4); // Remove the preceeding "edsk"
const messagePrefixHex = "0500";
const prefixedMessage = Buffer.from(
messagePrefixHex + writeSignedInt(num),
"hex"
); // Buffer is empty if prefixed with 0x!?
const hashedMessage = Buffer.from(blake2b(prefixedMessage, null, 32)); // Replace with libsodium.crypto_generichash?
await libsodium.ready;
const signature = libsodium.crypto_sign_detached(
hashedMessage,
privateKeyBytes
);
// Prefix so after base58 encoding, the sig is prefixed with "edsig"
const prefixedSig = Buffer.concat([
Buffer.from("09f5cd8612", "hex"),
signature
]);
return base58check.encode(prefixedSig);
};
const signMessageStr = async message => {
const keystore = await generateKeystore();
const privateKeyBytes = base58check.decode(keystore.privateKey).slice(4); // Remove the preceeding "edsk"
const messagePrefixHex = "0501";
const messageHex = Buffer.from(message, "ascii").toString("hex");
const buf = Buffer.allocUnsafe(4);
buf.writeUInt32BE(message.length, 0);
const messageLengthHex = buf.toString("hex");
const prefixedMessage = Buffer.from(
messagePrefixHex + messageLengthHex + messageHex,
"hex"
); // Buffer is empty if prefixed with 0x!?
const hashedMessage = Buffer.from(blake2b(prefixedMessage, null, 32)); // Replace with libsodium.crypto_generichash?
await libsodium.ready;
const signature = libsodium.crypto_sign_detached(
hashedMessage,
privateKeyBytes
);
// Prefix so after base58 encoding, the sig is prefixed with "edsig"
const prefixedSig = Buffer.concat([
Buffer.from("09f5cd8612", "hex"),
signature
]);
return base58check.encode(prefixedSig);
};
const closeChannel = async (contractAddress, message, signature) => {
const keystore = await generateKeystore();
return TezosNodeWriter.sendContractInvocationOperation(
tezosNode,
keystore,
contractAddress,
10000,
100000,
"",
1000,
100000,
`(Pair "${message}" "${signature}")`
);
};
/**
* Utility functions copied from:
* https://github.com/Cryptonomic/ConseilJS/blob/master/src/chain/tezos/TezosMessageUtil.ts
*/
const base128 = baseN.create({
characters: [...Array(128).keys()].map(k => ("0" + k.toString(16)).slice(-2))
});
function writeInt(value) {
if (value < 0) {
throw new Error("Use writeSignedInt to encode negative numbers");
}
return Buffer.from(
Buffer.from(base128.encode(value), "hex")
.map((v, i) => {
return i === 0 ? v : v ^ 0x80;
})
.reverse()
).toString("hex");
}
/**
* Encodes a signed integer into hex.
* @param {number} value Number to be obfuscated.
*/
function writeSignedInt(value) {
if (value === 0) {
return "00";
}
const n = bigInt(value).abs();
const l = n.bitLength().toJSNumber();
let arr = [];
let v = n;
for (let i = 0; i < l; i += 7) {
let byte = bigInt.zero;
if (i === 0) {
byte = v.and(0x3f); // first byte makes room for sign flag
v = v.shiftRight(6);
} else {
byte = v.and(0x7f); // NOT base128 encoded
v = v.shiftRight(7);
}
if (value < 0 && i === 0) {
byte = byte.or(0x40);
} // set sign flag
if (i + 7 < l) {
byte = byte.or(0x80);
} // set next byte flag
arr.push(byte.toJSNumber());
}
if (l % 7 === 0) {
arr[arr.length - 1] = arr[arr.length - 1] | 0x80;
arr.push(1);
}
return arr.map(v => ("0" + v.toString(16)).slice(-2)).join("");
}
async function run(amount) {
console.log(`Public key: ${(await generateKeystore()).publicKey}`);
const signature = await signMessageNum(amount);
console.log(`Signature: ${signature}`);
// TODO Interact with the contract to execute the channel close
// const response = await closeChannel(
// "KT1Rgr9zMhPvHA1nJ4xPbQAMwHwBKiErVowh", // Address of deployed contract
// message,
// signature
// );
}
run(100000).catch(err => console.error(err));