Skip to content
Merged
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
125 changes: 71 additions & 54 deletions cjs/client.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -23452,7 +23452,7 @@ Crypto.prototype.extractPin = function (mpinId, publicKey, PIN, clientSecretHex,
return this._bytesToHex(clientSecretBytes);
};

Crypto.prototype.calculatePass1 = function (curve, mpinId, publicKey, token, userPin, X, SEC) {
Crypto.prototype.calculatePass1 = function (mpinId, publicKey, token, userPin, X, SEC, curve) {
const U = [], UT = [];

const mpinIdHex = this._mpinIdWithPublicKey(mpinId, publicKey);
Expand Down Expand Up @@ -23481,7 +23481,7 @@ Crypto.prototype.calculatePass1 = function (curve, mpinId, publicKey, token, use
};
};

Crypto.prototype.calculatePass2 = function (curve, X, yHex, SEC) {
Crypto.prototype.calculatePass2 = function (X, yHex, SEC, curve) {
const errorCode = this._crypto(curve).MPIN.CLIENT_2(X, this._hexToBytes(yHex), SEC);
if (errorCode !== 0) {
throw new Error("Could not calculate pass 2 request data: " + errorCode);
Expand All @@ -23490,7 +23490,7 @@ Crypto.prototype.calculatePass2 = function (curve, X, yHex, SEC) {
return this._bytesToHex(SEC);
};

Crypto.prototype.sign = function (curve, mpinId, publicKey, token, userPin, message, timestamp) {
Crypto.prototype.sign = function (mpinId, publicKey, token, userPin, message, timestamp, curve) {
const SEC = [];
const X = [];
const Y1 = [];
Expand Down Expand Up @@ -23583,9 +23583,11 @@ Crypto.prototype._bytesToHex = function (b) {
return s;
};

function HTTP(timeout, clientName, projectId, cors) {
function HTTP(timeout, clientName, deviceName, deviceTag, projectId, cors) {
this.requestTimeout = timeout;
this.clientName = clientName;
this.deviceName = deviceName;
this.deviceTag = deviceTag;
this.projectId = projectId;
this.cors = cors;
}
Expand Down Expand Up @@ -23648,8 +23650,9 @@ HTTP.prototype.request = function (options, callback) {

request.timeout = this.requestTimeout;

request.setRequestHeader("X-MIRACL-CID", this.projectId);
request.setRequestHeader("X-MIRACL-CLIENT", this.clientName);
request.setRequestHeader("X-Miracl-Client", this.clientName);
request.setRequestHeader("X-Miracl-Device-Name", this.deviceName);
request.setRequestHeader("X-Miracl-Device-Tag", this.deviceTag);

// Set authorization header if provided
if (options.authorization) {
Expand Down Expand Up @@ -23905,11 +23908,11 @@ function Client(options) {
}

// Set the client name using the current lib version and provided application info
options.clientName = "MIRACL Client.js/8.10.0" + (options.applicationInfo ? " " + options.applicationInfo : "");
options.clientName = "MIRACL Client.js/8.11.0" + (options.applicationInfo ? " " + options.applicationInfo : "");

this.options = options;

this.http = new HTTP(options.requestTimeout, options.clientName, options.projectId, options.cors);
this.http = new HTTP(options.requestTimeout, options.clientName, this._getDeviceName(), this._getDeviceTag(), options.projectId, options.cors);

this.crypto = new Crypto(options.seed);

Expand Down Expand Up @@ -24113,7 +24116,7 @@ Client.prototype.register = function (userId, activationToken, pinCallback, call
return callback(new Error("Empty activation token"), null);
}

const keypair = this.crypto.generateKeypair("BN254CX");
const keypair = this.crypto.generateKeypair();

this._createMPinID(userId, activationToken, keypair, (err, identityData) => {
if (err) {
Expand All @@ -24128,28 +24131,22 @@ Client.prototype.register = function (userId, activationToken, pinCallback, call
return callback(new Error("Project mismatch"), null);
}

this._getSecret(identityData.secretUrls[0], (err, sec1Data) => {
let pinLength = identityData.pinLength;
if (!pinLength) {
pinLength = this.options.defaultPinLength;
}

this._getTAShares(identityData.designatedTAs, identityData.mpinId, keypair.publicKey, (err, secData) => {
if (err) {
return callback(new Error("Registration fail", { cause: err }), null);
}

this._getSecret(identityData.secretUrls[1], (err, sec2Data) => {
if (err) {
return callback(new Error("Registration fail", { cause: err }), null);
}
// Should be called to continue the flow after a PIN was provided
const passPin = (userPin) => {
this._createIdentity(userId, userPin, identityData, secData[0], secData[1], keypair, callback);
};

let pinLength = identityData.pinLength;
if (!pinLength) {
pinLength = this.options.defaultPinLength;
}

// Should be called to continue the flow after a PIN was provided
const passPin = (userPin) => {
this._createIdentity(userId, userPin, identityData, sec1Data, sec2Data, keypair, callback);
};

pinCallback(passPin, pinLength);
});
pinCallback(passPin, pinLength);
});
});
};
Expand All @@ -24163,7 +24160,8 @@ Client.prototype._createMPinID = function (userId, activationToken, keypair, cal
deviceName: this._getDeviceName(),
deviceTag: this._getDeviceTag(),
activationToken: activationToken,
publicKey: keypair.publicKey
publicKey: keypair.publicKey,
ver: 2
}
};

Expand All @@ -24172,8 +24170,6 @@ Client.prototype._createMPinID = function (userId, activationToken, keypair, cal
return callback(err, result);
}

this.users.write(userId, { state: this.users.states.start });

callback(null, result);
});
};
Expand Down Expand Up @@ -24203,20 +24199,45 @@ Client.prototype._getDeviceTag = function () {
return newTag;
};

Client.prototype._getSecret = function (secretUrl, callback) {
Client.prototype._getTAShares = function (designatedTas, mpinId, publicKey, callback) {
const results = [];
const errors = [];

let pending = designatedTas.length;

for (let i = 0; i < designatedTas.length; i++) {
this._getTAShare(designatedTas[i], mpinId, publicKey, (err, result) => {
if (err) {
errors.push(err);
} else {
results[i] = result;
}

if (--pending === 0) {
if (errors.length > 0) {
return callback(new Error("Failed to get shares", { cause: errors }), null);
}

callback(null, results);
}
});
}
};

Client.prototype._getTAShare = function (designatedTa, mpinId, publicKey, callback) {
const requestData = {
url: secretUrl
type: "POST",
url: designatedTa.url,
authorization: "Bearer " + designatedTa.token,
data: {
mpinId: mpinId,
pubKey: publicKey
}
};

this.http.request(requestData, (err, result) => {
if (err) {
if (err.message === "The request was aborted") {
this.http.request(requestData, callback);
} else {
callback(err, result);
}

return;
return callback(err, null);
}

callback(null, result);
Expand All @@ -24227,17 +24248,19 @@ Client.prototype._createIdentity = function (userId, userPin, identityData, sec1
let csHex, token;

try {
csHex = this.crypto.addShares(keypair.privateKey, sec1Data.dvsClientSecret, sec2Data.dvsClientSecret, identityData.curve);
token = this.crypto.extractPin(identityData.mpinId, keypair.publicKey, userPin, csHex, identityData.curve);
csHex = this.crypto.addShares(keypair.privateKey, sec1Data.share, sec2Data.share);
token = this.crypto.extractPin(identityData.mpinId, keypair.publicKey, userPin, csHex);
} catch (err) {
return callback(err, null);
}

const dtas = btoa(JSON.stringify([sec1Data.node, sec2Data.node]));

const userData = {
mpinId: identityData.mpinId,
token: token,
curve: identityData.curve,
dtas: identityData.dtas,
curve: "BN254CX",
dtas: dtas,
publicKey: keypair.publicKey,
pinLength: identityData.pinLength,
projectId: identityData.projectId,
Expand Down Expand Up @@ -24413,7 +24436,7 @@ Client.prototype._getPass1 = function (identityData, userPin, scope, X, SEC, cal
let res;

try {
res = this.crypto.calculatePass1(identityData.curve, identityData.mpinId, identityData.publicKey, identityData.token, userPin, X, SEC);
res = this.crypto.calculatePass1(identityData.mpinId, identityData.publicKey, identityData.token, userPin, X, SEC);
} catch (err) {
return callback(err, null);
}
Expand Down Expand Up @@ -24449,7 +24472,7 @@ Client.prototype._getPass2 = function (identityData, scope, yHex, X, SEC, callba
let vHex;

try {
vHex = this.crypto.calculatePass2(identityData.curve, X, yHex, SEC);
vHex = this.crypto.calculatePass2(X, yHex, SEC);
} catch (err) {
return callback(err, null);
}
Expand Down Expand Up @@ -24490,25 +24513,19 @@ Client.prototype._finishAuthentication = function (userId, userPin, scope, authO
};

Client.prototype._renewSecret = function (userId, userPin, activationData, callback) {
const keypair = this.crypto.generateKeypair(activationData.curve);
const keypair = this.crypto.generateKeypair();

this._createMPinID(userId, activationData.token, keypair, (err, identityData) => {
if (err) {
return callback(err, null);
}

this._getSecret(identityData.secretUrls[0], (err, sec1Data) => {
this._getTAShares(identityData.designatedTAs, identityData.mpinId, keypair.publicKey, (err, secData) => {
if (err) {
return callback(err, null);
}

this._getSecret(identityData.secretUrls[1], (err, sec2Data) => {
if (err) {
return callback(err, null);
}

this._createIdentity(userId, userPin, identityData, sec1Data, sec2Data, keypair, callback);
});
this._createIdentity(userId, userPin, identityData, secData[0], secData[1], keypair, callback);
});
});
};
Expand Down Expand Up @@ -24556,7 +24573,7 @@ Client.prototype.sign = function (userId, userPin, message, timestamp, callback)
let res;

try {
res = this.crypto.sign(identityData.curve, identityData.mpinId, identityData.publicKey, identityData.token, userPin, message, timestamp);
res = this.crypto.sign(identityData.mpinId, identityData.publicKey, identityData.token, userPin, message, timestamp);
} catch (err) {
return callback(new Error("Signing fail", { cause: err }), null);
}
Expand Down
Loading
Loading