How to use the allocUnsafe function from safe-buffer

Find comprehensive JavaScript safe-buffer.allocUnsafe code examples handpicked from public code repositorys.

88
89
90
91
92
93
94
95
96
97
if (r[0] & 0x80) throw new Error('R value is negative')
if (s[0] & 0x80) throw new Error('S value is negative')
if (lenR > 1 && (r[0] === 0x00) && !(r[1] & 0x80)) throw new Error('R value excessively padded')
if (lenS > 1 && (s[0] === 0x00) && !(s[1] & 0x80)) throw new Error('S value excessively padded')

var signature = Buffer.allocUnsafe(6 + lenR + lenS)

// 0x30 [total-length] 0x02 [R-length] [R] 0x02 [S-length] [S]
signature[0] = 0x30
signature[1] = signature.length - 2
fork icon42
star icon128
watch icon15

48
49
50
51
52
53
54
55
56
57
  return string
}

function decodeUnsafe (string) {
  if (typeof string !== 'string') throw new TypeError('Expected String')
  if (string.length === 0) return Buffer.allocUnsafe(0)

  var bytes = [0]
  for (var i = 0; i < string.length; i++) {
    var value = ALPHABET_MAP[string[i]]
fork icon79
star icon18
watch icon9

+ 9 other calls in file

33
34
35
36
37
38
39
40
41
42
Transaction.SIGHASH_SINGLE = 0x03
Transaction.SIGHASH_ANYONECANPAY = 0x80
Transaction.ADVANCED_TRANSACTION_MARKER = 0x00
Transaction.ADVANCED_TRANSACTION_FLAG = 0x01

var EMPTY_SCRIPT = Buffer.allocUnsafe(0)
var EMPTY_WITNESS = []
var ZERO = Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex')
var ONE = Buffer.from('0000000000000000000000000000000000000000000000000000000000000001', 'hex')
var VALUE_UINT64_MAX = Buffer.from('ffffffffffffffff', 'hex')
fork icon0
star icon7
watch icon3

+ 3 other calls in file

176
177
178
179
180
181
182
183
184
if (__isPrivate !== undefined) throw new TypeError('Unsupported argument in 2.0.0')

// Version
var network = this.keyPair.network
var version = (!this.isNeutered()) ? network.bip32.private : network.bip32.public
var buffer = Buffer.allocUnsafe(78)

// 4 bytes: version bytes
buffer.writeUInt32BE(version, 0)
fork icon121
star icon3
watch icon2

+ 7 other calls in file

50
51
52
53
54
55
56
57
58
59
60
    buf.readUInt32BE(12)
  ]
}


function fromArray (out) {
  var buf = Buffer.allocUnsafe(16)
  buf.writeUInt32BE(out[0] >>> 0, 0)
  buf.writeUInt32BE(out[1] >>> 0, 4)
  buf.writeUInt32BE(out[2] >>> 0, 8)
  buf.writeUInt32BE(out[3] >>> 0, 12)
fork icon25
star icon56
watch icon0

46
47
48
49
50
51
52
53
54
55
}

