Skip to content

Add a Serializable Credential Class #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 15 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dist/constants/vdxf.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export declare const VDXF_OBJECT_DEFAULT_VERSION: import("bn.js");
export declare const HASH160_BYTE_LENGTH = 20;
export declare const I_ADDR_VERSION = 102;
export declare const R_ADDR_VERSION = 60;
export declare const NULL_ADDRESS = "i3UXS5QPRQGNRDDqVnyWTnmFCTHDbzmsYk";
export declare const VERUS_DATA_SIGNATURE_PREFIX: Buffer;
3 changes: 2 additions & 1 deletion dist/constants/vdxf.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.VERUS_DATA_SIGNATURE_PREFIX = exports.R_ADDR_VERSION = exports.I_ADDR_VERSION = exports.HASH160_BYTE_LENGTH = exports.VDXF_OBJECT_DEFAULT_VERSION = void 0;
exports.VERUS_DATA_SIGNATURE_PREFIX = exports.NULL_ADDRESS = exports.R_ADDR_VERSION = exports.I_ADDR_VERSION = exports.HASH160_BYTE_LENGTH = exports.VDXF_OBJECT_DEFAULT_VERSION = void 0;
const bn_js_1 = require("bn.js");
const bufferutils_1 = require("../utils/bufferutils");
exports.VDXF_OBJECT_DEFAULT_VERSION = new bn_js_1.BN(1, 10);
exports.HASH160_BYTE_LENGTH = 20;
exports.I_ADDR_VERSION = 102;
exports.R_ADDR_VERSION = 60;
exports.NULL_ADDRESS = "i3UXS5QPRQGNRDDqVnyWTnmFCTHDbzmsYk";
const VERUS_DATA_SIGNATURE_PREFIX_STRING = "Verus signed data:\n";
var bufferWriter = new bufferutils_1.default.BufferWriter(Buffer.alloc(VERUS_DATA_SIGNATURE_PREFIX_STRING.length + 1));
bufferWriter.writeVarSlice(Buffer.from(VERUS_DATA_SIGNATURE_PREFIX_STRING, "utf-8"));
Expand Down
42 changes: 42 additions & 0 deletions dist/pbaas/Credential.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/// <reference types="bn.js" />
/// <reference types="node" />
import { BigNumber } from "../utils/types/BigNumber";
import { SerializableEntity } from "../utils/types/SerializableEntity";
export declare type CredentialJSON = {
version?: number;
flags?: number;
credentialKey?: string;
credential?: Object;
scopes?: Object;
label?: string;
};
export declare class Credential implements SerializableEntity {
static VERSION_INVALID: import("bn.js");
static VERSION_FIRST: import("bn.js");
static VERSION_LAST: import("bn.js");
static VERSION_CURRENT: import("bn.js");
static FLAG_LABEL_PRESENT: import("bn.js");
version: BigNumber;
flags: BigNumber;
credentialKey: string;
credential: Object;
scopes: Object;
label: string;
constructor(data?: {
version?: BigNumber;
flags?: BigNumber;
credentialKey?: string;
credential?: Object;
scopes?: Object;
label?: string;
});
getByteLength(): number;
toBuffer(): Buffer;
fromBuffer(buffer: Buffer, offset?: number): number;
hasLabel(): boolean;
calcFlags(): BigNumber;
setFlags(): void;
isValid(): boolean;
toJSON(): CredentialJSON;
static fromJSON(json: CredentialJSON): Credential;
}
121 changes: 121 additions & 0 deletions dist/pbaas/Credential.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Credential = void 0;
const bn_js_1 = require("bn.js");
const bufferutils_1 = require("../utils/bufferutils");
const varuint_1 = require("../utils/varuint");
const vdxf_1 = require("../constants/vdxf");
const { BufferReader, BufferWriter } = bufferutils_1.default;
class Credential {
constructor(data) {
this.version = Credential.VERSION_INVALID;
this.flags = new bn_js_1.BN(0, 10);
this.credentialKey = "";
this.credential = {};
this.scopes = {};
this.label = "";
if (data) {
if (data.flags)
this.flags = new bn_js_1.BN(data.flags);
if (data.version)
this.version = new bn_js_1.BN(data.version);
if (data.credentialKey)
this.credentialKey = data.credentialKey;
if (data.credential)
this.credential = data.credential;
if (data.scopes)
this.scopes = data.scopes;
if (data.label)
this.label = data.label;
this.setFlags();
}
}
getByteLength() {
let length = 0;
length += 4; // version (UInt32)
length += 4; // flags (UInt32)
const credentialKeyLength = this.credentialKey.length;
length += varuint_1.default.encodingLength(credentialKeyLength);
length += credentialKeyLength;
// Both the credential and scopes are serialized as JSON strings.
const credStr = JSON.stringify(this.credential);
const credentialLength = credStr.length;
length += varuint_1.default.encodingLength(credentialLength);
length += credentialLength;
const scopesStr = JSON.stringify(this.scopes);
const scopesLength = scopesStr.length;
length += varuint_1.default.encodingLength(scopesLength);
length += scopesLength;
if (this.hasLabel()) {
length += varuint_1.default.encodingLength(this.label.length);
length += Buffer.from(this.label).length;
}
return length;
}
toBuffer() {
const writer = new BufferWriter(Buffer.alloc(this.getByteLength()));
writer.writeUInt32(this.version.toNumber());
writer.writeUInt32(this.flags.toNumber());
writer.writeVarSlice(Buffer.from(this.credentialKey));
writer.writeVarSlice(Buffer.from(JSON.stringify(this.credential)));
writer.writeVarSlice(Buffer.from(JSON.stringify(this.scopes)));
if (this.hasLabel()) {
writer.writeVarSlice(Buffer.from(this.label));
}
return writer.buffer;
}
fromBuffer(buffer, offset) {
const reader = new BufferReader(buffer, offset);
this.version = new bn_js_1.BN(reader.readUInt32(), 10);
this.flags = new bn_js_1.BN(reader.readUInt32(), 10);
this.credentialKey = Buffer.from(reader.readVarSlice()).toString();
this.credential = JSON.parse(Buffer.from(reader.readVarSlice()).toString());
this.scopes = JSON.parse(Buffer.from(reader.readVarSlice()).toString());
if (this.hasLabel()) {
this.label = Buffer.from(reader.readVarSlice()).toString();
}
return reader.offset;
}
hasLabel() {
return this.flags.and(Credential.FLAG_LABEL_PRESENT).gt(new bn_js_1.BN(0, 10));
}
calcFlags() {
return this.label.length > 0 ? Credential.FLAG_LABEL_PRESENT : new bn_js_1.BN(0, 10);
}
setFlags() {
this.flags = this.calcFlags();
}
// The credentials is invalid if the version is not within the valid range or the key is null.
isValid() {
return this.version.gte(Credential.VERSION_FIRST) && this.version.lte(Credential.VERSION_LAST)
&& this.credentialKey !== vdxf_1.NULL_ADDRESS;
}
toJSON() {
const ret = {
version: this.version.toNumber(),
flags: this.flags.toNumber(),
credentialKey: this.credentialKey,
credential: this.credential,
scopes: this.scopes,
label: this.hasLabel() ? this.label : null
};
return ret;
}
static fromJSON(json) {
return new Credential({
version: json.version ? new bn_js_1.BN(json.version, 10) : undefined,
flags: json.flags ? new bn_js_1.BN(json.flags, 10) : undefined,
credentialKey: json.credentialKey,
credential: json.credential,
scopes: json.scopes,
label: json.label,
});
}
}
exports.Credential = Credential;
// Credential enum types
Credential.VERSION_INVALID = new bn_js_1.BN(0, 10);
Credential.VERSION_FIRST = new bn_js_1.BN(1, 10);
Credential.VERSION_LAST = new bn_js_1.BN(1, 10);
Credential.VERSION_CURRENT = new bn_js_1.BN(1, 10);
Credential.FLAG_LABEL_PRESENT = new bn_js_1.BN(1, 10);
1 change: 1 addition & 0 deletions dist/pbaas/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './Credential';
export * from './CurrencyValueMap';
export * from './Identity';
export * from './ReserveTransfer';
Expand Down
1 change: 1 addition & 0 deletions dist/pbaas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
__exportStar(require("./Credential"), exports);
__exportStar(require("./CurrencyValueMap"), exports);
__exportStar(require("./Identity"), exports);
__exportStar(require("./ReserveTransfer"), exports);
Expand Down
3 changes: 3 additions & 0 deletions dist/vdxf/classes/Decision.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference types="node" />
import { VDXFObject } from "..";
import { Credential } from "../../pbaas/Credential";
import { Attestation } from "./Challenge";
import { Context } from "./Context";
import { Request, RequestInterface } from "./Request";
Expand All @@ -11,6 +12,7 @@ export interface DecisionInterface {
skipped?: boolean;
context?: Context;
attestations?: Array<Attestation>;
credentials?: Array<Credential>;
}
export declare class Decision extends VDXFObject {
decision_id: string;
Expand All @@ -20,6 +22,7 @@ export declare class Decision extends VDXFObject {
skipped?: boolean;
attestations: Array<any>;
salt?: string;
credentials: Array<Credential>;
constructor(decision?: DecisionInterface, vdxfkey?: string);
dataByteLength(): number;
toDataBuffer(): Buffer;
Expand Down
30 changes: 30 additions & 0 deletions dist/vdxf/classes/Decision.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.Decision = void 0;
const __1 = require("..");
const bufferutils_1 = require("../../utils/bufferutils");
const Credential_1 = require("../../pbaas/Credential");
const varuint_1 = require("../../utils/varuint");
const Context_1 = require("./Context");
const Hash160_1 = require("./Hash160");
Expand All @@ -21,6 +22,17 @@ class Decision extends __1.VDXFObject {
this.attestations = decision.attestations;
this.salt = decision.salt;
this.skipped = decision.skipped ? true : false;
// Parse the credentials given.
this.credentials = [];
if (decision.credentials && Array.isArray(decision.credentials)) {
// Convert each credential into the Credential class if it isn't already.
this.credentials = decision.credentials.map(cred => {
if (cred instanceof Credential_1.Credential) {
return cred;
}
return new Credential_1.Credential(cred);
});
}
}
dataByteLength() {
let length = 0;
Expand All @@ -40,6 +52,13 @@ class Decision extends __1.VDXFObject {
}
length += _request.byteLength();
length += _context.byteLength();
// The credential list has zero or more credentials.
length += varuint_1.default.encodingLength(this.credentials.length);
for (const cred of this.credentials) {
const credLength = cred.getByteLength();
length += varuint_1.default.encodingLength(credLength);
length += credLength;
}
return length;
}
toDataBuffer() {
Expand All @@ -61,6 +80,10 @@ class Decision extends __1.VDXFObject {
writer.writeArray(_attestations.map((x) => x.toBuffer()));
}
writer.writeSlice(_context.toBuffer());
// The credentials must be written before the request as the provisioning decision
// will read the decision and then the request separately afterwards.
const bufferCreds = this.credentials.map(x => x.toBuffer());
writer.writeVector(bufferCreds);
writer.writeSlice(_request.toBuffer());
return writer.buffer;
}
Expand Down Expand Up @@ -89,6 +112,13 @@ class Decision extends __1.VDXFObject {
const _context = new Context_1.Context();
reader.offset = _context.fromBuffer(reader.buffer, reader.offset);
this.context = _context;
const _credentials = reader.readVector();
this.credentials = [];
for (const _cred of _credentials) {
const cred = new Credential_1.Credential();
cred.fromBuffer(_cred, 0); // Read each credential buffer separately.
this.credentials.push(cred);
}
if (readRequest) {
const _request = new Request_1.Request();
reader.offset = _request.fromBuffer(reader.buffer, reader.offset);
Expand Down
4 changes: 4 additions & 0 deletions dist/vdxf/keys.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ export declare const SIGNED_SESSION_OBJECT_DATA: VDXFKeyInterface;
export declare const SIGNED_SESSION_OBJECT: VDXFKeyInterface;
export declare const CURRENCY_ADDRESS: VDXFKeyInterface;
export declare const DATA_TYPE_STRING: VDXFKeyInterface;
export declare const DATA_TYPE_OBJECT_DATADESCRIPTOR: VDXFKeyInterface;
export declare const DATA_TYPE_OBJECT_CREDENTIAL: VDXFKeyInterface;
export declare const IDENTITY_CREDENTIAL_PLAINLOGIN: VDXFKeyInterface;
export declare const IDENTITY_CREDENTIAL_USERNAME: VDXFKeyInterface;
34 changes: 33 additions & 1 deletion dist/vdxf/keys.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DATA_TYPE_STRING = exports.CURRENCY_ADDRESS = exports.SIGNED_SESSION_OBJECT = exports.SIGNED_SESSION_OBJECT_DATA = exports.LOGIN_CONSENT_PROVISIONING_ERROR_KEY_TRANSFER_FAILED = exports.LOGIN_CONSENT_PROVISIONING_ERROR_KEY_CREATION_FAILED = exports.LOGIN_CONSENT_PROVISIONING_ERROR_KEY_COMMIT_FAILED = exports.LOGIN_CONSENT_PROVISIONING_ERROR_KEY_UNKNOWN = exports.LOGIN_CONSENT_PROVISIONING_ERROR_KEY_NAMETAKEN = exports.LOGIN_CONSENT_PROVISIONING_RESULT_STATE_FAILED = exports.LOGIN_CONSENT_PROVISIONING_RESULT_STATE_COMPLETE = exports.LOGIN_CONSENT_PROVISIONING_RESULT_STATE_PENDINGAPPROVAL = exports.LOGIN_CONSENT_PROVISIONING_RESULT_STATE_PENDINGREQUIREDINFO = exports.IDENTITY_UPDATE_TXID = exports.IDENTITY_REGISTRATION_TXID = exports.IDENTITY_NAME_COMMITMENT_TXID = exports.LOGIN_CONSENT_PROVISIONING_RESULT_VDXF_KEY = exports.LOGIN_CONSENT_PROVISIONING_RESPONSE_VDXF_KEY = exports.LOGIN_CONSENT_PROVISIONING_DECISION_VDXF_KEY = exports.LOGIN_CONSENT_PROVISIONING_CHALLENGE_VDXF_KEY = exports.LOGIN_CONSENT_PROVISIONING_REQUEST_VDXF_KEY = exports.ID_PARENT_VDXF_KEY = exports.ID_FULLYQUALIFIEDNAME_VDXF_KEY = exports.ID_SYSTEMID_VDXF_KEY = exports.ID_ADDRESS_VDXF_KEY = exports.LOGIN_CONSENT_ID_PROVISIONING_WEBHOOK_VDXF_KEY = exports.LOGIN_CONSENT_CONTEXT_VDXF_KEY = exports.LOGIN_CONSENT_WEBHOOK_VDXF_KEY = exports.LOGIN_CONSENT_REDIRECT_VDXF_KEY = exports.WALLET_VDXF_KEY = exports.LOGIN_CONSENT_DECISION_VDXF_KEY = exports.LOGIN_CONSENT_CHALLENGE_VDXF_KEY = exports.LOGIN_CONSENT_RESPONSE_VDXF_KEY = exports.LOGIN_CONSENT_REQUEST_VDXF_KEY = exports.LOGIN_CONSENT_RESPONSE_SIG_VDXF_KEY = exports.IDENTITY_AUTH_SIG_VDXF_KEY = exports.VERUSPAY_INVOICE_VDXF_KEY = void 0;
exports.IDENTITY_CREDENTIAL_USERNAME = exports.IDENTITY_CREDENTIAL_PLAINLOGIN = exports.DATA_TYPE_OBJECT_CREDENTIAL = exports.DATA_TYPE_OBJECT_DATADESCRIPTOR = exports.DATA_TYPE_STRING = exports.CURRENCY_ADDRESS = exports.SIGNED_SESSION_OBJECT = exports.SIGNED_SESSION_OBJECT_DATA = exports.LOGIN_CONSENT_PROVISIONING_ERROR_KEY_TRANSFER_FAILED = exports.LOGIN_CONSENT_PROVISIONING_ERROR_KEY_CREATION_FAILED = exports.LOGIN_CONSENT_PROVISIONING_ERROR_KEY_COMMIT_FAILED = exports.LOGIN_CONSENT_PROVISIONING_ERROR_KEY_UNKNOWN = exports.LOGIN_CONSENT_PROVISIONING_ERROR_KEY_NAMETAKEN = exports.LOGIN_CONSENT_PROVISIONING_RESULT_STATE_FAILED = exports.LOGIN_CONSENT_PROVISIONING_RESULT_STATE_COMPLETE = exports.LOGIN_CONSENT_PROVISIONING_RESULT_STATE_PENDINGAPPROVAL = exports.LOGIN_CONSENT_PROVISIONING_RESULT_STATE_PENDINGREQUIREDINFO = exports.IDENTITY_UPDATE_TXID = exports.IDENTITY_REGISTRATION_TXID = exports.IDENTITY_NAME_COMMITMENT_TXID = exports.LOGIN_CONSENT_PROVISIONING_RESULT_VDXF_KEY = exports.LOGIN_CONSENT_PROVISIONING_RESPONSE_VDXF_KEY = exports.LOGIN_CONSENT_PROVISIONING_DECISION_VDXF_KEY = exports.LOGIN_CONSENT_PROVISIONING_CHALLENGE_VDXF_KEY = exports.LOGIN_CONSENT_PROVISIONING_REQUEST_VDXF_KEY = exports.ID_PARENT_VDXF_KEY = exports.ID_FULLYQUALIFIEDNAME_VDXF_KEY = exports.ID_SYSTEMID_VDXF_KEY = exports.ID_ADDRESS_VDXF_KEY = exports.LOGIN_CONSENT_ID_PROVISIONING_WEBHOOK_VDXF_KEY = exports.LOGIN_CONSENT_CONTEXT_VDXF_KEY = exports.LOGIN_CONSENT_WEBHOOK_VDXF_KEY = exports.LOGIN_CONSENT_REDIRECT_VDXF_KEY = exports.WALLET_VDXF_KEY = exports.LOGIN_CONSENT_DECISION_VDXF_KEY = exports.LOGIN_CONSENT_CHALLENGE_VDXF_KEY = exports.LOGIN_CONSENT_RESPONSE_VDXF_KEY = exports.LOGIN_CONSENT_REQUEST_VDXF_KEY = exports.LOGIN_CONSENT_RESPONSE_SIG_VDXF_KEY = exports.IDENTITY_AUTH_SIG_VDXF_KEY = exports.VERUSPAY_INVOICE_VDXF_KEY = void 0;
exports.VERUSPAY_INVOICE_VDXF_KEY = {
hash160result: "628efc28c2e2d40050e1a9de7a93e7ddf2aa0076",
qualifiedname: {
Expand Down Expand Up @@ -297,3 +297,35 @@ exports.DATA_TYPE_STRING = {
"name": "vrsc::data.type.string"
},
};
exports.DATA_TYPE_OBJECT_DATADESCRIPTOR = {
vdxfid: "i4GC1YGEVD21afWudGoFJVdnfjJ5XWnCQv",
hash160result: "4d4f12424ded2033a526a4e2a8835fc5b2eba208",
qualifiedname: {
namespace: "i5w5MuNik5NtLcYmNzcvaoixooEebB6MGV",
name: "vrsc::data.type.object.datadescriptor"
},
};
exports.DATA_TYPE_OBJECT_CREDENTIAL = {
vdxfid: "iDTG49YLqmkHMYRyuQBYgEyTByQwAzqGd6",
hash160result: "09fbc202710c9f2dacb87e5623e97e2e4101746d",
qualifiedname: {
namespace: "i5w5MuNik5NtLcYmNzcvaoixooEebB6MGV",
name: "vrsc::data.type.object.credential"
},
};
exports.IDENTITY_CREDENTIAL_PLAINLOGIN = {
vdxfid: "iHh1FFVvcNb2mcBudD11umfKJXHbBbH6Sj",
hash160result: "21edefb10b2ea96ffb0fbad986e268164df8ed9b",
qualifiedname: {
namespace: "i5w5MuNik5NtLcYmNzcvaoixooEebB6MGV",
name: "vrsc::identity.credential.plainlogin"
},
};
exports.IDENTITY_CREDENTIAL_USERNAME = {
vdxfid: "iN6LYCurcypx7orxkFB73mWRq6Jetf23ck",
hash160result: "9125e70938468eea614a4f538199fa4d052538cc",
qualifiedname: {
namespace: "i5w5MuNik5NtLcYmNzcvaoixooEebB6MGV",
name: "vrsc::identity.credential.username"
},
};
Loading