Skip to content

Commit bb32283

Browse files
authored
Merge pull request #492 from steemit/fix_deprecated_buffer_usage
fix deprecated buffer usage
2 parents 78ba61e + 7e40d57 commit bb32283

23 files changed

+162
-158
lines changed

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@
3232
},
3333
"homepage": "https://github.com/steemit/steem-js#readme",
3434
"dependencies": {
35+
"@exodus/bytebuffer": "git+https://github.com/ExodusMovement/bytebuffer.js.git#exodus",
3536
"@steemit/rpc-auth": "^1.1.1",
3637
"bigi": "^1.4.2",
37-
"bluebird": "^3.4.6",
38+
"bluebird": "^3.7.2",
3839
"browserify-aes": "^1.0.6",
3940
"bs58": "^4.0.0",
4041
"buffer": "^5.0.6",
41-
"bytebuffer": "^5.0.1",
4242
"create-hash": "^1.1.2",
4343
"create-hmac": "^1.1.4",
4444
"cross-env": "^5.0.0",
@@ -59,7 +59,7 @@
5959
"babel-preset-es2015": "^6.16.0",
6060
"babel-preset-es2017": "^6.16.0",
6161
"babel-register": "^6.14.0",
62-
"bluebird": "^3.4.6",
62+
"bluebird": "^3.7.2",
6363
"eslint": "^3.5.0",
6464
"eslint-plugin-import": "^1.15.0",
6565
"eslint-plugin-jsx-a11y": "^2.2.2",

src/auth/ecc/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
"test:watch": "npm test -- --watch"
1313
},
1414
"dependencies": {
15+
"@exodus/bytebuffer": "git+https://github.com/ExodusMovement/bytebuffer.js.git#exodus",
1516
"bigi": "^1.4.1",
1617
"bs58": "^3.0.0",
17-
"bytebuffer": "^5.0.0",
1818
"crypto-js": "^3.1.5",
1919
"ecurve": "^1.0.2",
2020
"secure-random": "^1.1.1"

src/auth/ecc/src/address.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Address {
2020
const prefix = string.slice(0, address_prefix.length);
2121
assert.equal(address_prefix, prefix, `Expecting key to begin with ${address_prefix}, instead got ${prefix}`);
2222
let addy = string.slice(address_prefix.length);
23-
addy = new Buffer(base58.decode(addy), 'binary');
23+
addy = new Buffer.from(base58.decode(addy), 'binary');
2424
const checksum = addy.slice(-4);
2525
addy = addy.slice(0, -4);
2626
let new_checksum = hash.ripemd160(addy);
@@ -33,7 +33,7 @@ class Address {
3333
static fromPublic(public_key, compressed = true, version = 56) {
3434
const sha2 = hash.sha256(public_key.toBuffer(compressed));
3535
const rep = hash.ripemd160(sha2);
36-
const versionBuffer = new Buffer(1);
36+
const versionBuffer = new Buffer.alloc(1);
3737
versionBuffer.writeUInt8((0xFF & version), 0);
3838
const addr = Buffer.concat([versionBuffer, rep]);
3939
let check = hash.sha256(addr);

src/auth/ecc/src/aes.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import secureRandom from 'secure-random';
2-
import ByteBuffer from 'bytebuffer';
2+
import ByteBuffer from '@exodus/bytebuffer';
33
import crypto from 'browserify-aes';
44
import assert from 'assert';
55
import PublicKey from './key_public';
@@ -58,7 +58,7 @@ function crypt(private_key, public_key, nonce, message, checksum) {
5858
if (!Buffer.isBuffer(message)) {
5959
if (typeof message !== 'string')
6060
throw new TypeError('message should be buffer or string')
61-
message = new Buffer(message, 'binary')
61+
message = new Buffer.from(message, 'binary')
6262
}
6363
if (checksum && typeof checksum !== 'number')
6464
throw new TypeError('checksum should be a number')
@@ -67,7 +67,7 @@ function crypt(private_key, public_key, nonce, message, checksum) {
6767
let ebuf = new ByteBuffer(ByteBuffer.DEFAULT_CAPACITY, ByteBuffer.LITTLE_ENDIAN)
6868
ebuf.writeUint64(nonce)
6969
ebuf.append(S.toString('binary'), 'binary')
70-
ebuf = new Buffer(ebuf.copy(0, ebuf.offset).toBinary(), 'binary')
70+
ebuf = new Buffer.from(ebuf.copy(0, ebuf.offset).toBinary(), 'binary')
7171
const encryption_key = hash.sha512(ebuf)
7272

7373
// D E B U G
@@ -147,4 +147,4 @@ let unique_nonce_entropy = null
147147
const toPrivateObj = o => (o ? o.d ? o : PrivateKey.fromWif(o) : o/*null or undefined*/)
148148
const toPublicObj = o => (o ? o.Q ? o : PublicKey.fromString(o) : o/*null or undefined*/)
149149
const toLongObj = o => (o ? Long.isLong(o) ? o : Long.fromString(o) : o)
150-
const toBinaryBuffer = o => (o ? Buffer.isBuffer(o) ? o : new Buffer(o, 'binary') : o)
150+
const toBinaryBuffer = o => (o ? Buffer.isBuffer(o) ? o : new Buffer.from(o, 'binary') : o)

src/auth/ecc/src/ecdsa.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ var ECSignature = require('./ecsignature')
77

88
// https://tools.ietf.org/html/rfc6979#section-3.2
99
function deterministicGenerateK(curve, hash, d, checkSig, nonce) {
10-
10+
1111
enforceType('Buffer', hash)
1212
enforceType(BigInteger, d)
13-
13+
1414
if (nonce) {
15-
hash = crypto.sha256(Buffer.concat([hash, new Buffer(nonce)]))
15+
hash = crypto.sha256(Buffer.concat([hash, new Buffer.alloc(nonce)]))
1616
}
1717

1818
// sanity check
1919
assert.equal(hash.length, 32, 'Hash must be 256 bit')
2020

2121
var x = d.toBuffer(32)
22-
var k = new Buffer(32)
23-
var v = new Buffer(32)
22+
var k = new Buffer.alloc(32)
23+
var v = new Buffer.alloc(32)
2424

2525
// Step B
2626
v.fill(1)
@@ -29,13 +29,13 @@ function deterministicGenerateK(curve, hash, d, checkSig, nonce) {
2929
k.fill(0)
3030

3131
// Step D
32-
k = crypto.HmacSHA256(Buffer.concat([v, new Buffer([0]), x, hash]), k)
32+
k = crypto.HmacSHA256(Buffer.concat([v, new Buffer.from([0]), x, hash]), k)
3333

3434
// Step E
3535
v = crypto.HmacSHA256(v, k)
3636

3737
// Step F
38-
k = crypto.HmacSHA256(Buffer.concat([v, new Buffer([1]), x, hash]), k)
38+
k = crypto.HmacSHA256(Buffer.concat([v, new Buffer.from([1]), x, hash]), k)
3939

4040
// Step G
4141
v = crypto.HmacSHA256(v, k)
@@ -48,13 +48,13 @@ function deterministicGenerateK(curve, hash, d, checkSig, nonce) {
4848

4949
// Step H3, repeat until T is within the interval [1, n - 1]
5050
while ((T.signum() <= 0) || (T.compareTo(curve.n) >= 0) || !checkSig(T)) {
51-
k = crypto.HmacSHA256(Buffer.concat([v, new Buffer([0])]), k)
51+
k = crypto.HmacSHA256(Buffer.concat([v, new Buffer.from([0])]), k)
5252
v = crypto.HmacSHA256(v, k)
5353

5454
// Step H1/H2a, again, ignored as tlen === qlen (256 bit)
5555
// Step H2b again
5656
v = crypto.HmacSHA256(v, k)
57-
57+
5858
T = BigInteger.fromBuffer(v)
5959
}
6060

@@ -63,24 +63,24 @@ function deterministicGenerateK(curve, hash, d, checkSig, nonce) {
6363
}
6464

6565
function sign(curve, hash, d, nonce) {
66-
66+
6767
var e = BigInteger.fromBuffer(hash)
6868
var n = curve.n
6969
var G = curve.G
70-
70+
7171
var r, s
7272
var k = deterministicGenerateK(curve, hash, d, function (k) {
7373
// find canonically valid signature
7474
var Q = G.multiply(k)
75-
75+
7676
if (curve.isInfinity(Q)) return false
77-
77+
7878
r = Q.affineX.mod(n)
7979
if (r.signum() === 0) return false
80-
80+
8181
s = k.modInverse(n).multiply(e.add(d.multiply(r))).mod(n)
8282
if (s.signum() === 0) return false
83-
83+
8484
return true
8585
}, nonce)
8686

@@ -124,7 +124,7 @@ function verifyRaw(curve, e, signature, Q) {
124124

125125
// 1.4.7 Set v = xR mod n
126126
var v = xR.mod(n)
127-
127+
128128
// 1.4.8 If v = r, output "valid", and if v != r, output "invalid"
129129
return v.equals(r)
130130
}

src/auth/ecc/src/ecsignature.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ ECSignature.prototype.toCompact = function(i, compressed) {
8787
if (compressed) i += 4
8888
i += 27
8989

90-
var buffer = new Buffer(65)
90+
var buffer = new Buffer.alloc(65)
9191
buffer.writeUInt8(i, 0)
9292

9393
this.r.toBuffer(32).copy(buffer, 1)
@@ -113,11 +113,11 @@ ECSignature.prototype.toDER = function() {
113113
// SEQUENCE
114114
sequence.unshift(0x30, sequence.length)
115115

116-
return new Buffer(sequence)
116+
return new Buffer.from(sequence)
117117
}
118118

119119
ECSignature.prototype.toScriptSignature = function(hashType) {
120-
var hashTypeBuffer = new Buffer(1)
120+
var hashTypeBuffer = new Buffer.alloc(1)
121121
hashTypeBuffer.writeUInt8(hashType, 0)
122122

123123
return Buffer.concat([this.toDER(), hashTypeBuffer])

src/auth/ecc/src/key_private.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class PrivateKey {
5353
@return {string} Wallet Import Format (still a secret, Not encrypted)
5454
*/
5555
static fromWif(_private_wif) {
56-
var private_wif = new Buffer(base58.decode(_private_wif));
56+
var private_wif = new Buffer.from(base58.decode(_private_wif));
5757
var version = private_wif.readUInt8(0);
5858
assert.equal(0x80, version, `Expected version ${0x80}, instead got ${version}`);
5959
// checksum includes the version
@@ -72,7 +72,7 @@ class PrivateKey {
7272
toWif() {
7373
var private_key = this.toBuffer();
7474
// checksum includes the version
75-
private_key = Buffer.concat([new Buffer([0x80]), private_key]);
75+
private_key = Buffer.concat([new Buffer.from([0x80]), private_key]);
7676
var checksum = hash.sha256(private_key);
7777
checksum = hash.sha256(checksum);
7878
checksum = checksum.slice(0, 4);
@@ -151,7 +151,7 @@ class PrivateKey {
151151
// }
152152

153153
static fromHex(hex) {
154-
return PrivateKey.fromBuffer(new Buffer(hex, 'hex'));
154+
return PrivateKey.fromBuffer(new Buffer.from(hex, 'hex'));
155155
}
156156

157157
toHex() {

src/auth/ecc/src/key_public.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class PublicKey {
1616
constructor(Q) { this.Q = Q; }
1717

1818
static fromBinary(bin) {
19-
return PublicKey.fromBuffer(new Buffer(bin, 'binary'));
19+
return PublicKey.fromBuffer(new Buffer.from(bin, 'binary'));
2020
}
2121

2222
static fromBuffer(buffer) {
@@ -98,7 +98,7 @@ class PublicKey {
9898
`Expecting key to begin with ${address_prefix}, instead got ${prefix}`);
9999
public_key = public_key.slice(address_prefix.length);
100100

101-
public_key = new Buffer(base58.decode(public_key), 'binary');
101+
public_key = new Buffer.from(base58.decode(public_key), 'binary');
102102
var checksum = public_key.slice(-4);
103103
public_key = public_key.slice(0, -4);
104104
var new_checksum = hash.ripemd160(public_key);
@@ -120,7 +120,7 @@ class PublicKey {
120120
var pub_buf = this.toBuffer();
121121
var pub_sha = hash.sha256(pub_buf);
122122
var addy = hash.ripemd160(pub_sha);
123-
addy = Buffer.concat([new Buffer([0x38]), addy]); //version 56(decimal)
123+
addy = Buffer.concat([new Buffer.from([0x38]), addy]); //version 56(decimal)
124124

125125
var checksum = hash.sha256(addy);
126126
checksum = hash.sha256(checksum);
@@ -159,15 +159,15 @@ class PublicKey {
159159
// }
160160

161161
static fromHex(hex) {
162-
return PublicKey.fromBuffer(new Buffer(hex, 'hex'));
162+
return PublicKey.fromBuffer(new Buffer.from(hex, 'hex'));
163163
}
164164

165165
toHex() {
166166
return this.toBuffer().toString('hex');
167167
}
168168

169169
static fromStringHex(hex) {
170-
return PublicKey.fromString(new Buffer(hex, 'hex'));
170+
return PublicKey.fromString(new Buffer.from(hex, 'hex'));
171171
}
172172

173173
/* </HEX> */

src/auth/ecc/src/signature.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Signature {
2929

3030
toBuffer() {
3131
var buf;
32-
buf = new Buffer(65);
32+
buf = new Buffer.alloc(65);
3333
buf.writeUInt8(this.i, 0);
3434
this.r.toBuffer(32).copy(buf, 1);
3535
this.s.toBuffer(32).copy(buf, 33);
@@ -63,7 +63,7 @@ class Signature {
6363
var _hash = hash.sha256(buf);
6464
return Signature.signBufferSha256(_hash, private_key)
6565
}
66-
66+
6767
/** Sign a buffer of exactally 32 bytes in size (sha256(text))
6868
@param {Buffer} buf - 32 bytes binary
6969
@param {PrivateKey} private_key
@@ -98,7 +98,7 @@ class Signature {
9898
};
9999

100100
static sign(string, private_key) {
101-
return Signature.signBuffer(new Buffer(string), private_key);
101+
return Signature.signBuffer(new Buffer.from(string), private_key);
102102
};
103103

104104

@@ -129,7 +129,7 @@ class Signature {
129129
// };
130130

131131
static fromHex(hex) {
132-
return Signature.fromBuffer(new Buffer(hex, "hex"));
132+
return Signature.fromBuffer(new Buffer.from(hex, "hex"));
133133
};
134134

135135
toHex() {
@@ -138,13 +138,13 @@ class Signature {
138138

139139
static signHex(hex, private_key) {
140140
var buf;
141-
buf = new Buffer(hex, 'hex');
141+
buf = new Buffer.from(hex, 'hex');
142142
return Signature.signBuffer(buf, private_key);
143143
};
144144

145145
verifyHex(hex, public_key) {
146146
var buf;
147-
buf = new Buffer(hex, 'hex');
147+
buf = new Buffer.from(hex, 'hex');
148148
return this.verifyBuffer(buf, public_key);
149149
};
150150

src/auth/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Auth.getPrivateKeys = function (name, password, roles = ['owner', 'active', 'pos
6363
Auth.isWif = function (privWif) {
6464
var isWif = false;
6565
try {
66-
var bufWif = new Buffer(bs58.decode(privWif));
66+
var bufWif = new Buffer.from(bs58.decode(privWif));
6767
var privKey = bufWif.slice(0, -4);
6868
var checksum = bufWif.slice(-4);
6969
var newChecksum = hash.sha256(privKey);
@@ -80,7 +80,7 @@ Auth.toWif = function (name, password, role) {
8080
var seed = name + role + password;
8181
var brainKey = seed.trim().split(/[\t\n\v\f\r ]+/).join(' ');
8282
var hashSha256 = hash.sha256(brainKey);
83-
var privKey = Buffer.concat([new Buffer([0x80]), hashSha256]);
83+
var privKey = Buffer.concat([new Buffer.from([0x80]), hashSha256]);
8484
var checksum = hash.sha256(privKey);
8585
checksum = hash.sha256(checksum);
8686
checksum = checksum.slice(0, 4);
@@ -108,7 +108,7 @@ Auth.signTransaction = function (trx, keys) {
108108
signatures = [].concat(trx.signatures);
109109
}
110110

111-
var cid = new Buffer(config.get('chain_id'), 'hex');
111+
var cid = new Buffer.from(config.get('chain_id'), 'hex');
112112
var buf = transaction.toBuffer(trx);
113113

114114
for (var key in keys) {

src/auth/memo.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
import ByteBuffer from 'bytebuffer'
2+
import ByteBuffer from '@exodus/bytebuffer'
33
import assert from 'assert'
44
import base58 from 'bs58'
55
import {Aes, PrivateKey, PublicKey} from './ecc'
@@ -25,7 +25,7 @@ export function decode(private_key, memo) {
2525
private_key = toPrivateObj(private_key)
2626

2727
memo = base58.decode(memo)
28-
memo = encMemo.fromBuffer(new Buffer(memo, 'binary'))
28+
memo = encMemo.fromBuffer(new Buffer.from(memo, 'binary'))
2929

3030
const {from, to, nonce, check, encrypted} = memo
3131
const pubkey = private_key.toPublicKey().toString()
@@ -40,7 +40,7 @@ export function decode(private_key, memo) {
4040
} catch(e) {
4141
mbuf.reset()
4242
// Sender did not length-prefix the memo
43-
memo = new Buffer(mbuf.toString('binary'), 'binary').toString('utf-8')
43+
memo = new Buffer.from(mbuf.toString('binary'), 'binary').toString('utf-8')
4444
return '#' + memo
4545
}
4646
}
@@ -68,7 +68,7 @@ export function encode(private_key, public_key, memo, testNonce) {
6868

6969
const mbuf = new ByteBuffer(ByteBuffer.DEFAULT_CAPACITY, ByteBuffer.LITTLE_ENDIAN)
7070
mbuf.writeVString(memo)
71-
memo = new Buffer(mbuf.copy(0, mbuf.offset).toBinary(), 'binary')
71+
memo = new Buffer.from(mbuf.copy(0, mbuf.offset).toBinary(), 'binary')
7272

7373
const {nonce, message, checksum} = Aes.encrypt(private_key, public_key, memo, testNonce)
7474
memo = encMemo.fromObject({
@@ -80,7 +80,7 @@ export function encode(private_key, public_key, memo, testNonce) {
8080
})
8181
// serialize
8282
memo = encMemo.toBuffer(memo)
83-
return '#' + base58.encode(new Buffer(memo, 'binary'))
83+
return '#' + base58.encode(new Buffer.from(memo, 'binary'))
8484
}
8585

8686
let encodeTest = undefined

0 commit comments

Comments
 (0)