function encode (number) {
  var value = Math.abs(number)
  var size = scriptNumSize(value)
  var buffer = Buffer.allocUnsafe(size)
  var negative = number < 0

  for (var i = 0; i < size; ++i) {
    buffer.writeUInt8(value & 0xff, i)
fork icon4
star icon13
watch icon3

+ 11 other calls in file

48
49
50
51
52
53
54
55
56
57
}

// for use with Bitcore
function toBitcore() {
    // reverse magic
    let nm = Buffer.allocUnsafe(4);
    nm.writeUInt32BE(this.protocol ? this.protocol.magic : 0, 0);
    nm = nm.readUInt32LE(0);

    return Object.assign({}, this, {
fork icon693
star icon0
watch icon148

363
364
365
366
367
368
369
370
371
372
  else lengths[i] = Buffer.byteLength(list[i])

  length += lengths[i]
}

result = Buffer.allocUnsafe(length)

for (i = 0; i < list.length && list[i]; i++) {
  if (typeof list[i] !== 'string') {
    list[i].copy(result, pos)
fork icon7
star icon10
watch icon3

+ 3 other calls in file

165
166
167
168
169
170
171
172
173
174
  this._gh = (this._gh + gh + getCarry(this._gl, gl)) | 0
  this._hh = (this._hh + hh + getCarry(this._hl, hl)) | 0
}

Sha512.prototype._hash = function () {
  var H = Buffer.allocUnsafe(64)

  function writeInt64BE (h, l, offset) {
    H.writeInt32BE(h, offset)
    H.writeInt32BE(l, offset + 4)
fork icon1
star icon1
watch icon1

+ 3 other calls in file

148
149
150
151
152
153
154
155
156
157
let it = size - length
while (it !== size && b256[it] === 0) {
  it++
}

const vch = Buffer.allocUnsafe(zeroes + (size - it))
vch.fill(0x00, 0, zeroes)

let j = zeroes
while (it !== size) {
fork icon0
star icon2
watch icon0

558
559
560
561
562
563
564
565
566
567
  for (i = 0; i < list.length; ++i) {
    length += list[i].length
  }
}

var buffer = Buffer.allocUnsafe(length)
var pos = 0
for (i = 0; i < list.length; ++i) {
  var buf = list[i]
  if (isInstance(buf, Uint8Array)) {
fork icon0
star icon4
watch icon0

+ 27 other calls in file

3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
  this._d = (d + this._d) | 0
  this._e = (e + this._e) | 0
}


Sha.prototype._hash = function () {
  var H = Buffer.allocUnsafe(20)


  H.writeInt32BE(this._a | 0, 0)
  H.writeInt32BE(this._b | 0, 4)
  H.writeInt32BE(this._c | 0, 8)
fork icon0
star icon1
watch icon5

+ 48 other calls in file

3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
} else if (clone.__isDate(parent)) {
	child = new Date(parent.getTime());
} else if (useBuffer && Buffer.isBuffer(parent)) {
	if (Buffer.allocUnsafe) {
		// Node.js >= 4.5.0
		child = Buffer.allocUnsafe(parent.length);
	} else {
		// Older Node.js versions
		child = new Buffer(parent.length);
	}
fork icon1
star icon3
watch icon2

+ 5 other calls in file

4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
    // Skip leading zeroes in b256.
var it4 = size - length
while (it4 !== size && b256[it4] === 0) {
  it4++
}
var vch = _Buffer.allocUnsafe(zeroes + (size - it4))
vch.fill(0x00, 0, zeroes)
var j = zeroes
while (it4 !== size) {
  vch[j++] = b256[it4++]
fork icon0
star icon1
watch icon0

237
238
239
240
241
242
243
244
245
246

_sendFrame: function(frame) {
  var length = frame.length,
      header = (length <= 125) ? 2 : (length <= 65535 ? 4 : 10),
      offset = header + (frame.masked ? 4 : 0),
      buffer = Buffer.allocUnsafe(offset + length),
      masked = frame.masked ? this.MASK : 0;

  buffer[0] = (frame.final ? this.FIN : 0) |
              (frame.rsv1 ? this.RSV1 : 0) |
fork icon0
star icon0
watch icon1

5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
  while (p = p.next) ret += s + p.data;
  return ret;
}
concat(n) {
  if (this.length === 0) return Buffer.alloc(0);
  const ret = Buffer.allocUnsafe(n >>> 0);
  var p = this.head;
  var i = 0;
  while (p) {
    copyBuffer(p.data, ret, i);
fork icon0
star icon0
watch icon1

+ 89 other calls in file

90
91
92
93
94
95
96
97
98
99
100
101
  })
}


function toBitcore () {
  // reverse magic
  var nm = Buffer.allocUnsafe(4)
  nm.writeUInt32BE(this.protocol ? this.protocol.magic : 0, 0)
  nm = nm.readUInt32LE(0)


  return Object.assign({}, this, {
fork icon0
star icon0
watch icon0