@ceramicnetwork/core
Version:
Typescript implementation of the Ceramic protocol
94 lines • 5.31 kB
JavaScript
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _DIDAnchorServiceAuth_anchorServiceUrl, _DIDAnchorServiceAuth_logger, _DIDAnchorServiceAuth_signer, _DIDAnchorServiceAuth_fetchFn;
import { fetchJson, } from '@ceramicnetwork/common';
import * as sha256 from '@stablelib/sha256';
import * as uuid from '@stablelib/uuid';
import { CARFactory } from 'cartonne';
import * as u8a from 'uint8arrays';
export class DIDAnchorServiceAuth {
constructor(anchorServiceUrl, logger, signer, fetchFn = fetchJson) {
_DIDAnchorServiceAuth_anchorServiceUrl.set(this, void 0);
_DIDAnchorServiceAuth_logger.set(this, void 0);
_DIDAnchorServiceAuth_signer.set(this, void 0);
_DIDAnchorServiceAuth_fetchFn.set(this, void 0);
__classPrivateFieldSet(this, _DIDAnchorServiceAuth_anchorServiceUrl, anchorServiceUrl, "f");
__classPrivateFieldSet(this, _DIDAnchorServiceAuth_logger, logger, "f");
__classPrivateFieldSet(this, _DIDAnchorServiceAuth_signer, signer, "f");
__classPrivateFieldSet(this, _DIDAnchorServiceAuth_fetchFn, fetchFn, "f");
}
get signer() {
return __classPrivateFieldGet(this, _DIDAnchorServiceAuth_signer, "f");
}
async init() {
return;
}
async sendAuthenticatedRequest(url, opts) {
const { request } = await this.signRequest({ url, opts });
return this._sendRequest(request).catch((err) => {
if (err.message.includes("status 'Unauthorized'")) {
throw new Error(`You are not authorized to use the anchoring service found at: ${__classPrivateFieldGet(this, _DIDAnchorServiceAuth_anchorServiceUrl, "f")}. Are you using the correct anchoring service url? If so please ensure that you have access to 3Box Labs’ Ceramic Anchor Service by following the steps found here: https://composedb.js.org/docs/0.4.x/guides/composedb-server/access-mainnet`);
}
throw err;
});
}
async signRequest(request) {
const payload = { url: request.url, nonce: uuid.uuid() };
const payloadDigest = this._buildSignaturePayloadDigest(request.opts);
if (payloadDigest) {
payload.digest = payloadDigest;
}
const jws = await __classPrivateFieldGet(this, _DIDAnchorServiceAuth_signer, "f").createJWS(payload);
const Authorization = `Bearer ${jws.signatures[0].protected}.${jws.payload}.${jws.signatures[0].signature}`;
let requestOpts = { headers: { Authorization } };
if (request.opts) {
if (request.opts.headers) {
requestOpts.headers = { ...request.opts.headers, Authorization };
}
requestOpts = { ...request.opts, ...requestOpts };
}
request.opts = requestOpts;
return { request, jws };
}
_buildSignaturePayloadDigest(requestOpts) {
if (!requestOpts)
return;
if (requestOpts.body == undefined)
return;
let hash;
if (requestOpts.headers) {
const contentType = requestOpts.headers['Content-Type'];
if (contentType.includes('application/vnd.ipld.car')) {
const carFactory = new CARFactory();
const car = carFactory.fromBytes(requestOpts.body);
return car.roots[0].toString();
}
else if (contentType.includes('application/json')) {
hash = sha256.hash(u8a.fromString(JSON.stringify(requestOpts.body)));
}
}
if (!hash) {
hash = sha256.hash(u8a.fromString(JSON.stringify(requestOpts.body)));
}
return `0x${u8a.toString(hash, 'base16')}`;
}
async _sendRequest(request) {
const data = await __classPrivateFieldGet(this, _DIDAnchorServiceAuth_fetchFn, "f").call(this, request.url, request.opts);
if (data.error) {
__classPrivateFieldGet(this, _DIDAnchorServiceAuth_logger, "f").err(data.error);
return data;
}
return data;
}
}
_DIDAnchorServiceAuth_anchorServiceUrl = new WeakMap(), _DIDAnchorServiceAuth_logger = new WeakMap(), _DIDAnchorServiceAuth_signer = new WeakMap(), _DIDAnchorServiceAuth_fetchFn = new WeakMap();
//# sourceMappingURL=did-anchor-service-auth.js.map