Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (index < 0) {
return {valid: false, message: "field index out of bounds < 0" };
}
if (index >= args.length) {
return {valid: false, message: "field index out of bounds > length" };
}
fieldsToSign.push(args[index]);
}
const bufWriter = new bsv.encoding.BufferWriter();
for (const fieldToSign of fieldsToSign) {
let bf = Buffer.from(fieldToSign, 'hex');
bufWriter.write(bf);
}
const appData = bufWriter.toBuffer();
//try {
const verified = bsv.Message(appData).verify(address, signature);
if (verified) {
return {
valid: true,
address: address,
signature: signature,
pos: pos,
fieldIndexesForSignature: fieldIndexesForSignature
};
}
// } catch (ex) {
// Fail silently
// todo: add debug/verbose mode in future
// console.log('ex', ex);
/// }
return {valid: false, message: 'signature not match'};
}
it('#signArguments should encode abde', async () => {
const result = index.signArguments({
args: ['abde'],
address: address,
key: privateKey,
indexes: [0]
});
const expectedSignature = bsv.Message(Buffer.from('abdE', 'hex')).sign(bsv.PrivateKey(privateKey))
expect(result).to.eql(expectedSignature);
});
args: ['0x6a', Buffer.from('|'), '0x01', Buffer.from('hello, world'), '0x103A', '0x', '', Buffer.from('|')],
address: address,
key: privateKey
});
const bufs = Buffer.concat([
Buffer.from('6a', 'hex'),
Buffer.from('|'),
Buffer.from('01', 'hex'),
Buffer.from('hello, world'),
Buffer.from('103a', 'hex'),
Buffer.from('00', 'hex'),
Buffer.from('00', 'hex'),
Buffer.from('|'),
]);
const expectedSignature = bsv.Message(bufs).sign(bsv.PrivateKey(privateKey))
expect(signature).to.eql(expectedSignature);
const opReturnHexArray = index.buildAuthorIdentity({
args: [
Buffer.from('|'),
'0x01',
Buffer.from('hello, world'),
'0x103A',
'0x',
'',
Buffer.from('|')
],
address: address,
key: privateKey
});
if (!usedArgs || !usedArgs.length) {
throw new Error('insufficient index args');
}
const bufferWriter = new bsv.encoding.BufferWriter();
for (const field of usedArgs) {
let bf = field;
if (!Buffer.isBuffer(field)) {
bf = new bsv.encoding.BufferReader(field);
bf = bf.buf;
}
console.log('bf', bf);
bufferWriter.write(bf);
}
const appData = bufferWriter.toBuffer();
const signature = bsv.Message(appData).sign(bsv.PrivateKey(payload.key))
const verified = bsv.Message(appData).verify(payload.address, signature);
if (!verified) {
throw new Error('signArguments - Signature verification failure: ' + signature);
}
return signature;
}
throw new Error('insufficient index args');
}
const bufferWriter = new bsv.encoding.BufferWriter();
for (const field of usedArgs) {
let bf = field;
if (!Buffer.isBuffer(field)) {
bf = new bsv.encoding.BufferReader(field);
bf = bf.buf;
}
console.log('bf', bf);
bufferWriter.write(bf);
}
const appData = bufferWriter.toBuffer();
const signature = bsv.Message(appData).sign(bsv.PrivateKey(payload.key))
const verified = bsv.Message(appData).verify(payload.address, signature);
if (!verified) {
throw new Error('signArguments - Signature verification failure: ' + signature);
}
return signature;
}