Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function toItxAddress(itx, type, role = types.RoleType.ROLE_TX) {
if (transactions.indexOf(type) === -1) {
throw new Error(`Unsupported itx type ${type}`);
}
const message = createMessage(type, itx);
// console.log({ message: message.toObject(), itx });
const itxBytes = message.serializeBinary();
const hash = Hasher.SHA3.hash256(itxBytes);
const address = fromHash(hash, role);
return address;
}
const steps = Array.isArray(claims) ? claims : [claims];
if (steps.some(x => x.authPrincipal)) {
// eslint-disable-next-line no-console
console.warn('Setting authPrincipal in claims object is not recommended, and may break things');
}
// Prepend auth principal claim by default
if (authPrincipal) {
let target = '';
let description = 'Please select authentication principal';
let chainInfo;
let targetType;
let declareParams;
if (typeof authPrincipal === 'string') {
if (isValidDid(authPrincipal)) {
// If auth principal is provided as a did
target = authPrincipal;
} else {
// If auth principal is provided as a string
description = authPrincipal;
}
}
if (typeof authPrincipal === 'object') {
target = get(authPrincipal, 'target', target);
description = get(authPrincipal, 'description', description);
targetType = get(authPrincipal, 'targetType', targetType);
declareParams = get(authPrincipal, 'declareParams', declareParams);
// If provided a chainInfo
if (authPrincipal.chainInfo && authenticator._isValidChainInfo(authPrincipal.chainInfo)) {
chainInfo = authPrincipal.chainInfo;
pathname = '',
extraParams = {},
}) {
this._verifyClaims(verifiableClaims);
this._verifyAuthorizer(authorizer);
this._validateAppInfo(appInfo, { address: authorizer.did });
const claimsInfo = await this.genRequestedClaims({ claims, context, extraParams });
const chainInfoParams = Object.assign({}, context, extraParams);
// FIXME: this maybe buggy if user provided multiple claims
const tmp = claimsInfo.find(x => this.getChainInfo(chainInfoParams, x.chainInfo || {}));
const payload = {
action: 'responseAuth',
appInfo,
iss: toDid(authorizer.did),
agentDid: toDid(this.wallet.address),
chainInfo: this.getChainInfo(chainInfoParams, tmp ? tmp.chainInfo : undefined),
verifiableClaims,
requestedClaims: claimsInfo.map(x => {
delete x.chainInfo;
return x;
}),
url: `${this.baseUrl}${pathname}?${qs.stringify(Object.assign({ [this.tokenKey]: token }, extraParams))}`,
};
const signed = {
appPk: toBase58(authorizer.pk),
agentPk: this.appPk,
authInfo: Jwt.sign(this.wallet.address, this.wallet.sk, payload),
};
extraParams = {},
}) {
this._verifyClaims(verifiableClaims);
this._verifyAuthorizer(authorizer);
this._validateAppInfo(appInfo, { address: authorizer.did });
const claimsInfo = await this.genRequestedClaims({ claims, context, extraParams });
const chainInfoParams = Object.assign({}, context, extraParams);
// FIXME: this maybe buggy if user provided multiple claims
const tmp = claimsInfo.find(x => this.getChainInfo(chainInfoParams, x.chainInfo || {}));
const payload = {
action: 'responseAuth',
appInfo,
iss: toDid(authorizer.did),
agentDid: toDid(this.wallet.address),
chainInfo: this.getChainInfo(chainInfoParams, tmp ? tmp.chainInfo : undefined),
verifiableClaims,
requestedClaims: claimsInfo.map(x => {
delete x.chainInfo;
return x;
}),
url: `${this.baseUrl}${pathname}?${qs.stringify(Object.assign({ [this.tokenKey]: token }, extraParams))}`,
};
const signed = {
appPk: toBase58(authorizer.pk),
agentPk: this.appPk,
authInfo: Jwt.sign(this.wallet.address, this.wallet.sk, payload),
};
debug('responseAuth.sign', { context, extraParams, payload, signed });
},
[types.KeyType.ED25519]: {
alg: 'Ed25519',
type: 'JWT',
},
};
// make header
const header = headers[type.pk];
const headerB64 = toBase64(stringify(header));
// make body
const now = Math.floor(Date.now() / 1000);
let body = Object.assign(
{
iss: toDid(signer),
iat: now,
nbf: now,
exp: now + 5 * 60,
version: DID_AUTH_PROTOCOL_VERSION,
},
payload || {}
);
// remove empty keys
body = Object.keys(body)
.filter(x => {
if (typeof body[x] === 'undefined' || body[x] == null || body[x] === '') {
return false;
}
return true;
})
const sign = (signer, sk, payload = {}, doSign = true) => {
if (isValid(signer) === false) {
throw new Error('Cannot do sign with invalid signer');
}
const type = toTypeInfo(signer);
const headers = {
[types.KeyType.SECP256K1]: {
alg: 'ES256K',
type: 'JWT',
},
[types.KeyType.ED25519]: {
alg: 'Ed25519',
type: 'JWT',
},
};
// make header
const sign = (signer, sk, payload = {}, doSign = true) => {
if (isValid(signer) === false) {
throw new Error('Cannot do sign with invalid signer');
}
const type = toTypeInfo(signer);
const headers = {
[types.KeyType.SECP256K1]: {
alg: 'ES256K',
type: 'JWT',
},
[types.KeyType.ED25519]: {
alg: 'Ed25519',
type: 'JWT',
},
};
// make header
const header = headers[type.pk];
const headerB64 = toBase64(stringify(header));
// make body
const decode = (token, payloadOnly = true) => {
const [headerB64, bodyB64, sigB64] = token.split('.');
const header = JSON.parse(fromBase64(headerB64));
const body = JSON.parse(fromBase64(bodyB64));
const sig = Buffer.from(fromBase64(sigB64)).toString('hex');
if (payloadOnly) {
return body;
}
return { header, body, signature: `0x${toStrictHex(sig)}` };
};
function toTetherAddress(hash) {
return fromHash(hash, types.RoleType.ROLE_TETHER);
}
function toSwapAddress(hash) {
return fromHash(hash, types.RoleType.ROLE_SWAP);
}