Implementation of the IETF HTTP Message Signatures draft standard
npm install @ltonetwork/http-message-signaturesSigner and
Verify callback functions for signing and verifying messages, respectively. This allows you to choose your preferred
javascript
import { sign } from '@ltonetwork/http-message-signatures';
const signer = {
keyid: 'test-key',
alg: 'hmac-sha256',
sign: (data) => {
// ... Sign the data using your preferred cryptographic library
}
};
const request = {
method: 'POST',
url: 'https://example.com/api/data',
headers: {
'Content-Type': 'application/json',
'Digest': 'sha-256=4VYMyeX0tNLQ7opuAJeMECP3HgfLswAG3n+IqQprO0Q=',
}
};
(async () => {
const signedRequest = await sign(request, { signer });
// ... Send the signed request to the server
})();
`
$3
`javascript
import { verify } from '@ltonetwork/http-message-signatures';
const verifyCallback = async (data, signature, params) => {
const account = await getAccount(params.keyid);
// ... Verify the signature using your preferred cryptographic library
if (!valid) throw new Error('Invalid signature');
return account;
};
const request = {
method: 'POST',
url: 'https://example.com/api/data',
headers: {
'Content-Type': 'application/json',
'Digest': 'sha-256=4VYMyeX0tNLQ7opuAJeMECP3HgfLswAG3n+IqQprO0Q=',
'Signature': 'keyid="test-key",algorithm="hmac-sha256",signature="base64encodedsignature"',
'Signature-Input': 'sig1=("@method" "@path" "@authority" "content-type" "digest");created=1618884475'
}
};
(async () => {
try {
const verified = await verify(request, verifyCallback);
console.log('Verification succeeded');
} catch (err) {
console.error('Verification failed:', err.message);
}
})();
``