Example #1
3
export function decode(data: Buffer, encoding?: string): any {
    let value: string;
    let rest: Buffer
    ({ value, rest } = _decode(data, encoding));
    if (rest.length) {
        throw new Error(`Unexpected continuation: "${rest.toString()}"`)
    }
    return value;
}
Example #2
0
function bufferTests() {
    var utf8Buffer = new Buffer('test');
    var base64Buffer = new Buffer('','base64');
    var octets: Uint8Array = null;
    var octetBuffer = new Buffer(octets);
    var sharedBuffer = new Buffer(octets.buffer);
    var copiedBuffer = new Buffer(utf8Buffer);
    console.log(Buffer.isBuffer(octetBuffer));
    console.log(Buffer.isEncoding('utf8'));
    console.log(Buffer.byteLength('xyz123'));
    console.log(Buffer.byteLength('xyz123', 'ascii'));
    var result1 = Buffer.concat([utf8Buffer, base64Buffer]);
    var result2 = Buffer.concat([utf8Buffer, base64Buffer], 9999999);

    // Test that TS 1.6 works with the 'as Buffer' annotation
    // on isBuffer.
    var a: Buffer | number;
    a = new Buffer(10);
    if (Buffer.isBuffer(a)) {
        a.writeUInt8(3, 4);
    }

    // write* methods return offsets.
    var b = new Buffer(16);
    var result: number = b.writeUInt32LE(0, 0);
    result = b.writeUInt16LE(0, 4);
    result = b.writeUInt8(0, 6);
    result = b.writeInt8(0, 7);
    result = b.writeDoubleLE(0, 8);

    // fill returns the input buffer.
    b.fill('a').fill('b');

    {
        let buffer = new Buffer('123');
        let index: number;
        index = buffer.indexOf("23");
        index = buffer.indexOf("23", 1);
        index = buffer.indexOf(23);
        index = buffer.indexOf(buffer);
    }

    // Imported Buffer from buffer module works properly
    {
        let b = new ImportedBuffer('123');
        b.writeUInt8(0, 6);
        let sb = new ImportedSlowBuffer(43);
        b.writeUInt8(0, 6);
    }

    // Buffer has Uint8Array's buffer field (an ArrayBuffer).
    {
      let buffer = new Buffer('123');
      let octets = new Uint8Array(buffer.buffer);
    }
}
Example #3
0
 const streamWrite: NodeJS.WritableStream['write'] = function (chunk: Buffer | string, encoding?: any, callback?: Function) {
   if (Buffer.isBuffer(chunk)) {
     chunk = chunk.toString(encoding)
   }
   process.log(chunk)
   if (callback) {
     callback()
   }
   return true
 }
Example #4
0
function encodeDict(d: any): Buffer {
    let result: Array<Buffer> = [Buffer.from('d')];
    let keys = Object.keys(d).sort();
    keys.forEach(k => {
        result.push(encodeString(k));
        result.push(encode(d[k]))
    });
    result.push(Buffer.from('e'));
    return Buffer.concat(result);
}
Example #5
0
 t.test('Verify EIP155 Signature before and after signing with private key', function(st) {
   // Inputs and expected results for this test are taken directly from the example in https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md
   const txRaw = [
     '0x09',
     '0x4a817c800',
     '0x5208',
     '0x3535353535353535353535353535353535353535',
     '0x0de0b6b3a7640000',
     '0x',
   ]
   const privateKey = Buffer.from(
     '4646464646464646464646464646464646464646464646464646464646464646',
     'hex',
   )
   const pt = new Transaction(txRaw, { chain: 1 })
   st.equal(
     pt.serialize().toString('hex'),
     'ec098504a817c800825208943535353535353535353535353535353535353535880de0b6b3a764000080018080',
   )
   pt.sign(privateKey)
   st.equal(
     pt.hash(false).toString('hex'),
     'daf5a779ae972f972197303d7b574746c7ef83eadac0f2791ad23db92e4c8e53',
   )
   st.equal(
     pt.serialize().toString('hex'),
     'f86c098504a817c800825208943535353535353535353535353535353535353535880de0b6b3a76400008025a028ef61340bd939bc2195fe537567866003e1a15d3c71ff63e1590620aa636276a067cbe9d8997f761aecb703304b3800ccf555c9f3dc64214b297fb1966a3b6d83',
   )
   st.end()
 })
