From 7b7c03370589782833c557f7accc58b3377a4cd2 Mon Sep 17 00:00:00 2001 From: ety001 Date: Sun, 24 Apr 2022 15:07:32 +0000 Subject: [PATCH 1/2] fix deprecated buffer usage --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 95cdba7b..f8db70d6 100644 --- a/package.json +++ b/package.json @@ -32,13 +32,13 @@ }, "homepage": "https://github.com/steemit/steem-js#readme", "dependencies": { + "@exodus/bytebuffer": "git+https://github.com/ExodusMovement/bytebuffer.js.git#exodus", "@steemit/rpc-auth": "^1.1.1", "bigi": "^1.4.2", "bluebird": "^3.4.6", "browserify-aes": "^1.0.6", "bs58": "^4.0.0", "buffer": "^5.0.6", - "bytebuffer": "^5.0.1", "create-hash": "^1.1.2", "create-hmac": "^1.1.4", "cross-env": "^5.0.0", From 7e40d577068c0d19581d85086f66423d8778e188 Mon Sep 17 00:00:00 2001 From: ety001 Date: Mon, 25 Apr 2022 03:44:25 +0000 Subject: [PATCH 2/2] fix deprecated buffer usage --- package.json | 4 +- src/auth/ecc/package.json | 2 +- src/auth/ecc/src/address.js | 4 +- src/auth/ecc/src/aes.js | 8 ++-- src/auth/ecc/src/ecdsa.js | 32 ++++++------- src/auth/ecc/src/ecsignature.js | 6 +-- src/auth/ecc/src/key_private.js | 6 +-- src/auth/ecc/src/key_public.js | 10 ++--- src/auth/ecc/src/signature.js | 12 ++--- src/auth/index.js | 6 +-- src/auth/memo.js | 10 ++--- src/auth/serializer/package.json | 2 +- src/auth/serializer/src/convert.js | 12 ++--- src/auth/serializer/src/fast_parser.js | 2 +- src/auth/serializer/src/object_id.js | 18 ++++---- src/auth/serializer/src/serializer.js | 56 +++++++++++------------ src/auth/serializer/src/types.js | 24 +++++----- src/auth/serializer/src/validation.js | 62 +++++++++++++------------- src/broadcast/index.js | 2 +- src/index.js | 4 ++ test/Crypto.js | 32 ++++++------- test/operations_test.js | 2 +- test/types_test.js | 2 +- 23 files changed, 161 insertions(+), 157 deletions(-) diff --git a/package.json b/package.json index f8db70d6..dfbf5375 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "@exodus/bytebuffer": "git+https://github.com/ExodusMovement/bytebuffer.js.git#exodus", "@steemit/rpc-auth": "^1.1.1", "bigi": "^1.4.2", - "bluebird": "^3.4.6", + "bluebird": "^3.7.2", "browserify-aes": "^1.0.6", "bs58": "^4.0.0", "buffer": "^5.0.6", @@ -59,7 +59,7 @@ "babel-preset-es2015": "^6.16.0", "babel-preset-es2017": "^6.16.0", "babel-register": "^6.14.0", - "bluebird": "^3.4.6", + "bluebird": "^3.7.2", "eslint": "^3.5.0", "eslint-plugin-import": "^1.15.0", "eslint-plugin-jsx-a11y": "^2.2.2", diff --git a/src/auth/ecc/package.json b/src/auth/ecc/package.json index e0bf0094..9dee908c 100644 --- a/src/auth/ecc/package.json +++ b/src/auth/ecc/package.json @@ -12,9 +12,9 @@ "test:watch": "npm test -- --watch" }, "dependencies": { + "@exodus/bytebuffer": "git+https://github.com/ExodusMovement/bytebuffer.js.git#exodus", "bigi": "^1.4.1", "bs58": "^3.0.0", - "bytebuffer": "^5.0.0", "crypto-js": "^3.1.5", "ecurve": "^1.0.2", "secure-random": "^1.1.1" diff --git a/src/auth/ecc/src/address.js b/src/auth/ecc/src/address.js index 28b7365c..ae264679 100644 --- a/src/auth/ecc/src/address.js +++ b/src/auth/ecc/src/address.js @@ -20,7 +20,7 @@ class Address { const prefix = string.slice(0, address_prefix.length); assert.equal(address_prefix, prefix, `Expecting key to begin with ${address_prefix}, instead got ${prefix}`); let addy = string.slice(address_prefix.length); - addy = new Buffer(base58.decode(addy), 'binary'); + addy = new Buffer.from(base58.decode(addy), 'binary'); const checksum = addy.slice(-4); addy = addy.slice(0, -4); let new_checksum = hash.ripemd160(addy); @@ -33,7 +33,7 @@ class Address { static fromPublic(public_key, compressed = true, version = 56) { const sha2 = hash.sha256(public_key.toBuffer(compressed)); const rep = hash.ripemd160(sha2); - const versionBuffer = new Buffer(1); + const versionBuffer = new Buffer.alloc(1); versionBuffer.writeUInt8((0xFF & version), 0); const addr = Buffer.concat([versionBuffer, rep]); let check = hash.sha256(addr); diff --git a/src/auth/ecc/src/aes.js b/src/auth/ecc/src/aes.js index 20a0f21e..2e20d0a4 100644 --- a/src/auth/ecc/src/aes.js +++ b/src/auth/ecc/src/aes.js @@ -1,5 +1,5 @@ import secureRandom from 'secure-random'; -import ByteBuffer from 'bytebuffer'; +import ByteBuffer from '@exodus/bytebuffer'; import crypto from 'browserify-aes'; import assert from 'assert'; import PublicKey from './key_public'; @@ -58,7 +58,7 @@ function crypt(private_key, public_key, nonce, message, checksum) { if (!Buffer.isBuffer(message)) { if (typeof message !== 'string') throw new TypeError('message should be buffer or string') - message = new Buffer(message, 'binary') + message = new Buffer.from(message, 'binary') } if (checksum && typeof checksum !== 'number') throw new TypeError('checksum should be a number') @@ -67,7 +67,7 @@ function crypt(private_key, public_key, nonce, message, checksum) { let ebuf = new ByteBuffer(ByteBuffer.DEFAULT_CAPACITY, ByteBuffer.LITTLE_ENDIAN) ebuf.writeUint64(nonce) ebuf.append(S.toString('binary'), 'binary') - ebuf = new Buffer(ebuf.copy(0, ebuf.offset).toBinary(), 'binary') + ebuf = new Buffer.from(ebuf.copy(0, ebuf.offset).toBinary(), 'binary') const encryption_key = hash.sha512(ebuf) // D E B U G @@ -147,4 +147,4 @@ let unique_nonce_entropy = null const toPrivateObj = o => (o ? o.d ? o : PrivateKey.fromWif(o) : o/*null or undefined*/) const toPublicObj = o => (o ? o.Q ? o : PublicKey.fromString(o) : o/*null or undefined*/) const toLongObj = o => (o ? Long.isLong(o) ? o : Long.fromString(o) : o) -const toBinaryBuffer = o => (o ? Buffer.isBuffer(o) ? o : new Buffer(o, 'binary') : o) +const toBinaryBuffer = o => (o ? Buffer.isBuffer(o) ? o : new Buffer.from(o, 'binary') : o) diff --git a/src/auth/ecc/src/ecdsa.js b/src/auth/ecc/src/ecdsa.js index e09b134e..81fd6d1e 100644 --- a/src/auth/ecc/src/ecdsa.js +++ b/src/auth/ecc/src/ecdsa.js @@ -7,20 +7,20 @@ var ECSignature = require('./ecsignature') // https://tools.ietf.org/html/rfc6979#section-3.2 function deterministicGenerateK(curve, hash, d, checkSig, nonce) { - + enforceType('Buffer', hash) enforceType(BigInteger, d) - + if (nonce) { - hash = crypto.sha256(Buffer.concat([hash, new Buffer(nonce)])) + hash = crypto.sha256(Buffer.concat([hash, new Buffer.alloc(nonce)])) } // sanity check assert.equal(hash.length, 32, 'Hash must be 256 bit') var x = d.toBuffer(32) - var k = new Buffer(32) - var v = new Buffer(32) + var k = new Buffer.alloc(32) + var v = new Buffer.alloc(32) // Step B v.fill(1) @@ -29,13 +29,13 @@ function deterministicGenerateK(curve, hash, d, checkSig, nonce) { k.fill(0) // Step D - k = crypto.HmacSHA256(Buffer.concat([v, new Buffer([0]), x, hash]), k) + k = crypto.HmacSHA256(Buffer.concat([v, new Buffer.from([0]), x, hash]), k) // Step E v = crypto.HmacSHA256(v, k) // Step F - k = crypto.HmacSHA256(Buffer.concat([v, new Buffer([1]), x, hash]), k) + k = crypto.HmacSHA256(Buffer.concat([v, new Buffer.from([1]), x, hash]), k) // Step G v = crypto.HmacSHA256(v, k) @@ -48,13 +48,13 @@ function deterministicGenerateK(curve, hash, d, checkSig, nonce) { // Step H3, repeat until T is within the interval [1, n - 1] while ((T.signum() <= 0) || (T.compareTo(curve.n) >= 0) || !checkSig(T)) { - k = crypto.HmacSHA256(Buffer.concat([v, new Buffer([0])]), k) + k = crypto.HmacSHA256(Buffer.concat([v, new Buffer.from([0])]), k) v = crypto.HmacSHA256(v, k) // Step H1/H2a, again, ignored as tlen === qlen (256 bit) // Step H2b again v = crypto.HmacSHA256(v, k) - + T = BigInteger.fromBuffer(v) } @@ -63,24 +63,24 @@ function deterministicGenerateK(curve, hash, d, checkSig, nonce) { } function sign(curve, hash, d, nonce) { - + var e = BigInteger.fromBuffer(hash) var n = curve.n var G = curve.G - + var r, s var k = deterministicGenerateK(curve, hash, d, function (k) { // find canonically valid signature var Q = G.multiply(k) - + if (curve.isInfinity(Q)) return false - + r = Q.affineX.mod(n) if (r.signum() === 0) return false - + s = k.modInverse(n).multiply(e.add(d.multiply(r))).mod(n) if (s.signum() === 0) return false - + return true }, nonce) @@ -124,7 +124,7 @@ function verifyRaw(curve, e, signature, Q) { // 1.4.7 Set v = xR mod n var v = xR.mod(n) - + // 1.4.8 If v = r, output "valid", and if v != r, output "invalid" return v.equals(r) } diff --git a/src/auth/ecc/src/ecsignature.js b/src/auth/ecc/src/ecsignature.js index 48b73d5d..5627c0c8 100644 --- a/src/auth/ecc/src/ecsignature.js +++ b/src/auth/ecc/src/ecsignature.js @@ -87,7 +87,7 @@ ECSignature.prototype.toCompact = function(i, compressed) { if (compressed) i += 4 i += 27 - var buffer = new Buffer(65) + var buffer = new Buffer.alloc(65) buffer.writeUInt8(i, 0) this.r.toBuffer(32).copy(buffer, 1) @@ -113,11 +113,11 @@ ECSignature.prototype.toDER = function() { // SEQUENCE sequence.unshift(0x30, sequence.length) - return new Buffer(sequence) + return new Buffer.from(sequence) } ECSignature.prototype.toScriptSignature = function(hashType) { - var hashTypeBuffer = new Buffer(1) + var hashTypeBuffer = new Buffer.alloc(1) hashTypeBuffer.writeUInt8(hashType, 0) return Buffer.concat([this.toDER(), hashTypeBuffer]) diff --git a/src/auth/ecc/src/key_private.js b/src/auth/ecc/src/key_private.js index 200fe186..d29090cc 100644 --- a/src/auth/ecc/src/key_private.js +++ b/src/auth/ecc/src/key_private.js @@ -53,7 +53,7 @@ class PrivateKey { @return {string} Wallet Import Format (still a secret, Not encrypted) */ static fromWif(_private_wif) { - var private_wif = new Buffer(base58.decode(_private_wif)); + var private_wif = new Buffer.from(base58.decode(_private_wif)); var version = private_wif.readUInt8(0); assert.equal(0x80, version, `Expected version ${0x80}, instead got ${version}`); // checksum includes the version @@ -72,7 +72,7 @@ class PrivateKey { toWif() { var private_key = this.toBuffer(); // checksum includes the version - private_key = Buffer.concat([new Buffer([0x80]), private_key]); + private_key = Buffer.concat([new Buffer.from([0x80]), private_key]); var checksum = hash.sha256(private_key); checksum = hash.sha256(checksum); checksum = checksum.slice(0, 4); @@ -151,7 +151,7 @@ class PrivateKey { // } static fromHex(hex) { - return PrivateKey.fromBuffer(new Buffer(hex, 'hex')); + return PrivateKey.fromBuffer(new Buffer.from(hex, 'hex')); } toHex() { diff --git a/src/auth/ecc/src/key_public.js b/src/auth/ecc/src/key_public.js index 81791b08..cf4d2461 100644 --- a/src/auth/ecc/src/key_public.js +++ b/src/auth/ecc/src/key_public.js @@ -16,7 +16,7 @@ class PublicKey { constructor(Q) { this.Q = Q; } static fromBinary(bin) { - return PublicKey.fromBuffer(new Buffer(bin, 'binary')); + return PublicKey.fromBuffer(new Buffer.from(bin, 'binary')); } static fromBuffer(buffer) { @@ -98,7 +98,7 @@ class PublicKey { `Expecting key to begin with ${address_prefix}, instead got ${prefix}`); public_key = public_key.slice(address_prefix.length); - public_key = new Buffer(base58.decode(public_key), 'binary'); + public_key = new Buffer.from(base58.decode(public_key), 'binary'); var checksum = public_key.slice(-4); public_key = public_key.slice(0, -4); var new_checksum = hash.ripemd160(public_key); @@ -120,7 +120,7 @@ class PublicKey { var pub_buf = this.toBuffer(); var pub_sha = hash.sha256(pub_buf); var addy = hash.ripemd160(pub_sha); - addy = Buffer.concat([new Buffer([0x38]), addy]); //version 56(decimal) + addy = Buffer.concat([new Buffer.from([0x38]), addy]); //version 56(decimal) var checksum = hash.sha256(addy); checksum = hash.sha256(checksum); @@ -159,7 +159,7 @@ class PublicKey { // } static fromHex(hex) { - return PublicKey.fromBuffer(new Buffer(hex, 'hex')); + return PublicKey.fromBuffer(new Buffer.from(hex, 'hex')); } toHex() { @@ -167,7 +167,7 @@ class PublicKey { } static fromStringHex(hex) { - return PublicKey.fromString(new Buffer(hex, 'hex')); + return PublicKey.fromString(new Buffer.from(hex, 'hex')); } /* */ diff --git a/src/auth/ecc/src/signature.js b/src/auth/ecc/src/signature.js index abc7a2a2..5c241437 100644 --- a/src/auth/ecc/src/signature.js +++ b/src/auth/ecc/src/signature.js @@ -29,7 +29,7 @@ class Signature { toBuffer() { var buf; - buf = new Buffer(65); + buf = new Buffer.alloc(65); buf.writeUInt8(this.i, 0); this.r.toBuffer(32).copy(buf, 1); this.s.toBuffer(32).copy(buf, 33); @@ -63,7 +63,7 @@ class Signature { var _hash = hash.sha256(buf); return Signature.signBufferSha256(_hash, private_key) } - + /** Sign a buffer of exactally 32 bytes in size (sha256(text)) @param {Buffer} buf - 32 bytes binary @param {PrivateKey} private_key @@ -98,7 +98,7 @@ class Signature { }; static sign(string, private_key) { - return Signature.signBuffer(new Buffer(string), private_key); + return Signature.signBuffer(new Buffer.from(string), private_key); }; @@ -129,7 +129,7 @@ class Signature { // }; static fromHex(hex) { - return Signature.fromBuffer(new Buffer(hex, "hex")); + return Signature.fromBuffer(new Buffer.from(hex, "hex")); }; toHex() { @@ -138,13 +138,13 @@ class Signature { static signHex(hex, private_key) { var buf; - buf = new Buffer(hex, 'hex'); + buf = new Buffer.from(hex, 'hex'); return Signature.signBuffer(buf, private_key); }; verifyHex(hex, public_key) { var buf; - buf = new Buffer(hex, 'hex'); + buf = new Buffer.from(hex, 'hex'); return this.verifyBuffer(buf, public_key); }; diff --git a/src/auth/index.js b/src/auth/index.js index 76b0a3b7..875d6cf3 100644 --- a/src/auth/index.js +++ b/src/auth/index.js @@ -63,7 +63,7 @@ Auth.getPrivateKeys = function (name, password, roles = ['owner', 'active', 'pos Auth.isWif = function (privWif) { var isWif = false; try { - var bufWif = new Buffer(bs58.decode(privWif)); + var bufWif = new Buffer.from(bs58.decode(privWif)); var privKey = bufWif.slice(0, -4); var checksum = bufWif.slice(-4); var newChecksum = hash.sha256(privKey); @@ -80,7 +80,7 @@ Auth.toWif = function (name, password, role) { var seed = name + role + password; var brainKey = seed.trim().split(/[\t\n\v\f\r ]+/).join(' '); var hashSha256 = hash.sha256(brainKey); - var privKey = Buffer.concat([new Buffer([0x80]), hashSha256]); + var privKey = Buffer.concat([new Buffer.from([0x80]), hashSha256]); var checksum = hash.sha256(privKey); checksum = hash.sha256(checksum); checksum = checksum.slice(0, 4); @@ -108,7 +108,7 @@ Auth.signTransaction = function (trx, keys) { signatures = [].concat(trx.signatures); } - var cid = new Buffer(config.get('chain_id'), 'hex'); + var cid = new Buffer.from(config.get('chain_id'), 'hex'); var buf = transaction.toBuffer(trx); for (var key in keys) { diff --git a/src/auth/memo.js b/src/auth/memo.js index cf46a903..1bd56bbe 100644 --- a/src/auth/memo.js +++ b/src/auth/memo.js @@ -1,5 +1,5 @@ -import ByteBuffer from 'bytebuffer' +import ByteBuffer from '@exodus/bytebuffer' import assert from 'assert' import base58 from 'bs58' import {Aes, PrivateKey, PublicKey} from './ecc' @@ -25,7 +25,7 @@ export function decode(private_key, memo) { private_key = toPrivateObj(private_key) memo = base58.decode(memo) - memo = encMemo.fromBuffer(new Buffer(memo, 'binary')) + memo = encMemo.fromBuffer(new Buffer.from(memo, 'binary')) const {from, to, nonce, check, encrypted} = memo const pubkey = private_key.toPublicKey().toString() @@ -40,7 +40,7 @@ export function decode(private_key, memo) { } catch(e) { mbuf.reset() // Sender did not length-prefix the memo - memo = new Buffer(mbuf.toString('binary'), 'binary').toString('utf-8') + memo = new Buffer.from(mbuf.toString('binary'), 'binary').toString('utf-8') return '#' + memo } } @@ -68,7 +68,7 @@ export function encode(private_key, public_key, memo, testNonce) { const mbuf = new ByteBuffer(ByteBuffer.DEFAULT_CAPACITY, ByteBuffer.LITTLE_ENDIAN) mbuf.writeVString(memo) - memo = new Buffer(mbuf.copy(0, mbuf.offset).toBinary(), 'binary') + memo = new Buffer.from(mbuf.copy(0, mbuf.offset).toBinary(), 'binary') const {nonce, message, checksum} = Aes.encrypt(private_key, public_key, memo, testNonce) memo = encMemo.fromObject({ @@ -80,7 +80,7 @@ export function encode(private_key, public_key, memo, testNonce) { }) // serialize memo = encMemo.toBuffer(memo) - return '#' + base58.encode(new Buffer(memo, 'binary')) + return '#' + base58.encode(new Buffer.from(memo, 'binary')) } let encodeTest = undefined diff --git a/src/auth/serializer/package.json b/src/auth/serializer/package.json index 060ba7f2..874829d8 100644 --- a/src/auth/serializer/package.json +++ b/src/auth/serializer/package.json @@ -13,8 +13,8 @@ "author": "", "license": "BSD-2-Clause-FreeBSD", "dependencies": { + "@exodus/bytebuffer": "git+https://github.com/ExodusMovement/bytebuffer.js.git#exodus", "bigi": "^1.4.1", - "bytebuffer": "^5.0.0", "ecc": "^1.0.0" }, "devDependencies": { diff --git a/src/auth/serializer/src/convert.js b/src/auth/serializer/src/convert.js index 9eef9301..bfef08b5 100644 --- a/src/auth/serializer/src/convert.js +++ b/src/auth/serializer/src/convert.js @@ -1,4 +1,4 @@ -var ByteBuffer = require('bytebuffer'); +var ByteBuffer = require('@exodus/bytebuffer'); module.exports=function(type){ @@ -6,26 +6,26 @@ module.exports=function(type){ var b = ByteBuffer.fromHex(hex, ByteBuffer.LITTLE_ENDIAN); return type.fromByteBuffer(b); }, - + toHex(object) { var b=toByteBuffer(type, object); return b.toHex(); }, - + fromBuffer(buffer){ var b = ByteBuffer.fromBinary(buffer.toString(), ByteBuffer.LITTLE_ENDIAN); return type.fromByteBuffer(b); }, - + toBuffer(object){ return new Buffer(toByteBuffer(type, object).toBinary(), 'binary'); }, - + fromBinary(string){ var b = ByteBuffer.fromBinary(string, ByteBuffer.LITTLE_ENDIAN); return type.fromByteBuffer(b); }, - + toBinary(object) { return toByteBuffer(type, object).toBinary(); } diff --git a/src/auth/serializer/src/fast_parser.js b/src/auth/serializer/src/fast_parser.js index 35516393..b9b829fb 100644 --- a/src/auth/serializer/src/fast_parser.js +++ b/src/auth/serializer/src/fast_parser.js @@ -15,7 +15,7 @@ class FastParser { } else { let b_copy = b.copy(b.offset, b.offset + len); b.skip(len); - return new Buffer(b_copy.toBinary(), 'binary'); + return new Buffer.from(b_copy.toBinary(), 'binary'); } } diff --git a/src/auth/serializer/src/object_id.js b/src/auth/serializer/src/object_id.js index 16a33767..7ee3e488 100644 --- a/src/auth/serializer/src/object_id.js +++ b/src/auth/serializer/src/object_id.js @@ -1,10 +1,10 @@ -var Long = (require('bytebuffer')).Long; +var Long = (require('@exodus/bytebuffer')).Long; var v = require('./validation'); var DB_MAX_INSTANCE_ID = Long.fromNumber(((Math.pow(2,48))-1)); class ObjectId { - + constructor(space,type,instance){ this.space = space; this.type = type; @@ -15,10 +15,10 @@ class ObjectId { throw new `Invalid object id ${object_id}`(); } } - + static fromString(value){ if ( - value.space !== undefined && + value.space !== undefined && value.type !== undefined && value.instance !== undefined ) { @@ -35,28 +35,28 @@ class ObjectId { Long.fromString(params[3]) ); } - + static fromLong(long){ var space = long.shiftRight(56).toInt(); var type = long.shiftRight(48).toInt() & 0x00ff; var instance = long.and(DB_MAX_INSTANCE_ID); return new ObjectId(space, type, instance); } - + static fromByteBuffer(b){ return ObjectId.fromLong(b.readUint64()); } - + toLong() { return Long.fromNumber(this.space).shiftLeft(56).or( Long.fromNumber(this.type).shiftLeft(48).or(this.instance) ); } - + appendByteBuffer(b){ return b.writeUint64(this.toLong()); } - + toString() { return `${this.space}.${this.type}.${this.instance.toString()}`; } diff --git a/src/auth/serializer/src/serializer.js b/src/auth/serializer/src/serializer.js index 4e82b113..e6f54d68 100644 --- a/src/auth/serializer/src/serializer.js +++ b/src/auth/serializer/src/serializer.js @@ -1,19 +1,19 @@ -var ByteBuffer = require('bytebuffer'); +var ByteBuffer = require('@exodus/bytebuffer'); var EC = require('./error_with_cause'); const HEX_DUMP = process.env.npm_config__graphene_serializer_hex_dump class Serializer { - + constructor(operation_name, types) { this.operation_name = operation_name this.types = types if(this.types) this.keys = Object.keys(this.types) - + Serializer.printDebug = true } - + fromByteBuffer(b) { var object = {}; var field = null; @@ -34,7 +34,7 @@ class Serializer { //b.reset() var _b = b.copy(o1, o2); console.error( - `${this.operation_name}.${field}\t`, + `${this.operation_name}.${field}\t`, _b.toHex() ); } @@ -48,14 +48,14 @@ class Serializer { throw e; } } - + } catch (error) { EC.throw(this.operation_name+'.'+field, error); } - + return object; } - + appendByteBuffer(b, object) { var field = null; try { @@ -65,7 +65,7 @@ class Serializer { var type = this.types[field]; type.appendByteBuffer(b, object[field]); } - + } catch (error) { try { EC.throw(this.operation_name+'.'+field+" = "+ JSON.stringify(object[field]), error); @@ -75,7 +75,7 @@ class Serializer { } return; } - + fromObject(serialized_object){ var result = {}; var field = null; @@ -90,14 +90,14 @@ class Serializer { var object = type.fromObject(value); result[field] = object; } - + } catch (error) { EC.throw(this.operation_name+'.'+field, error); } - + return result; } - + /** @arg {boolean} [debug.use_default = false] - more template friendly @arg {boolean} [debug.annotate = false] - add user-friendly information @@ -108,7 +108,7 @@ class Serializer { try { if( ! this.types ) return result; - + var iterable = this.keys; for (var i = 0, field; i < iterable.length; i++) { field = iterable[i]; @@ -133,63 +133,63 @@ class Serializer { } catch (error) { EC.throw(this.operation_name+'.'+field, error); } - + return result; } - + /** Sort by the first element in a operation */ compare(a, b) { let first_key = this.keys[0] let first_type = this.types[first_key] - + let valA = a[first_key] let valB = b[first_key] if(first_type.compare) return first_type.compare(valA, valB) - + if(typeof valA === "number" && typeof valB === "number") return valA - valB - + let encoding if(Buffer.isBuffer(valA) && Buffer.isBuffer(valB)) { // A binary string compare does not work. If localeCompare is well supported that could replace HEX. Performanance is very good so comparing HEX works. encoding = "hex" } - + let strA = valA.toString(encoding) let strB = valB.toString(encoding) return strA > strB ? 1 : strA < strB ? -1 : 0 } - + // - + fromHex(hex) { var b = ByteBuffer.fromHex(hex, ByteBuffer.LITTLE_ENDIAN); return this.fromByteBuffer(b); } - + fromBuffer(buffer){ var b = ByteBuffer.fromBinary(buffer.toString("binary"), ByteBuffer.LITTLE_ENDIAN); return this.fromByteBuffer(b); } - + toHex(object) { // return this.toBuffer(object).toString("hex") var b=this.toByteBuffer(object); return b.toHex(); } - + toByteBuffer(object) { var b = new ByteBuffer(ByteBuffer.DEFAULT_CAPACITY, ByteBuffer.LITTLE_ENDIAN); this.appendByteBuffer(b, object); return b.copy(0, b.offset); } - + toBuffer(object){ - return new Buffer(this.toByteBuffer(object).toBinary(), 'binary'); + return new Buffer.from(this.toByteBuffer(object).toBinary(), 'binary'); } } -module.exports = Serializer \ No newline at end of file +module.exports = Serializer diff --git a/src/auth/serializer/src/types.js b/src/auth/serializer/src/types.js index 397b85d8..2b29e6eb 100644 --- a/src/auth/serializer/src/types.js +++ b/src/auth/serializer/src/types.js @@ -95,7 +95,7 @@ Types.asset = { { // NAI Case let b_copy = b.copy(b.offset - 1, b.offset + 3) - let nai = new Buffer(b_copy.toBinary(), "binary").readInt32() + let nai = new Buffer.from(b_copy.toBinary(), "binary").readInt32() nai = nai / 32 symbol = "@@" + nai.toString().padStart(8, '0') + damm_checksum_8digit(nai).to_String() precision = precision % 16 @@ -106,7 +106,7 @@ Types.asset = { { // Legacy Case let b_copy = b.copy(b.offset, b.offset + 7) - symbol = new Buffer(b_copy.toBinary(), "binary").toString().replace(/\x00/g, "") + symbol = new Buffer.from(b_copy.toBinary(), "binary").toString().replace(/\x00/g, "") b.skip(7) // "1.000 STEEM" always written with full precision amount_string = fromImpliedDecimal(amount, precision) @@ -208,7 +208,7 @@ Types.asset_symbol = { { // NAI Case let b_copy = b.copy(b.offset - 1, b.offset + 3) - let nai = new Buffer(b_copy.toBinary(), "binary").readInt32() + let nai = new Buffer.from(b_copy.toBinary(), "binary").readInt32() nai = nai / 32 nai_string = "@@" + nai.toString().padStart(8, '0') + damm_checksum_8digit(nai).to_String() precision = precision % 16 @@ -218,7 +218,7 @@ Types.asset_symbol = { { // Legacy Case let b_copy = b.copy(b.offset, b.offset + 7) - let symbol = new Buffer(b_copy.toBinary(), "binary").toString().replace(/\x00/g, "") + let symbol = new Buffer.from(b_copy.toBinary(), "binary").toString().replace(/\x00/g, "") if(symbol == "STEEM" || symbol == "TESTS") nai_string = "@@000000021" else if(symbol == "SBD" || symbol == "TBD") @@ -460,7 +460,7 @@ Types.uint128 = Types.string = {fromByteBuffer(b){ - return new Buffer(b.readVString(), 'utf8'); + return new Buffer.from(b.readVString(), 'utf8'); }, appendByteBuffer(b, object){ v.required(object); @@ -469,7 +469,7 @@ Types.string = }, fromObject(object){ v.required(object); - return new Buffer(object, 'utf8'); + return new Buffer.from(object, 'utf8'); }, toObject(object, debug = {}){ if (debug.use_default && object === undefined) { return ""; } @@ -482,7 +482,7 @@ Types.string_binary = var b_copy; var len = b.readVarint32(); b_copy = b.copy(b.offset, b.offset + len), b.skip(len); - return new Buffer(b_copy.toBinary(), 'binary'); + return new Buffer.from(b_copy.toBinary(), 'binary'); }, appendByteBuffer(b, object){ @@ -492,7 +492,7 @@ Types.string_binary = }, fromObject(object){ v.required(object); - return new Buffer(object); + return new Buffer.from(object); }, toObject(object, debug = {}){ if (debug.use_default && object === undefined) { return ""; } @@ -506,16 +506,16 @@ Types.bytes = function(size){ var b_copy; var len = b.readVarint32(); b_copy = b.copy(b.offset, b.offset + len), b.skip(len); - return new Buffer(b_copy.toBinary(), 'binary'); + return new Buffer.from(b_copy.toBinary(), 'binary'); } else { b_copy = b.copy(b.offset, b.offset + size), b.skip(size); - return new Buffer(b_copy.toBinary(), 'binary'); + return new Buffer.from(b_copy.toBinary(), 'binary'); } }, appendByteBuffer(b, object){ v.required(object); if(typeof object === "string") - object = new Buffer(object, "hex") + object = new Buffer.from(object, "hex") if (size === undefined) { b.writeVarint32(object.length); @@ -528,7 +528,7 @@ Types.bytes = function(size){ if( Buffer.isBuffer(object) ) return object - return new Buffer(object, 'hex'); + return new Buffer.from(object, 'hex'); }, toObject(object, debug = {}){ if (debug.use_default && object === undefined) { diff --git a/src/auth/serializer/src/validation.js b/src/auth/serializer/src/validation.js index e3ed064d..9865d88f 100644 --- a/src/auth/serializer/src/validation.js +++ b/src/auth/serializer/src/validation.js @@ -13,7 +13,7 @@ var get_protocol_instance; var get_protocol_type; var require_implementation_type; var get_implementation_instance; -var Long = require('bytebuffer').Long; +var Long = require('@exodus/bytebuffer').Long; // var BigInteger = require('bigi'); var chain_types = require('./ChainTypes'); @@ -22,7 +22,7 @@ var MAX_SAFE_INT = 9007199254740991; var MIN_SAFE_INT =-9007199254740991; /** - Most validations are skipped and the value returned unchanged when an empty string, null, or undefined is encountered (except "required"). + Most validations are skipped and the value returned unchanged when an empty string, null, or undefined is encountered (except "required"). Validations support a string format for dealing with large numbers. */ @@ -31,21 +31,21 @@ module.exports = _my = { is_empty: is_empty=function(value){ return value === null || value === undefined; }, - + required(value, field_name=""){ if (is_empty(value) ){ throw new Error(`value required ${field_name} ${value}`); } return value; }, - + require_long(value, field_name=""){ if (!Long.isLong(value)) { throw new Error(`Long value required ${field_name} ${value}`); } return value; }, - + string(value){ if (is_empty(value) ){ return value; } if (typeof value !== "string") { @@ -53,7 +53,7 @@ module.exports = _my = { } return value; }, - + number(value){ if (is_empty(value) ){ return value; } if (typeof value !== "number") { @@ -61,7 +61,7 @@ module.exports = _my = { } return value; }, - + whole_number(value, field_name=""){ if (is_empty(value) ){ return value; } if (/\./.test(value) ){ @@ -69,7 +69,7 @@ module.exports = _my = { } return value; }, - + unsigned(value, field_name=""){ if (is_empty(value) ){ return value; } if (/-/.test(value) ){ @@ -77,12 +77,12 @@ module.exports = _my = { } return value; }, - + is_digits: is_digits=function(value){ if (typeof value === "numeric") { return true; } return /^[0-9]+$/.test(value); }, - + to_number: to_number=function(value, field_name=""){ if (is_empty(value) ){ return value; } _my.no_overflow53(value, field_name); @@ -95,18 +95,18 @@ module.exports = _my = { })(); return int_value; }, - + to_long(value, field_name=""){ if (is_empty(value) ){ return value; } if (Long.isLong(value) ){ return value; } - + _my.no_overflow64(value, field_name); if (typeof value === "number") { value = ""+value; } return Long.fromString(value); }, - + to_string(value, field_name=""){ if (is_empty(value) ){ return value; } if (typeof value === "string") { return value; } @@ -119,7 +119,7 @@ module.exports = _my = { } throw `unsupported type ${field_name}: (${typeof value}) ${value}`; }, - + require_test(regex, value, field_name=""){ if (is_empty(value) ){ return value; } if (!regex.test(value)) { @@ -127,7 +127,7 @@ module.exports = _my = { } return value; }, - + require_match: require_match=function(regex, value, field_name=""){ if (is_empty(value) ){ return value; } var match = value.match(regex); @@ -136,7 +136,7 @@ module.exports = _my = { } return match; }, - + // require_object_id: require_object_id=function(value, field_name){ // return require_match( // /^([0-9]+)\.([0-9]+)\.([0-9]+)$/, @@ -144,7 +144,7 @@ module.exports = _my = { // field_name // ); // }, - + // Does not support over 53 bits require_range(min,max,value, field_name=""){ if (is_empty(value) ){ return value; } @@ -154,7 +154,7 @@ module.exports = _my = { } return value; }, - + require_object_type: require_object_type=function( reserved_spaces = 1, type, value, field_name="" @@ -170,53 +170,53 @@ module.exports = _my = { } return value; }, - + get_instance: get_instance=function(reserve_spaces, type, value, field_name){ if (is_empty(value) ){ return value; } require_object_type(reserve_spaces, type, value, field_name); return to_number(value.split('.')[2]); }, - + require_relative_type: require_relative_type=function(type, value, field_name){ require_object_type(0, type, value, field_name); return value; }, - + get_relative_instance: get_relative_instance=function(type, value, field_name){ if (is_empty(value) ){ return value; } require_object_type(0, type, value, field_name); return to_number(value.split('.')[2]); }, - + require_protocol_type: require_protocol_type=function(type, value, field_name){ require_object_type(1, type, value, field_name); return value; }, - + get_protocol_instance: get_protocol_instance=function(type, value, field_name){ if (is_empty(value) ){ return value; } require_object_type(1, type, value, field_name); return to_number(value.split('.')[2]); }, - + get_protocol_type: get_protocol_type=function(value, field_name){ if (is_empty(value) ){ return value; } require_object_id(value, field_name); var values = value.split('.'); return to_number(values[1]); }, - + get_protocol_type_name(value, field_name){ if (is_empty(value) ){ return value; } var type_id = get_protocol_type(value, field_name); return (Object.keys(chain_types.object_type))[type_id]; }, - + require_implementation_type: require_implementation_type=function(type, value, field_name){ require_object_type(2, type, value, field_name); return value; }, - + get_implementation_instance: get_implementation_instance=function(type, value, field_name){ if (is_empty(value) ){ return value; } require_object_type(2, type, value, field_name); @@ -245,18 +245,18 @@ module.exports = _my = { } throw `unsupported type ${field_name}: (${typeof value}) ${value}`; }, - + // signed / unsigned whole numbers only no_overflow64(value, field_name=""){ // https://github.com/dcodeIO/Long.js/issues/20 if (Long.isLong(value) ){ return; } - + // BigInteger#isBigInteger https://github.com/cryptocoinjs/bigi/issues/20 if (value.t !== undefined && value.s !== undefined) { _my.no_overflow64(value.toString(), field_name); return; } - + if (typeof value === "string") { // remove leading zeros, will cause a false positive value = value.replace(/^0+/,''); @@ -281,7 +281,7 @@ module.exports = _my = { } return; } - + throw `unsupported type ${field_name}: (${typeof value}) ${value}`; } }; diff --git a/src/broadcast/index.js b/src/broadcast/index.js index 3890ff2d..667bb46f 100644 --- a/src/broadcast/index.js +++ b/src/broadcast/index.js @@ -58,7 +58,7 @@ steemBroadcast._prepareTransaction = function steemBroadcast$_prepareTransaction const headBlockId = block ? block.previous : '0000000000000000000000000000000000000000'; return Object.assign({ ref_block_num: refBlockNum, - ref_block_prefix: new Buffer(headBlockId, 'hex').readUInt32LE(4), + ref_block_prefix: new Buffer.from(headBlockId, 'hex').readUInt32LE(4), expiration: new Date( chainDate.getTime() + 600 * 1000 diff --git a/src/index.js b/src/index.js index 6498412e..cffa62a0 100644 --- a/src/index.js +++ b/src/index.js @@ -15,3 +15,7 @@ module.exports = { config, utils, }; + +process.on('warning', (warning) => { + console.log('warning_stack: ', warning.stack); +}); diff --git a/test/Crypto.js b/test/Crypto.js index d35c6ee2..7bd194ec 100644 --- a/test/Crypto.js +++ b/test/Crypto.js @@ -12,14 +12,14 @@ describe("steem.auth: Crypto", function() { private_key = PrivateKey.fromHex decrypted_key.substring 0, 64 public_key = private_key.toPublicKey() console.log public_key.toHex());*/ - + it("sign", function() { this.timeout(10000); var private_key = PrivateKey.fromSeed("1"); return (() => { var result = []; for (var i = 0; i < 10; i++) { - result.push(Signature.signBuffer((new Buffer(i)), private_key)); + result.push(Signature.signBuffer((new Buffer.alloc(i)), private_key)); } return result; })(); @@ -28,60 +28,60 @@ describe("steem.auth: Crypto", function() { }) describe("steem.auth: derives", ()=> { - + let prefix = config.get("address_prefix") let one_time_private = PrivateKey.fromHex("8fdfdde486f696fd7c6313325e14d3ff0c34b6e2c390d1944cbfe150f4457168") let to_public = PublicKey.fromStringOrThrow(prefix + "7vbxtK1WaZqXsiCHPcjVFBewVj8HFRd5Z5XZDpN6Pvb2dZcMqK") let secret = one_time_private.get_shared_secret( to_public ) let child = hash.sha256( secret ) - + // Check everything above with `wdump((child));` from the witness_node: assert.equal(child.toString('hex'), "1f296fa48172d9af63ef3fb6da8e369e6cc33c1fb7c164207a3549b39e8ef698") - + let nonce = hash.sha256( one_time_private.toBuffer() ) assert.equal(nonce.toString('hex'), "462f6c19ece033b5a3dba09f1e1d7935a5302e4d1eac0a84489cdc8339233fbf") - + it("child from public", ()=> assert.equal( to_public.child(child).toString(), "STM6XA72XARQCain961PCJnXiKYdEMrndNGago2PV5bcUiVyzJ6iL", "derive child public key" )) - + // child = hash.sha256( one_time_private.get_secret( to_public )) it("child from private", ()=> assert.equal( PrivateKey.fromSeed("alice-brain-key").child(child).toPublicKey().toString(), "STM6XA72XARQCain961PCJnXiKYdEMrndNGago2PV5bcUiVyzJ6iL", "derive child from private key" )) - + // "many keys" works, not really needed // it("many keys", function() { - // + // // this.timeout(10 * 1000) - // + // // for (var i = 0; i < 10; i++) { // let privkey1 = key.get_random_key() // let privkey2 = key.get_random_key() - // + // // let secret1 = one_time_private.get_shared_secret( privkey1.toPublicKey() ) // let child1 = hash.sha256( secret1 ) - // + // // let secret2 = privkey2.get_shared_secret( privkey2.toPublicKey() ) // let child2 = hash.sha256( secret2 ) - // + // // it("child from public", ()=> assert.equal( // privkey1.toPublicKey().child(child1).toString(), // privkey2.toPublicKey().child(child2).toString(), // "derive child public key" // )) - // + // // it("child from private", ()=> assert.equal( // privkey1.child(child1).toString(), // privkey2.child(child2).toString(), // "derive child private key" // )) // } - // + // // }) }) @@ -93,7 +93,7 @@ var min_time_elapsed = function(f){ assert.equal( // repeat operations may take less time elapsed >= 250 * 0.8, true, - `minimum time requirement was not met, instead only ${elapsed/1000.0} elapsed` + `minimum time requirement was not met, instead only ${elapsed/1000.0} elapsed` ); return ret; }; diff --git a/test/operations_test.js b/test/operations_test.js index 3120e06c..a4be2247 100644 --- a/test/operations_test.js +++ b/test/operations_test.js @@ -19,7 +19,7 @@ describe("steem.auth: operation test", ()=> { let tx_hex = "614bde71d95f911bf3560109000000000000000003535445454d000009696e69746d696e65720573636f74740100000000010332757668fa45c2bc21447a2ff1dc2bbed9d9dda1616fd7b700255bd28e9d674a010001000000000103fb8900a262d51b908846be54fcf04b3a80d12ee749b9446f976b58b220ba4eed010001000000000102af4963d0f034043f4b4b0c99220e6a4b5d8b9cc71e5cd7d110f7602f3a0a11d1010002ff0de11ef55b998daf88047f1a00a60ed5dffb0c23c3279f8bd42a733845c5da000000" // 03 53 54 45 45 4d 0000 - assert.equal("STEEM", new Buffer("535445454d", "hex").toString()) + assert.equal("STEEM", new Buffer.from("535445454d", "hex").toString()) let tx_object1 = ops.signed_transaction.fromObject(tx) let tx_object2 = ops.signed_transaction.fromHex(tx_hex) assert.deepEqual(tx, ops.signed_transaction.toObject(tx_object1)) diff --git a/test/types_test.js b/test/types_test.js index 97d7c5f4..ab3dfb57 100644 --- a/test/types_test.js +++ b/test/types_test.js @@ -1,5 +1,5 @@ var Convert = require('../src/auth/serializer/src/convert'); -var Long = require('bytebuffer').Long; +var Long = require('@exodus/bytebuffer').Long; var assert = require('assert'); var type = require('../src/auth/serializer/src/types');