dcl-crypto
Advanced tools
Comparing version 2.1.0 to 2.2.0
import { EthereumProvider } from 'web3x/providers'; | ||
import { AuthIdentity, AuthChain, EthAddress, AuthLinkType, IdentityType, AuditInfo, AuthLink, Signature, ValidationResult } from './types'; | ||
import { AuthIdentity, AuthChain, EthAddress, AuthLinkType, IdentityType, AuthLink, Signature, ValidationResult } from './types'; | ||
export declare const VALID_SIGNATURE: string; | ||
@@ -14,3 +14,3 @@ export declare class Authenticator { | ||
static createSignature(identity: IdentityType, message: string): string; | ||
static ownerAddress(auditInfo: AuditInfo): EthAddress; | ||
static ownerAddress(authChain: AuthChain): EthAddress; | ||
static getEphemeralMessage(ephemeralAddress: string, expiration: Date): string; | ||
@@ -23,3 +23,3 @@ } | ||
declare type ValidationOptions = { | ||
dateToValidateExpirationInMillis?: number; | ||
dateToValidateExpirationInMillis: number; | ||
provider?: EthereumProvider; | ||
@@ -31,3 +31,5 @@ }; | ||
export declare const ECDSA_EIP_1654_EPHEMERAL_VALIDATOR: ValidatorType; | ||
export declare const EIP_1654_SIGNED_ENTITY_VALIDATOR: ValidatorType; | ||
export declare function getEphemeralSignatureType(signature: string): AuthLinkType; | ||
export declare function getSignedIdentitySignatureType(signature: string): AuthLinkType; | ||
export declare function parseEmphemeralPayload(payload: string): { | ||
@@ -34,0 +36,0 @@ message: string; |
@@ -14,2 +14,3 @@ "use strict"; | ||
exports.VALID_SIGNATURE = 'VALID_SIGNATURE'; | ||
const PERSONAL_SIGNATURE_LENGTH = 132; | ||
class Authenticator { | ||
@@ -65,3 +66,3 @@ /** Validate that the signature belongs to the Ethereum address */ | ||
{ | ||
type: types_1.AuthLinkType.ECDSA_SIGNED_ENTITY, | ||
type: getSignedIdentitySignatureType(signature), | ||
payload: finalPayload, | ||
@@ -89,3 +90,3 @@ signature: signature | ||
{ | ||
type: types_1.AuthLinkType.ECDSA_SIGNED_ENTITY, | ||
type: types_1.AuthLinkType.ECDSA_PERSONAL_SIGNED_ENTITY, | ||
payload: entityId, | ||
@@ -121,3 +122,3 @@ signature: secondSignature | ||
{ | ||
type: types_1.AuthLinkType.ECDSA_SIGNED_ENTITY, | ||
type: types_1.AuthLinkType.ECDSA_PERSONAL_SIGNED_ENTITY, | ||
payload: entityId, | ||
@@ -131,6 +132,6 @@ signature: secondSignature | ||
} | ||
static ownerAddress(auditInfo) { | ||
if (auditInfo.authChain.length > 0) { | ||
if (auditInfo.authChain[0].type === types_1.AuthLinkType.SIGNER) { | ||
return auditInfo.authChain[0].payload; | ||
static ownerAddress(authChain) { | ||
if (authChain.length > 0) { | ||
if (authChain[0].type === types_1.AuthLinkType.SIGNER) { | ||
return authChain[0].payload; | ||
} | ||
@@ -176,39 +177,17 @@ } | ||
var _a, _b; | ||
// bytes4(keccak256("isValidSignature(bytes32,bytes)") | ||
const ERC1271_MAGIC_VALUE = '0x1626ba7e'; | ||
const provider = options.provider; | ||
if (!provider) { | ||
throw new Error('Missing provider'); | ||
} | ||
const eth = new eth_1.Eth(provider); | ||
const signatureValidator = new SignatureValidator_1.SignatureValidator(eth, address_1.Address.fromString(authority)); | ||
const { message, ephemeralAddress, expiration } = parseEmphemeralPayload(authLink.payload); | ||
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) { | ||
let result = await signatureValidator.methods | ||
.isValidSignature(Authenticator.createEIP1271MessageHash(message), authLink.signature) | ||
.call(); | ||
if (result === ERC1271_MAGIC_VALUE) { | ||
if (await isValidEIP1654Message(options.provider, authority, message, authLink.signature, dateToValidateExpirationInMillis)) { | ||
return { nextAuthority: ephemeralAddress }; | ||
} | ||
else { | ||
// check based on the dateToValidateExpirationInMillis | ||
const dater = new blocks_1.default(provider); | ||
try { | ||
const { block } = await dater.getDate(dateToValidateExpirationInMillis, false); | ||
result = await signatureValidator.methods | ||
.isValidSignature(Authenticator.createEIP1271MessageHash(message), authLink.signature) | ||
.call({}, block); | ||
} | ||
catch (e) { | ||
throw new Error(`Invalid validation. Error: ${e.message}`); | ||
} | ||
if (result === ERC1271_MAGIC_VALUE) { | ||
return { nextAuthority: ephemeralAddress }; | ||
} | ||
} | ||
throw new Error(`Invalid validation. Expected: ${ERC1271_MAGIC_VALUE}.Actual: ${result}`); | ||
} | ||
throw new Error(`Ephemeral key expired. Expiration: ${expiration}. Test: ${dateToValidateExpirationInMillis}`); | ||
}; | ||
exports.EIP_1654_SIGNED_ENTITY_VALIDATOR = async (authority, authLink, options) => { | ||
if (await isValidEIP1654Message(options.provider, authority, authLink.payload, authLink.signature, options.dateToValidateExpirationInMillis)) { | ||
return { nextAuthority: authLink.payload }; | ||
} | ||
throw new Error(`Invalid validation`); | ||
}; | ||
const ERROR_VALIDATOR = async (_, __) => { | ||
@@ -218,11 +197,19 @@ return { error: 'Error Validator.' }; | ||
function getEphemeralSignatureType(signature) { | ||
// ERC 1654 support https://github.com/ethereum/EIPs/issues/1654 | ||
if (signature.length > 150) { | ||
if (signature.length === PERSONAL_SIGNATURE_LENGTH) { | ||
return types_1.AuthLinkType.ECDSA_PERSONAL_EPHEMERAL; | ||
} | ||
else { | ||
return types_1.AuthLinkType.ECDSA_EIP_1654_EPHEMERAL; | ||
} | ||
} | ||
exports.getEphemeralSignatureType = getEphemeralSignatureType; | ||
function getSignedIdentitySignatureType(signature) { | ||
if (signature.length === PERSONAL_SIGNATURE_LENGTH) { | ||
return types_1.AuthLinkType.ECDSA_PERSONAL_SIGNED_ENTITY; | ||
} | ||
else { | ||
return types_1.AuthLinkType.ECDSA_PERSONAL_EPHEMERAL; | ||
return types_1.AuthLinkType.ECDSA_EIP_1654_SIGNED_ENTITY; | ||
} | ||
} | ||
exports.getEphemeralSignatureType = getEphemeralSignatureType; | ||
exports.getSignedIdentitySignatureType = getSignedIdentitySignatureType; | ||
function parseEmphemeralPayload(payload) { | ||
@@ -248,2 +235,35 @@ // authLink payload structure: <human-readable message >\nEphemeral address: <ephemeral-eth - address >\nExpiration: <timestamp> | ||
} | ||
async function isValidEIP1654Message(provider, contractAddress, message, signature, dateToValidateExpirationInMillis) { | ||
// bytes4(keccak256("isValidSignature(bytes32,bytes)") | ||
const ERC1654_MAGIC_VALUE = '0x1626ba7e'; | ||
if (!provider) { | ||
throw new Error('Missing provider'); | ||
} | ||
const eth = new eth_1.Eth(provider); | ||
const signatureValidator = new SignatureValidator_1.SignatureValidator(eth, address_1.Address.fromString(contractAddress)); | ||
const hashedMessage = Authenticator.createEIP1271MessageHash(message); | ||
let result = await signatureValidator.methods | ||
.isValidSignature(hashedMessage, signature) | ||
.call(); | ||
if (result === ERC1654_MAGIC_VALUE) { | ||
return true; | ||
} | ||
else { | ||
// check based on the dateToValidateExpirationInMillis | ||
const dater = new blocks_1.default(provider); | ||
try { | ||
const { block } = await dater.getDate(dateToValidateExpirationInMillis, false); | ||
result = await signatureValidator.methods | ||
.isValidSignature(hashedMessage, signature) | ||
.call({}, block); | ||
} | ||
catch (e) { | ||
throw new Error(`Invalid validation. Error: ${e.message}`); | ||
} | ||
if (result === ERC1654_MAGIC_VALUE) { | ||
return true; | ||
} | ||
throw new Error(`Invalid validation. Expected: ${ERC1654_MAGIC_VALUE}. Actual: ${result}`); | ||
} | ||
} | ||
function getValidatorByType(type) { | ||
@@ -255,6 +275,8 @@ switch (type) { | ||
return exports.ECDSA_PERSONAL_EPHEMERAL_VALIDATOR; | ||
case types_1.AuthLinkType.ECDSA_SIGNED_ENTITY: | ||
case types_1.AuthLinkType.ECDSA_PERSONAL_SIGNED_ENTITY: | ||
return exports.ECDSA_SIGNED_ENTITY_VALIDATOR; | ||
case types_1.AuthLinkType.ECDSA_EIP_1654_EPHEMERAL: | ||
return exports.ECDSA_EIP_1654_EPHEMERAL_VALIDATOR; | ||
case types_1.AuthLinkType.ECDSA_EIP_1654_SIGNED_ENTITY: | ||
return exports.EIP_1654_SIGNED_ENTITY_VALIDATOR; | ||
default: | ||
@@ -261,0 +283,0 @@ return ERROR_VALIDATOR; |
@@ -17,24 +17,6 @@ export declare type Signature = string; | ||
ECDSA_PERSONAL_EPHEMERAL = "ECDSA_EPHEMERAL", | ||
ECDSA_PERSONAL_SIGNED_ENTITY = "ECDSA_SIGNED_ENTITY", | ||
ECDSA_EIP_1654_EPHEMERAL = "ECDSA_EIP_1654_EPHEMERAL", | ||
ECDSA_SIGNED_ENTITY = "ECDSA_SIGNED_ENTITY" | ||
ECDSA_EIP_1654_SIGNED_ENTITY = "ECDSA_EIP_1654_SIGNED_ENTITY" | ||
} | ||
export declare type AuditInfo = { | ||
version: EntityVersion; | ||
deployedTimestamp: Timestamp; | ||
authChain: AuthChain; | ||
overwrittenBy?: EntityId; | ||
isBlacklisted?: boolean; | ||
blacklistedContent?: ContentFileHash[]; | ||
originalMetadata?: { | ||
originalVersion: EntityVersion; | ||
data: any; | ||
}; | ||
}; | ||
export declare enum EntityVersion { | ||
V2 = "v2", | ||
V3 = "v3" | ||
} | ||
export declare type Timestamp = number; | ||
export declare type EntityId = ContentFileHash; | ||
export declare type ContentFileHash = string; | ||
export declare type AuthIdentity = { | ||
@@ -41,0 +23,0 @@ ephemeralIdentity: IdentityType; |
@@ -7,11 +7,7 @@ "use strict"; | ||
AuthLinkType["ECDSA_PERSONAL_EPHEMERAL"] = "ECDSA_EPHEMERAL"; | ||
AuthLinkType["ECDSA_PERSONAL_SIGNED_ENTITY"] = "ECDSA_SIGNED_ENTITY"; | ||
// https://github.com/ethereum/EIPs/issues/1654 | ||
AuthLinkType["ECDSA_EIP_1654_EPHEMERAL"] = "ECDSA_EIP_1654_EPHEMERAL"; | ||
AuthLinkType["ECDSA_SIGNED_ENTITY"] = "ECDSA_SIGNED_ENTITY"; | ||
AuthLinkType["ECDSA_EIP_1654_SIGNED_ENTITY"] = "ECDSA_EIP_1654_SIGNED_ENTITY"; | ||
})(AuthLinkType = exports.AuthLinkType || (exports.AuthLinkType = {})); | ||
var EntityVersion; | ||
(function (EntityVersion) { | ||
EntityVersion["V2"] = "v2"; | ||
EntityVersion["V3"] = "v3"; | ||
})(EntityVersion = exports.EntityVersion || (exports.EntityVersion = {})); | ||
//# sourceMappingURL=types.js.map |
{ | ||
"name": "dcl-crypto", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Crypto auth for Decentraland", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
44989
624