Skip to content
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

feat: support contracts wallet signatures eip1654 #1

Merged
merged 4 commits into from
Feb 4, 2020
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
9 changes: 9 additions & 0 deletions contracts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"outputPath": "./src/contracts",
"contracts": {
"SignatureValidator": {
"source": "files",
"abiFile": "./src/contracts/artifacts/SignatureValidator.json"
}
}
}
2,311 changes: 2,105 additions & 206 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@
},
"homepage": "https://github.com/decentraland/decentraland-crypto#readme",
"dependencies": {
"eth-crypto": "^1.5.1"
"eth-crypto": "^1.5.1",
"web3x": "^4.0.6"
},
"devDependencies": {
"@types/chai": "^4.2.8",
"@types/mocha": "^5.2.7",
"@types/sinon": "^7.5.1",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"dcl-tslint-config-standard": "^1.0.1",
Expand All @@ -53,17 +56,19 @@
"nyc": "^14.1.1",
"prettier": "^1.10.2",
"semantic-release": "^15.9.16",
"sinon": "^8.1.1",
"ts-node": "^7.0.1",
"tslint": "^5.7.0",
"tslint-config-prettier": "^1.10.0",
"tslint-language-service": "^0.9.9",
"tslint-plugin-prettier": "^1.3.0",
"typescript": "^3.7.5",
"validate-commit-msg": "^2.14.0"
"validate-commit-msg": "^2.14.0",
"web3x-codegen": "^4.0.6"
},
"prettier": {
"printWidth": 80,
"singleQuote": true,
"semi": false
}
}
}
107 changes: 95 additions & 12 deletions src/Authenticator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { hash, sign, recover } from 'eth-crypto'
import { Eth } from 'web3x/eth'
import { Address } from 'web3x/address'
import { EthereumProvider } from 'web3x/providers'

