UNPKG

wehelpjs

Version:

wehelpjs is the JavaScript API Library for the WeYouMe blockchain

141 lines (113 loc) 4.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.decode = decode; exports.encode = encode; var _bytebuffer = _interopRequireDefault(require("bytebuffer")); var _assert = _interopRequireDefault(require("assert")); var _bs = _interopRequireDefault(require("bs58")); var _ecc = require("./ecc"); var _serializer = require("./serializer"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const encMemo = _serializer.ops.encrypted_memo; /** Some fields are only required if the memo is marked for decryption (starts with a hash). @arg {string|PrivateKey} private_key - WIF or PrivateKey object @arg {string} memo - plain text is returned, hash prefix base58 is decrypted @return {string} - utf8 decoded string (hash prefix) */ function decode(private_key, memo) { (0, _assert.default)(memo, 'memo is required'); _assert.default.equal(typeof memo, 'string', 'memo'); if (!/^#/.test(memo)) return memo; memo = memo.substring(1); (0, _assert.default)(private_key, 'private_key is required'); checkEncryption(); private_key = toPrivateObj(private_key); memo = _bs.default.decode(memo); memo = encMemo.fromBuffer(new Buffer(memo, 'binary')); const { from, to, nonce, check, encrypted } = memo; const pubkey = private_key.toPublicKey().toString(); const otherpub = pubkey === from.toString() ? to.toString() : from.toString(); memo = _ecc.Aes.decrypt(private_key, otherpub, nonce, encrypted, check); // remove varint length prefix const mbuf = _bytebuffer.default.fromBinary(memo.toString('binary'), _bytebuffer.default.DEFAULT_CAPACITY, _bytebuffer.default.LITTLE_ENDIAN); try { mbuf.mark(); return '#' + mbuf.readVString(); } catch (e) { mbuf.reset(); // Sender did not length-prefix the memo memo = new Buffer(mbuf.toString('binary'), 'binary').toString('utf-8'); return '#' + memo; } } /** Some fields are only required if the memo is marked for encryption (starts with a hash). @arg {string|PrivateKey} private_key - WIF or PrivateKey object @arg {string|PublicKey} public_key - Recipient @arg {string} memo - plain text is returned, hash prefix text is encrypted @arg {string} [testNonce = undefined] - just for testing @return {string} - base64 decoded string (or plain text) */ function encode(private_key, public_key, memo, testNonce) { (0, _assert.default)(memo, 'memo is required'); _assert.default.equal(typeof memo, 'string', 'memo'); if (!/^#/.test(memo)) return memo; memo = memo.substring(1); (0, _assert.default)(private_key, 'private_key is required'); (0, _assert.default)(public_key, 'public_key is required'); checkEncryption(); private_key = toPrivateObj(private_key); public_key = toPublicObj(public_key); const mbuf = new _bytebuffer.default(_bytebuffer.default.DEFAULT_CAPACITY, _bytebuffer.default.LITTLE_ENDIAN); mbuf.writeVString(memo); memo = new Buffer(mbuf.copy(0, mbuf.offset).toBinary(), 'binary'); const { nonce, message, checksum } = _ecc.Aes.encrypt(private_key, public_key, memo, testNonce); memo = encMemo.fromObject({ from: private_key.toPublicKey(), to: public_key, nonce, check: checksum, encrypted: message }); // serialize memo = encMemo.toBuffer(memo); return '#' + _bs.default.encode(new Buffer(memo, 'binary')); } let encodeTest = undefined; /** Memo encryption has failed in the browser before. An Error will be thrown if a memo can't be encrypted and decrypted. */ function checkEncryption() { if (encodeTest === undefined) { let plaintext; encodeTest = true; // prevent infinate looping try { const wif = "5JdCeK8bPbtcCBWdqX85a2hFAMSSGWJmwTTNi2egWCTvcny9Wqs"; // const wif = '5JdeC9P7Pbd1uGdFVEsJ41EkEnADbbHGq6p1BwFxm6txNBsQnsw' const pubkey = "TWYM76FNC7oyuecCciUfDqfpLQwMcDsoJN8octqCaWdJy2k8pavPi4"; const cyphertext = encode(wif, pubkey, 'memo'); plaintext = decode(wif, cyphertext); } catch (e) { console.error(e); } finally { encodeTest = plaintext === 'memo'; } } if (encodeTest === false) throw new Error('This environment does not support encryption.'); } const toPrivateObj = o => o ? o.d ? o : _ecc.PrivateKey.fromWif(o) : o /*null or undefined*/ ; const toPublicObj = o => o ? o.Q ? o : _ecc.PublicKey.fromString(o) : o /*null or undefined*/ ;