dcl-crypto
Advanced tools
Comparing version 1.8.0 to 1.9.0
@@ -5,3 +5,3 @@ import { EthereumProvider } from 'web3x/providers'; | ||
/** Validate that the signature belongs to the Ethereum address */ | ||
static validateSignature(expectedFinalAuthority: string, authChain: AuthChain, provider: EthereumProvider): Promise<boolean>; | ||
static validateSignature(expectedFinalAuthority: string, authChain: AuthChain, provider: EthereumProvider, dateToValidateExpirationInMillis?: number): Promise<boolean>; | ||
static createEthereumMessageHash(msg: string): string; | ||
@@ -17,6 +17,10 @@ static createEIP1271MessageHash(msg: string): string; | ||
} | ||
declare type ValidatorType = (authority: string, authLink: AuthLink, provider?: EthereumProvider) => Promise<{ | ||
declare type ValidatorType = (authority: string, authLink: AuthLink, options?: ValidationOptions) => Promise<{ | ||
error?: boolean; | ||
nextAuthority?: string; | ||
}>; | ||
declare type ValidationOptions = { | ||
dateToValidateExpirationInMillis?: number; | ||
provider?: EthereumProvider; | ||
}; | ||
export declare const SIGNER_VALIDATOR: ValidatorType; | ||
@@ -23,0 +27,0 @@ export declare const ECDSA_SIGNED_ENTITY_VALIDATOR: ValidatorType; |
@@ -8,9 +8,10 @@ "use strict"; | ||
const types_1 = require("./types"); | ||
const utils_1 = require("./helper/utils"); | ||
class Authenticator { | ||
/** Validate that the signature belongs to the Ethereum address */ | ||
static async validateSignature(expectedFinalAuthority, authChain, provider) { | ||
static async validateSignature(expectedFinalAuthority, authChain, provider, dateToValidateExpirationInMillis = Date.now()) { | ||
let currentAuthority = ''; | ||
for (let authLink of authChain) { | ||
const validator = getValidatorByType(authLink.type); | ||
const { error, nextAuthority } = await validator(currentAuthority, authLink, provider); | ||
const { error, nextAuthority } = await validator(currentAuthority, authLink, { provider, dateToValidateExpirationInMillis }); | ||
if (error) { | ||
@@ -52,4 +53,3 @@ return false; | ||
static createAuthChain(ownerIdentity, ephemeralIdentity, ephemeralMinutesDuration, entityId) { | ||
let expiration = new Date(); | ||
expiration.setMinutes(expiration.getMinutes() + ephemeralMinutesDuration); | ||
const expiration = utils_1.moveMinutes(ephemeralMinutesDuration); | ||
const ephemeralMessage = Authenticator.getEphemeralMessage(ephemeralIdentity.address, expiration); | ||
@@ -138,6 +138,8 @@ const firstSignature = Authenticator.createSignature(ownerIdentity, ephemeralMessage); | ||
}; | ||
exports.ECDSA_PERSONAL_EPHEMERAL_VALIDATOR = async (authority, authLink) => { | ||
exports.ECDSA_PERSONAL_EPHEMERAL_VALIDATOR = async (authority, authLink, options) => { | ||
var _a, _b; | ||
try { | ||
const { message, ephemeralAddress, expiration } = parseEmphemeralPayload(authLink.payload); | ||
if (expiration > Date.now()) { | ||
const dateToValidateExpirationInMillis = ((_a = options) === null || _a === void 0 ? void 0 : _a.dateToValidateExpirationInMillis) ? (_b = options) === null || _b === void 0 ? void 0 : _b.dateToValidateExpirationInMillis : Date.now(); | ||
if (expiration > dateToValidateExpirationInMillis) { | ||
const signerAddress = eth_crypto_1.recover(authLink.signature, Authenticator.createEthereumMessageHash(message)); | ||
@@ -154,6 +156,8 @@ if (authority.toLocaleLowerCase() === signerAddress.toLocaleLowerCase()) { | ||
}; | ||
exports.ECDSA_EIP_1654_EPHEMERAL_VALIDATOR = async (authority, authLink, provider) => { | ||
exports.ECDSA_EIP_1654_EPHEMERAL_VALIDATOR = async (authority, authLink, options) => { | ||
var _a, _b; | ||
// bytes4(keccak256("isValidSignature(bytes32,bytes)") | ||
const ERC1271_MAGIC_VALUE = '0x1626ba7e'; | ||
try { | ||
const provider = options.provider; | ||
if (!provider) { | ||
@@ -165,3 +169,4 @@ throw new Error('Missing provider'); | ||
const { message, ephemeralAddress, expiration } = parseEmphemeralPayload(authLink.payload); | ||
if (expiration > Date.now()) { | ||
const dateToValidateExpirationInMillis = ((_a = options) === null || _a === void 0 ? void 0 : _a.dateToValidateExpirationInMillis) ? (_b = options) === null || _b === void 0 ? void 0 : _b.dateToValidateExpirationInMillis : Date.now(); | ||
if (expiration > dateToValidateExpirationInMillis) { | ||
const result = await signatureValidator.methods | ||
@@ -168,0 +173,0 @@ .isValidSignature(Authenticator.createEIP1271MessageHash(message), authLink.signature) |
{ | ||
"name": "dcl-crypto", | ||
"version": "1.8.0", | ||
"version": "1.9.0", | ||
"description": "Crypto auth for Decentraland", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -6,4 +6,4 @@ # DCL Auth Crypto Lib | ||
## Install | ||
## How to Install | ||
`npm i dcl-crypto` |
Sorry, the diff of this file is not supported yet
30496
23
433