Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async sign(bytes: string, _watermark?: Uint8Array) {
const prefixSig = await tezbridge.request({
method: 'raw_sign',
bytes, // Any operation bytes as string
});
const decoded = b58cdecode(prefixSig, prefix[prefixSig.substr(0, 5)]);
return {
bytes,
sig: b58cencode(decoded, prefix.sig),
prefixSig,
sbytes: bytes + buf2hex(toBuffer(decoded)),
};
}
}
constructor(private key: string, encrypted: boolean, decrypt: (k: any) => any) {
this._key = decrypt(b58cdecode(this.key, prefix[key.substr(0, encrypted ? 5 : 4)]));
this._publicKey = this._key.slice(32);
if (!this._key) {
throw new Error('Unable to decode key');
}
this.isInit = this.init();
}
constructor(
private curve: 'p256' | 'secp256k1',
private key: string,
encrypted: boolean,
decrypt: (k: any) => any
) {
this._key = decrypt(b58cdecode(this.key, prefix[key.substr(0, encrypted ? 5 : 4)]));
const keyPair = new elliptic.ec(this.curve).keyFromPrivate(this._key);
const pref =
keyPair
.getPublic()
.getY()
.toArray()[31] % 2
? 3
: 2;
this._publicKey = toBuffer(
new Uint8Array(
[pref].concat(
keyPair
.getPublic()
.getX()
.toArray()
)
async sign(bytes: string, watermark?: Uint8Array) {
try {
let bb = hex2buf(bytes);
if (typeof watermark !== 'undefined') {
bb = mergebuf(watermark, bb);
}
const { signature } = await this.http.createRequest(
{ url: this.createURL(`/keys/${this.pkh}`), method: 'POST' },
buf2hex(toBuffer(bb))
);
let pref = signature.startsWith('sig') ? signature.substr(0, 3) : signature.substr(0, 5);
const decoded = b58cdecode(signature, prefix[pref]);
return {
bytes,
sig: b58cencode(decoded, prefix.sig),
prefixSig: signature,
sbytes: bytes + buf2hex(toBuffer(decoded)),
};
} catch (ex) {
if (ex instanceof HttpResponseError) {
if (ex.status === STATUS_CODE.NOT_FOUND) {
throw new KeyNotFoundError(`Key not found: ${this.pkh}`, ex);
} else if (ex.status === STATUS_CODE.FORBIDDEN) {
throw new OperationNotAuthorizedError('Signing Operation not authorized', ex);
} else if (ex.status === STATUS_CODE.BAD_REQUEST) {
throw new BadSigningDataError('Invalid data', ex, {
bytes,