it('test receive right data', () => {
  const buffer = Buffer.from([
    0xda,
    0xbb,
    0x02,
    0x14,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00,
    0x02,
    0x00,
    0x00,
    0x00,
    0x06,
    0x91,
    0x04,
    0x70,
    0x61,
    0x6e,
    0x67,
  ]);
  const dBuff = DecodeBuffer.from(1).subscribe(data => {
    const {requestId, res, err} = decode(data);
    expect(requestId).toEqual(2);
    expect(res).toEqual('pang');
    expect(err).toEqual(null);
  });
  dBuff.receive(buffer);
});
Example #7
0
 t.test('should validate', st => {
   const tx = new FakeTransaction(txData)
   const txWithWrongSignature = new FakeTransaction({
     ...txData,
     r: Buffer.from('abcd1558260ac737ea6d800906c6d085a801e5e0f0952bf93978d6fa468fbdff', 'hex'),
   })
   const txWithLowLimit = new FakeTransaction({
     ...txData,
     gasLimit: '0x1',
   })
   st.plan(6)
   st.true(tx.validate(), 'tx should be valid')
   st.false(txWithWrongSignature.validate(), 'tx should be invalid')
   st.false(txWithLowLimit.validate(), 'tx should be invalid')
   st.equal(tx.validate(true), '', 'tx should return no errors')
   st.equal(
     txWithWrongSignature.validate(true),
     'Invalid Signature',
     'tx should return correct error',
   )
   st.equal(
     txWithLowLimit.validate(true),
     'gas limit is too low. Need at least 21464',
     'tx should return correct error',
   )
 })
Example #8
0
 return passworder.decrypt(password, unbase64).then((privateKey: Buffer|BufferLike) => {
   if (isBufferLike(privateKey)) {
     return new Keyring(Buffer.from(privateKey.data))
   } else {
     return new Keyring(privateKey)
   }
 })
Example #9
0
 t.test('instantiate with from / create a hash', function(st) {
   st.plan(3)
   const tx = new FakeTransaction(txData)
   const hash = tx.hash()
   const cmpHash = Buffer.from(
     'f74b039f6361c4351a99a7c6a10867369fe6701731d85dc07c15671ac1c1b648',
     'hex',
   )
   st.deepEqual(hash, cmpHash, 'should create hash with includeSignature=true (default)')
   const hash2 = tx.hash(false)
   const cmpHash2 = Buffer.from(
     '0401bf740d698674be321d0064f92cd6ebba5d73d1e5e5189c0bebbda33a85fe',
     'hex',
   )
   st.deepEqual(hash2, cmpHash2, 'should create hash with includeSignature=false')
   st.notDeepEqual(hash, hash2, 'previous hashes should be different')
 })
Example #10
0
function decodeString(data: Buffer, encoding?: string): DecodingResult {
    let firstColonIndex = null;
    for (let i = 0; i < data.length; i++) {
        if (data[i] === Delimeters.colon) {
            firstColonIndex = i;
            break;
        }
    }

    if (!firstColonIndex) {
        throw new Error(`Error while decoding ${data.toString(encoding)}. Missing colon.`);
    }

    let expectedLength = parseInt(data.slice(0, firstColonIndex).toString());
    let value = data.slice(firstColonIndex + 1, firstColonIndex + 1 + expectedLength);
    return { value: value.toString(encoding), rest: data.slice(firstColonIndex + 1 + expectedLength) }
}