How to use the bsv.Message function in bsv

To help you get started, we’ve selected a few bsv examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github BitcoinFiles / bitcoinfiles-sdk / lib / utils.ts View on Github external
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'};
    }
github BitcoinFiles / bitcoinfiles-sdk / test / utils.js View on Github external
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);
    });
github BitcoinFiles / bitcoinfiles-sdk / test / utils.js View on Github external
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
        });
github BitcoinFiles / bitcoinfiles-sdk / lib / utils.ts View on Github external
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;
    }
github BitcoinFiles / bitcoinfiles-sdk / lib / utils.ts View on Github external
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;
    }