Data Signature

Data Signing

📘

This feature requires the Data Signing permission.

HandCash users can sign transactions publicly.

This can be used by the developer to issue a public record of the user's intent to publish.

Signing the data

There are three different types of data you can sign:

UTF-8:

"Sign this message!!!"

Base64:

'IlNpZ24gdGhpcyBtZXNzYWdlISEhIgo='

HEX:

'225369676E2074686973206D65737361676521212122'

Publicly sign a message using the code example below:

const {HandCashConnect} = require('@handcash/handcash-connect');
const handCashConnect = new HandCashConnect({ 
   appId: '<app-id>', 
   appSecret: '<secret>',
}); 

const account =  handCashConnect.getAccountFromAuthToken(token);

const message = Buffer.from("Sign this message!!!").toString('hex')
const { publicKey, signature } = await account.profile.signData({
    value: message,
    format: 'hex'
})

Verify a public signature with the following:

const { PublicKey } = require('bsv')
const Message = require('bsv/message');

const message = '225369676E2074686973206D65737361676521212122';
console.log(Buffer.from(message).toString())

const verified = Message.verify(Buffer.from(message, 'hex'), PublicKey(publicKey).toAddress(), signature);
console.log(verified);
Sign this message!!!
true

Combine with a transaction.

Attach data with signature to a transaction:

const {HandCashConnect} = require('@handcash/handcash-connect');
const handCashConnect = new HandCashConnect({ 
   appId: '<app-id>', 
   appSecret: '<secret>',
}); 

const text = "Sign this message!!!"
const message = Buffer.from(text).toString('hex')

const { signature } = await account.profile.signData({
    value: message,
    format: 'hex'
})

const account = handCashConnect.getAccountFromAuthToken(token);
const paymentParameters = {
    payments: [
        { destination: 'nosetwo', currencyCode: 'USD', sendAmount: 0.25 },
    ],
    attachment: { 
        format: 'json', 
        value: {
            "message": message, 
            "signature": signature
        } 
    },
};

const paymentResult = await account.wallet.pay(paymentParameters);
console.log(paymentResult);

Did this page help you?