Skip to content

Commit

Permalink
Merge pull request #492 from steemit/fix_deprecated_buffer_usage
Browse files Browse the repository at this point in the history
fix deprecated buffer usage
  • Loading branch information
yuekun0707 authored Sep 27, 2024
2 parents 78ba61e + 7e40d57 commit bb32283
Show file tree
Hide file tree
Showing 23 changed files with 162 additions and 158 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"bluebird": "^3.7.2",
"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",
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/auth/ecc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions src/auth/ecc/src/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/auth/ecc/src/aes.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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')
Expand All @@ -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
Expand Down Expand Up @@ -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)
32 changes: 16 additions & 16 deletions src/auth/ecc/src/ecdsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
}

Expand All @@ -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)

Expand Down Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions src/auth/ecc/src/ecsignature.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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])
Expand Down
6 changes: 3 additions & 3 deletions src/auth/ecc/src/key_private.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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() {
Expand Down
10 changes: 5 additions & 5 deletions src/auth/ecc/src/key_public.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -159,15 +159,15 @@ class PublicKey {
// }

static fromHex(hex) {
return PublicKey.fromBuffer(new Buffer(hex, 'hex'));
return PublicKey.fromBuffer(new Buffer.from(hex, 'hex'));
}

toHex() {
return this.toBuffer().toString('hex');
}

static fromStringHex(hex) {
return PublicKey.fromString(new Buffer(hex, 'hex'));
return PublicKey.fromString(new Buffer.from(hex, 'hex'));
}

/* </HEX> */
Expand Down
12 changes: 6 additions & 6 deletions src/auth/ecc/src/signature.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
};


Expand Down Expand Up @@ -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() {
Expand All @@ -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);
};

Expand Down
6 changes: 3 additions & 3 deletions src/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions src/auth/memo.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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()
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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({
Expand All @@ -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
Expand Down
Loading

0 comments on commit bb32283

Please sign in to comment.