How to use tweetnacl

Comprehensive tweetnacl code examples:

How to use tweetnacl.default:

78
79
80
81
82
83
84
85
86
87
const verifySignUserService = (publicKey, nonce, signature) => {
    const fullMessage = `APTOS\nnonce: ${nonce}\nmessage: ${(0, sha256_1.default)(publicKey)}`;
    const fullMessage1 = `APTOS\nmessage: ${(0, sha256_1.default)(publicKey)}\nnonce: ${nonce}`;
    //console.log("fullMessage: ", fullMessage);
    return (tweetnacl_1.default.sign.detached.verify(Buffer.from(fullMessage), Buffer.from(signature, "hex"), Buffer.from(publicKey, "hex")) ||
        tweetnacl_1.default.sign.detached.verify(Buffer.from(fullMessage1), Buffer.from(signature, "hex"), Buffer.from(publicKey, "hex")));
};
exports.verifySignUserService = verifySignUserService;
const getTraderByDayService = (address, fromDate, toDate, chainId) => __awaiter(void 0, void 0, void 0, function* () {
    const histories = yield (0, history_services_1.getHistoryTraderByDayService)(fromDate, toDate, {

How to use tweetnacl.randomBytes:

37
38
39
40
41
42
43
44
45
46
 * @returns {String} ciphertext - base64-encoded ciphertext
 * @returns {String} nonce - base64-encoded nonce
 */
var encryptAsymmetric = function (_a) {
    var plaintext = _a.plaintext, publicKey = _a.publicKey, privateKey = _a.privateKey;
    var nonce = nacl.randomBytes(24);
    var ciphertext = nacl.box(util.decodeUTF8(plaintext), nonce, util.decodeBase64(publicKey), util.decodeBase64(privateKey));
    return {
        ciphertext: util.encodeBase64(ciphertext),
        nonce: util.encodeBase64(nonce)

How to use tweetnacl.secretbox:

519
520
521
522
523
524
525
526
527
528
var salt = bcrypt.encodeBase64(Buffer.from(header.salt, 'hex'), 16);
var key = bcrypt.hashSync(password, "".concat(types.keystoreSaltPerfix).concat(salt));
key = _utils.Utils.sha256(Buffer.from(key).toString('hex'));
var keystoreData = Buffer.from(keystore_content.data, 'base64');
var nonce = keystoreData.slice(0, types.xchacha20NonceLength);
var privKey_full = nacl.secretbox.open(keystoreData.slice(types.xchacha20NonceLength), nonce, Buffer.from(key, 'hex'));

if (!privKey_full) {
  throw new _errors.SdkError('KeyStore parsing failed', _errors.CODES.Internal);
}

How to use tweetnacl.util:

How to use tweetnacl.sign:

310
311
312
313
314
315
316
317
318
319
320
	var S = pubKey._pub.multiply(this._priv);
	return (Buffer.from(S.getX().toBigInteger().toByteArray()));
};


function generateED25519() {
	var pair = nacl.sign.keyPair();
	var priv = Buffer.from(pair.secretKey);
	var pub = Buffer.from(pair.publicKey);
	assert.strictEqual(priv.length, 64);
	assert.strictEqual(pub.length, 32);

How to use tweetnacl.box:

315
316
317
318
319
320
321
322
323
324
325
326


function getKeyPair() {
    // return new Promise((resolve) => setTimeout(resolve, ms));
    const [privateSpendKey, privateViewKey] = js_wallet.getPrimaryAddressPrivateKeys()
    let secretKey = hexToUint(privateSpendKey)
    let keyPair = nacl.box.keyPair.fromSecretKey(secretKey)
    return keyPair
}


function getMsgKey() {