@chorus-one/signer-fireblocks
Version:
Fireblocks signer for Chorus One SDK
36 lines (35 loc) • 1.1 kB
JavaScript
import { v4 as uuid } from 'uuid';
import { KJUR, KEYUTIL } from 'jsrsasign';
export class ApiTokenProvider {
privateKey;
apiKey;
constructor(privateKey, apiKey) {
this.privateKey = privateKey;
this.apiKey = apiKey;
}
signJwt(path, bodyJson) {
const header = {
alg: 'RS256',
typ: 'JWT'
};
const payload = {
uri: path,
nonce: uuid(),
iat: Math.floor(Date.now() / 1000),
exp: Math.floor(Date.now() / 1000) + 55,
sub: this.apiKey,
bodyHash: KJUR.crypto.Util.sha256(JSON.stringify(bodyJson || ''))
};
const sHeader = JSON.stringify(header);
const sPayload = JSON.stringify(payload);
const privateKeyObject = KEYUTIL.getKey(this.privateKey);
const token = KJUR.jws.JWS.sign('RS256', sHeader, sPayload, privateKeyObject);
return token;
}
getApiKey() {
return this.apiKey;
}
}
export function getAuthProvider(privateKey, apiKey) {
return new ApiTokenProvider(privateKey, apiKey);
}