forked from soramitsu/iroha-ed25519.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
85 lines (85 loc) · 3.47 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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Module = require('./lib/ed25519.min.js');
exports.createKeyPair = function () {
var pubKeyPtr = Module._malloc(32);
var pubKey = new Uint8Array(Module.HEAPU8.buffer, pubKeyPtr, 32);
var privKeyPtr = Module._malloc(32);
var privKey = new Uint8Array(Module.HEAPU8.buffer, privKeyPtr, 32);
Module._ed25519_create_keypair(privKeyPtr, pubKeyPtr);
var result = {
privateKey: Buffer.from(privKey),
publicKey: Buffer.from(pubKey),
};
Module._free(pubKeyPtr);
Module._free(privKeyPtr);
return result;
};
exports.derivePublicKey = function (privateKey) {
if (!Buffer.isBuffer(privateKey)) {
throw new Error('Input arguments are not buffers!');
}
var privKeyPtr = Module._malloc(32);
var privKey = new Uint8Array(Module.HEAPU8.buffer, privKeyPtr, 32);
privKey.set(privateKey);
var pubKeyPtr = Module._malloc(32);
var pubKey = new Uint8Array(Module.HEAPU8.buffer, pubKeyPtr, 32);
Module._ed25519_derive_public_key(privKeyPtr, pubKeyPtr);
var result = Buffer.from(pubKey);
Module._free(pubKeyPtr);
Module._free(privKeyPtr);
return result;
};
exports.sign = function (message, publicKey, privateKey) {
if (!Buffer.isBuffer(message) || !Buffer.isBuffer(publicKey) || !Buffer.isBuffer(privateKey)) {
throw new Error('Input arguments are not buffers!');
}
var msgLen = message.length;
var msgPtr = Module._malloc(msgLen);
var msg = new Uint8Array(Module.HEAPU8.buffer, msgPtr, msgLen);
msg.set(message);
var pubKeyPtr = Module._malloc(32);
var pubKey = new Uint8Array(Module.HEAPU8.buffer, pubKeyPtr, 32);
pubKey.set(publicKey);
var privKeyPtr = Module._malloc(32);
var privKey = new Uint8Array(Module.HEAPU8.buffer, privKeyPtr, 32);
privKey.set(privateKey);
var sigPtr = Module._malloc(64);
var sig = new Uint8Array(Module.HEAPU8.buffer, sigPtr, 64);
/*
WARNING: 0 is passed to the _ed25519_sign as 4th argument due to an error in the EMSCRIPTEN.
In case of EMSCRIPTEN fix and correct build, testcase will fail and this 0 will be removed from here.
*/
Module._ed25519_sign(sigPtr, msgPtr, msgLen, 0, pubKeyPtr, privKeyPtr);
var result = Buffer.from(sig);
Module._free(msgPtr);
Module._free(pubKeyPtr);
Module._free(privKeyPtr);
Module._free(sigPtr);
return result;
};
exports.verify = function (signature, message, publicKey) {
if (!Buffer.isBuffer(signature) || !Buffer.isBuffer(message) || !Buffer.isBuffer(publicKey)) {
throw new Error('Input arguments are not buffers!');
}
var msgLen = message.length;
var msgPtr = Module._malloc(msgLen);
var msg = new Uint8Array(Module.HEAPU8.buffer, msgPtr, msgLen);
msg.set(message);
var pubKeyPtr = Module._malloc(32);
var pubKey = new Uint8Array(Module.HEAPU8.buffer, pubKeyPtr, 32);
pubKey.set(publicKey);
var sigPtr = Module._malloc(64);
var sig = new Uint8Array(Module.HEAPU8.buffer, sigPtr, 64);
sig.set(signature);
/*
WARNING: 0 is passed to the _ed25519_verify as 4th argument due to an error in the EMSCRIPTEN.
In case of EMSCRIPTEN fix and correct build, testcase will fail and this 0 will be removed from here.
*/
var result = Module._ed25519_verify(sigPtr, msgPtr, msgLen, 0, pubKeyPtr) === 1;
Module._free(msgPtr);
Module._free(pubKeyPtr);
Module._free(sigPtr);
return result;
};
//# sourceMappingURL=index.js.map