Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return sodium.ready.then(() => {
const header = utils.public(self);
[ data, footer ] = (utils.parse('utf-8'))(data, footer);
// sign
let payload;
try {
payload = utils.pae(header, data, footer);
} catch (ex) {
return done(ex);
}
const _signature = sodium.crypto_sign_detached(payload, key.raw());
const signature = Buffer.from(_signature);
// format
const token = header.toString('utf-8') + utils.toB64URLSafe(Buffer.concat([ data, signature ]));
return (!Buffer.byteLength(footer))
? done(null, token)
: done(null, token + '.' + utils.toB64URLSafe(footer));
});
}
public async signWithPrivateKey(privateKey: Buffer, transaction: RawTezosTransaction): Promise {
await sodium.ready
const watermark: string = '03'
const watermarkedForgedOperationBytesHex: string = watermark + transaction.binaryTransaction
const watermarkedForgedOperationBytes: Buffer = Buffer.from(watermarkedForgedOperationBytesHex, 'hex')
const hashedWatermarkedOpBytes: Buffer = sodium.crypto_generichash(32, watermarkedForgedOperationBytes)
const opSignature: Buffer = sodium.crypto_sign_detached(hashedWatermarkedOpBytes, privateKey)
const signedOpBytes: Buffer = Buffer.concat([Buffer.from(transaction.binaryTransaction, 'hex'), Buffer.from(opSignature)])
return signedOpBytes.toString('hex')
}
async sign(bytes: string, bytesHash: Uint8Array) {
await this.isInit;
const signature = sodium.crypto_sign_detached(
new Uint8Array(bytesHash),
new Uint8Array(this._key)
);
const signatureBuffer = toBuffer(signature);
const sbytes = bytes + buf2hex(signatureBuffer);
return {
bytes,
sig: b58cencode(signature, prefix.sig),
prefixSig: b58cencode(signature, prefix.edsig),
sbytes,
};
}
export function sign(data: Uint8Array, privateKey: Uint8Array): Uint8Array {
return sodium.crypto_sign_detached(data, privateKey);
}
], async (ctx, args) => {
const chann = await core.db.getChannelSecrets(args.eci)
const key = bs58.decode(chann.sovrin.secret.indyPrivate)
return b64url(sodium.crypto_sign_detached(Uint8Array.from(args.bytes), key))
})
private sendSignedMessage(data: Uint8Array) {
const key = this.connection.secretKey;
const signature = sodium.crypto_sign_detached(data, key);
const encodedMessage = proto.SignedMessage.encode({
data,
signature,
}).finish();
this.transport.sendFrame(encodedMessage);
}
}