import { SignatureValidator } from './contracts/SignatureValidator'
import {
AuthIdentity,
AuthChain,
Expand All @@ -14,13 +19,18 @@ export class Authenticator {
/** Validate that the signature belongs to the Ethereum address */
static async validateSignature(
expectedFinalAuthority: string,
authChain: AuthChain
authChain: AuthChain,
provider: any
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be an EthereumProvider?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

): Promise<boolean> {
let currentAuthority: string = ''

for (let authLink of authChain) {
const validator: ValidatorType = getValidatorByType(authLink.type)
const { error, nextAuthority } = validator(currentAuthority, authLink)
const { error, nextAuthority } = await validator(
currentAuthority,
authLink,
provider
)
if (error) {
return false
}
Expand All @@ -36,6 +46,16 @@ export class Authenticator {
return msgHash
}

// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1271.md
static createEIP1271MessageHash(msg: string) {
return hash.keccak256([
{
type: 'string',
value: msg
}
])
}

static createSimpleAuthChain(
finalPayload: string,
ownerAddress: EthAddress,
Expand Down Expand Up @@ -81,7 +101,7 @@ export class Authenticator {
signature: ''
},
{
type: AuthLinkType.ECDSA_EPHEMERAL,
type: AuthLinkType.ECDSA_PERSONAL_EPHEMERAL,
payload: ephemeralMessage,
signature: firstSignature
},
Expand Down Expand Up @@ -110,7 +130,7 @@ export class Authenticator {
const authChain: AuthChain = [
{ type: AuthLinkType.SIGNER, payload: ethAddress, signature: '' },
{
type: AuthLinkType.ECDSA_EPHEMERAL,
type: getEphemeralSignatureType(firstSignature),
payload: ephemeralMessage,
signature: firstSignature
}
Expand Down Expand Up @@ -157,14 +177,18 @@ export class Authenticator {

type ValidatorType = (
authority: string,
authLink: AuthLink
) => { error?: boolean; nextAuthority?: string }
authLink: AuthLink,
provider?: EthereumProvider
) => Promise<{ error?: boolean; nextAuthority?: string }>

const SIGNER_VALIDATOR: ValidatorType = (_: string, authLink: AuthLink) => {
export const SIGNER_VALIDATOR: ValidatorType = async (
_: string,
authLink: AuthLink
) => {
return { nextAuthority: authLink.payload }
}

const ECDSA_SIGNED_ENTITY_VALIDATOR: ValidatorType = (
export const ECDSA_SIGNED_ENTITY_VALIDATOR: ValidatorType = async (
authority: string,
authLink: AuthLink
) => {
Expand All @@ -182,7 +206,7 @@ const ECDSA_SIGNED_ENTITY_VALIDATOR: ValidatorType = (
return { error: true }
}

const ECDSA_EPHEMERAL_VALIDATOR: ValidatorType = (
export const ECDSA_PERSONAL_EPHEMERAL_VALIDATOR: ValidatorType = async (
authority: string,
authLink: AuthLink
) => {
Expand Down Expand Up @@ -213,18 +237,77 @@ const ECDSA_EPHEMERAL_VALIDATOR: ValidatorType = (
return { error: true }
}

const ERROR_VALIDATOR: ValidatorType = (_: string, __: AuthLink) => {
export const ECDSA_EIP_1654_EPHEMERAL_VALIDATOR: ValidatorType = async (
authority: string,
authLink: AuthLink,
provider: EthereumProvider
) => {
// bytes4(keccak256("isValidSignature(bytes32,bytes)")
const ERC1271_MAGIC_VALUE = '0x1626ba7e'

try {
if (!provider) {
throw new Error('Missing provider')
}

const eth = new Eth(provider)
const signatureValidator = new SignatureValidator(
eth,
Address.fromString(authority)
)

// authLink payload structure: <human-readable message >\nEphemeral address: <ephemeral-eth - address >\nExpiration: <timestamp>
// authLink payload example: Decentraland Login\nEphemeral address: 0x123456\nExpiration: 2020 - 01 - 20T22: 57: 11.334Z
const payloadParts: string[] = authLink.payload.split('\n')
const ephemeralAddress: string = payloadParts[1].substring(
'Ephemeral address: '.length
)
const expirationString: string = payloadParts[2].substring(
'Expiration: '.length
)
const expiration = Date.parse(expirationString)

if (expiration > Date.now()) {
const result = await signatureValidator.methods
.isValidSignature(
Authenticator.createEIP1271MessageHash(authLink.payload),
authLink.signature
)
.call()

if (result === ERC1271_MAGIC_VALUE) {
return { nextAuthority: ephemeralAddress }
}
}
} catch (e) {
// console.error(e)
}
return { error: true }
}

const ERROR_VALIDATOR: ValidatorType = async (_: string, __: AuthLink) => {
return { error: true }
}

export function getEphemeralSignatureType(signature: string): AuthLinkType {
// ERC 1654 support https://github.com/ethereum/EIPs/issues/1654
if (signature.length > 150) {
return AuthLinkType.ECDSA_EIP_1654_EPHEMERAL
} else {
return AuthLinkType.ECDSA_PERSONAL_EPHEMERAL
}
}

function getValidatorByType(type: AuthLinkType): ValidatorType {
switch (type) {
case AuthLinkType.SIGNER:
return SIGNER_VALIDATOR
case AuthLinkType.ECDSA_EPHEMERAL:
return ECDSA_EPHEMERAL_VALIDATOR
case AuthLinkType.ECDSA_PERSONAL_EPHEMERAL:
return ECDSA_PERSONAL_EPHEMERAL_VALIDATOR
case AuthLinkType.ECDSA_SIGNED_ENTITY:
return ECDSA_SIGNED_ENTITY_VALIDATOR
case AuthLinkType.ECDSA_EIP_1654_EPHEMERAL:
return ECDSA_EIP_1654_EPHEMERAL_VALIDATOR
default:
return ERROR_VALIDATOR
}
Expand Down
24 changes: 24 additions & 0 deletions src/contracts/SignatureValidator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Address } from 'web3x/address';
import { TransactionReceipt } from 'web3x/formatters';
import { Contract, ContractOptions, TxCall } from 'web3x/contract';
import { Eth } from 'web3x/eth';
import abi from './SignatureValidatorAbi';
interface SignatureValidatorEvents { }
interface SignatureValidatorEventLogs { }
interface SignatureValidatorTxEventLogs { }
export interface SignatureValidatorTransactionReceipt
extends TransactionReceipt<SignatureValidatorTxEventLogs> { }
interface SignatureValidatorMethods {
isValidSignature(hash: string, _signature: string): TxCall<string>
}
export interface SignatureValidatorDefinition {
methods: SignatureValidatorMethods
events: SignatureValidatorEvents
eventLogs: SignatureValidatorEventLogs
}
export class SignatureValidator extends Contract<SignatureValidatorDefinition> {
constructor(eth: Eth, address?: Address, options?: ContractOptions) {
super(eth, abi, address, options)
}
}
export let SignatureValidatorAbi = abi
26 changes: 26 additions & 0 deletions src/contracts/SignatureValidatorAbi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ContractAbi} from 'web3x/contract';
export default new ContractAbi([
{
"constant": true,
"inputs": [
{
"name": "hash",
"type": "bytes32"
},
{
"name": "_signature",
"type": "bytes"
}
],
"name": "isValidSignature",
"outputs": [
{
"name": "magicValue",
"type": "bytes4"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
]);
25 changes: 25 additions & 0 deletions src/contracts/artifacts/SignatureValidator.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[
{
"constant": true,
"inputs": [
{
"name": "hash",
"type": "bytes32"
},
{
"name": "_signature",
"type": "bytes"
}
],
"name": "isValidSignature",
"outputs": [
{
"name": "magicValue",
"type": "bytes4"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
]
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export type AuthLink = {

export enum AuthLinkType {
SIGNER = 'SIGNER',
ECDSA_EPHEMERAL = 'ECDSA_EPHEMERAL',
ECDSA_PERSONAL_EPHEMERAL = 'ECDSA_EPHEMERAL',
// https://github.com/ethereum/EIPs/issues/1654
ECDSA_EIP_1654_EPHEMERAL = 'ECDSA_EIP_1654_EPHEMERAL',
ECDSA_SIGNED_ENTITY = 'ECDSA_SIGNED_ENTITY'
}

Expand Down
Loading