本文整理汇总了TypeScript中buffer.Buffer.from方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Buffer.from方法的具体用法?TypeScript Buffer.from怎么用?TypeScript Buffer.from使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类buffer.Buffer
的用法示例。
在下文中一共展示了Buffer.from方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: encodeString
function encodeString(s: string): Buffer {
let bytes = Buffer.from(s);
return Buffer.concat([
Buffer.from(bytes.length.toString()),
Buffer.from(':'),
bytes]);
}
示例2: encodeArray
function encodeArray(l: Array<any>): Buffer {
let result: Array<Buffer> = [Buffer.from('l')];
l.forEach(element => {
result.push(_encode(element));
});
result.push(Buffer.from('e'))
return Buffer.concat(result);
}
示例3: encodeDict
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);
}
示例4: function
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()
})
示例5: Keyring
return passworder.decrypt(password, unbase64).then((privateKey: Buffer|BufferLike) => {
if (isBufferLike(privateKey)) {
return new Keyring(Buffer.from(privateKey.data))
} else {
return new Keyring(privateKey)
}
})
示例6: FakeTransaction
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',
)
})
示例7: it
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);
});
示例8: function
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')
})
示例9: deserialize
static deserialize (string: string, password: string): Promise<Keyring> {
let unbase64 = Buffer.from(string, "base64").toString();
return passworder.decrypt(password, unbase64).then((privateKey: Buffer|BufferLike) => {
if (isBufferLike(privateKey)) {
return new Keyring(Buffer.from(privateKey.data))
} else {
return new Keyring(privateKey)
}
})
}