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

Implement signTypedData across all different wallets/signers #5311

Merged
merged 5 commits into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions packages/contractkit/src/wallets/signers/aws-hsm-signer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ensureLeading0x, trimLeading0x } from '@celo/utils/lib/address'
import { EIP712TypedData, generateTypedDataHash } from '@celo/utils/lib/sign-typed-data-utils'
import { KMS } from 'aws-sdk'
import { BigNumber } from 'bignumber.js'
import * as ethUtil from 'ethereumjs-util'
Expand Down Expand Up @@ -93,6 +94,17 @@ export default class AwsHsmSigner implements Signer {
}
}

async signTypedData(typedData: EIP712TypedData): Promise<Signature> {
const typedDataHashBuff = generateTypedDataHash(typedData)
const { v, r, s } = await this.sign(typedDataHashBuff)

return {
v: v + 27,
r,
s,
}
}

getNativeKey(): string {
return this.keyId
}
Expand Down
15 changes: 15 additions & 0 deletions packages/contractkit/src/wallets/signers/azure-hsm-signer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ensureLeading0x, trimLeading0x } from '@celo/base/lib/address'
import { EIP712TypedData, generateTypedDataHash } from '@celo/utils/lib/sign-typed-data-utils'
import * as ethUtil from 'ethereumjs-util'
import { AzureKeyVaultClient } from '../../utils/azure-key-vault-client'
import { getHashFromEncoded, RLPEncodedTx } from '../../utils/signing-utils'
Expand Down Expand Up @@ -53,6 +54,20 @@ export class AzureHSMSigner implements Signer {
}
}

async signTypedData(typedData: EIP712TypedData): Promise<{ v: number; r: Buffer; s: Buffer }> {
const dataBuff = generateTypedDataHash(typedData)
const signature = await AzureHSMSigner.keyVaultClient.signMessage(dataBuff, this.keyName)

// Recovery ID should be a byte prefix
// https://bitcoin.stackexchange.com/questions/38351/ecdsa-v-r-s-what-is-v
const sigV = signature.v + 27
return {
v: sigV,
r: signature.r,
s: signature.s,
}
}

getNativeKey(): string {
return this.keyName
}
Expand Down
14 changes: 14 additions & 0 deletions packages/contractkit/src/wallets/signers/local-signer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ensureLeading0x, trimLeading0x } from '@celo/base/lib/address'
import { Decrypt } from '@celo/utils/lib/ecies'
import { EIP712TypedData, generateTypedDataHash } from '@celo/utils/lib/sign-typed-data-utils'
// @ts-ignore-next-line
import { account as Account } from 'eth-lib'
import * as ethUtil from 'ethereumjs-util'
Expand Down Expand Up @@ -45,6 +46,19 @@ export class LocalSigner implements Signer {
}
}

async signTypedData(typedData: EIP712TypedData): Promise<{ v: number; r: Buffer; s: Buffer }> {
const dataBuff = generateTypedDataHash(typedData)
const trimmedKey = trimLeading0x(this.privateKey)
const pkBuffer = Buffer.from(trimmedKey, 'hex')

const sig = ethUtil.ecsign(dataBuff, pkBuffer)
return {
v: parseInt(sig.v, 10),
r: Buffer.from(sig.r),
s: Buffer.from(sig.s),
}
}

decrypt(ciphertext: Buffer) {
const decryptedPlaintext = Decrypt(
Buffer.from(trimLeading0x(this.privateKey), 'hex'),
Expand Down
13 changes: 13 additions & 0 deletions packages/contractkit/src/wallets/signers/rpc-signer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ensureLeading0x, normalizeAddressWith0x, trimLeading0x } from '@celo/base/lib/address'
import { EIP712TypedData } from '@celo/utils/lib/sign-typed-data-utils'
import BigNumber from 'bignumber.js'
import BN from 'bn.js'
import { EncodedTransaction, Tx } from 'web3-core'
Expand All @@ -25,6 +26,7 @@ enum RpcSignerEndpoint {
UnlockAccount = 'personal_unlockAccount',
SignTransaction = 'eth_signTransaction',
SignBytes = 'eth_sign',
SignTypedData = 'eth_signTypedData',
Decrypt = 'personal_decrypt',
}

Expand All @@ -34,6 +36,7 @@ type RpcSignerEndpointInputs = {
personal_unlockAccount: [string, string, number]
eth_signTransaction: [any] // RpcTx doesn't match Tx because of nonce as string instead of number
eth_sign: [string, string]
eth_signTypedData: [string, EIP712TypedData]
personal_decrypt: [string, string]
}

Expand All @@ -43,6 +46,7 @@ type RpcSignerEndpointResult = {
personal_unlockAccount: boolean
eth_signTransaction: EncodedTransaction
eth_sign: string
eth_signTypedData: string
personal_decrypt: string
}

Expand Down Expand Up @@ -96,6 +100,15 @@ export class RpcSigner implements Signer {
throw new Error('signTransaction unimplemented; use signRawTransaction')
}

async signTypedData(typedData: EIP712TypedData): Promise<{ v: number; r: Buffer; s: Buffer }> {
const result = await this.callAndCheckResponse(RpcSignerEndpoint.SignTypedData, [
this.account,
typedData,
])

return decodeSig(result)
}

async signPersonalMessage(data: string): Promise<{ v: number; r: Buffer; s: Buffer }> {
const result = await this.callAndCheckResponse(RpcSignerEndpoint.SignBytes, [
this.account,
Expand Down
2 changes: 2 additions & 0 deletions packages/contractkit/src/wallets/signers/signer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { EIP712TypedData } from '@celo/utils/lib/sign-typed-data-utils'
import { RLPEncodedTx } from '../../utils/signing-utils'

export interface Signer {
Expand All @@ -11,6 +12,7 @@ export interface Signer {
encodedTx: RLPEncodedTx
) => Promise<{ v: number; r: Buffer; s: Buffer }>
signPersonalMessage: (data: string) => Promise<{ v: number; r: Buffer; s: Buffer }>
signTypedData: (typedData: EIP712TypedData) => Promise<{ v: number; r: Buffer; s: Buffer }>
getNativeKey: () => string
decrypt: (ciphertext: Buffer) => Promise<Buffer>
}
7 changes: 2 additions & 5 deletions packages/contractkit/src/wallets/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isHexString, normalizeAddressWith0x } from '@celo/base/lib/address'
import { EIP712TypedData, generateTypedDataHash } from '@celo/utils/lib/sign-typed-data-utils'
import { EIP712TypedData } from '@celo/utils/lib/sign-typed-data-utils'
import * as ethUtil from 'ethereumjs-util'
import { EncodedTransaction, Tx } from 'web3-core'
import { Address } from '../base'
Expand Down Expand Up @@ -113,11 +113,8 @@ export abstract class WalletBase<TSigner extends Signer> implements ReadOnlyWall
throw Error('wallet@signTypedData: TypedData Missing')
}

const dataBuff = generateTypedDataHash(typedData)
const trimmedData = dataBuff.toString('hex')

const signer = this.getSigner(address)
const sig = await signer.signPersonalMessage(trimmedData)
const sig = await signer.signTypedData(typedData)

return ethUtil.toRpcSig(sig.v, sig.r, sig.s)
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading