Skip to content

Commit

Permalink
Simplify Transitioner (#470)
Browse files Browse the repository at this point in the history
* Simplifying UnipigTransitioner by making it accept a SignatureProvider instead of being a DefaultWallet
* Making UnipigTransitioner only act on behalf of one Wallet instead of potentially many
* Changing SignatureProvider interface to provide address access
* Changing DefaultWallet to not be a SignatureProvider but instead have many SignatureProviders
* Updating tests
  • Loading branch information
willmeister committed Sep 26, 2019
1 parent 42f632c commit 3239a0e
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 150 deletions.
5 changes: 1 addition & 4 deletions packages/core/src/app/aggregator/aggregator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ export class DefaultAggregator implements Aggregator {

return {
blockTransaction,
witness: await this.signatureProvider.sign(
this.publicKey,
serializedTransaction
),
witness: await this.signatureProvider.sign(serializedTransaction),
}
}

Expand Down
16 changes: 7 additions & 9 deletions packages/core/src/app/keystore/signatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class DefaultSignatureProvider implements SignatureProvider {
private readonly wallet: ethers.Wallet = ethers.Wallet.createRandom()
) {}

public async sign(_address: string, message: string): Promise<string> {
public async sign(message: string): Promise<string> {
return this.wallet.signMessage(message)
}

Expand All @@ -31,16 +31,14 @@ export class DefaultSignatureProvider implements SignatureProvider {
}

export class IdentitySigner implements SignatureProvider {
private static _instance: SignatureProvider
public static instance(): SignatureProvider {
if (!IdentitySigner._instance) {
IdentitySigner._instance = new IdentitySigner()
}
return IdentitySigner._instance
public constructor(private address: string) {}

public async getAddress(): Promise<string> {
return this.address
}

public async sign(address: string, message: string): Promise<string> {
return address
public async sign(message: string): Promise<string> {
return this.address
}
}

Expand Down
17 changes: 16 additions & 1 deletion packages/core/src/app/keystore/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import { ethers } from 'ethers'

/* Internal Imports */
import { Wallet, WalletDB } from '../../types'
import { SignatureProvider, Wallet, WalletDB } from '../../types'
import { DefaultSignatureProvider } from './signatures'

/**
* Simple Wallet implementation.
Expand Down Expand Up @@ -71,6 +72,20 @@ export class DefaultWallet implements Wallet {
delete this.unlocked[address]
}

public async getSignatureProvider(
address: string
): Promise<SignatureProvider> {
if (!(await this.walletdb.hasKeystore(address))) {
throw new Error('Account does not exist.')
}

if (!(address in this.unlocked)) {
throw new Error('Account is not unlocked.')
}

return new DefaultSignatureProvider(this.unlocked[address])
}

/**
* Signs a message.
* @param address Address to sign the message from.
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/app/ovm/examples/state-channel-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ export class StateChannelClient {
)

const signature: string = await this.signatureProvider.sign(
this.myAddress,
serializedMessage
)

Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/types/keystore/signatures.interface.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
export interface SignatureProvider {
/**
* Gets the public key for this signature provider.
* @returns the public key
*/
getAddress(): Promise<string>

/**
* Signs the provided message with the private key associated with the provided address.
*
* @param address The address
* @param message The message
* @returns the signature
*/
sign(address: string, message: string): Promise<string>
sign(message: string): Promise<string>
}

export interface SignatureVerifier {
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/types/keystore/wallet.interface.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { SignatureProvider } from './signatures.interface'

export interface Wallet extends SignatureProvider {
export interface Wallet {
listAccounts(): Promise<string[]>
createAccount(password: string): Promise<string>
unlockAccount(address: string, password: string): Promise<void>
lockAccount(address: string): Promise<void>
getSignatureProvider(address: string): Promise<SignatureProvider>
sign(address: string, message: string): Promise<string>
}
7 changes: 2 additions & 5 deletions packages/core/test/app/aggregator/aggregator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ class DummyStateManager implements StateManager {
*********/

describe('DefaultAggregator', () => {
const wallet: ethers.Wallet = ethers.Wallet.createRandom()

let blockManager: DummyBlockManager
let stateManager: DummyStateManager
let aggregator: Aggregator
Expand All @@ -107,8 +105,8 @@ describe('DefaultAggregator', () => {
beforeEach(async () => {
blockManager = new DummyBlockManager()
stateManager = new DummyStateManager()
signatureProvider = new DefaultSignatureProvider(wallet)
aggregatorAddress = await wallet.getAddress()
signatureProvider = new DefaultSignatureProvider()
aggregatorAddress = await signatureProvider.getAddress()
aggregator = new DefaultAggregator(
stateManager,
blockManager,
Expand Down Expand Up @@ -151,7 +149,6 @@ describe('DefaultAggregator', () => {
txCommitment.blockTransaction
)
const signature: string = await signatureProvider.sign(
aggregatorAddress,
serializedCommitment
)
assert(
Expand Down
17 changes: 5 additions & 12 deletions packages/example-rollup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
SignedByDecider,
SimpleClient,
getLogger,
DefaultSignatureProvider,
BaseDB,
} from '@pigi/core'
import {
Expand Down Expand Up @@ -119,9 +120,7 @@ async function initialize() {
transitionerDB,
rollupStateSolver,
rollupClient,
undefined,
undefined,
wallet
new DefaultSignatureProvider(wallet)
)
// Update account address
updateAccountAddress(wallet.address)
Expand All @@ -138,7 +137,7 @@ async function fetchBalanceUpdate() {
}

async function onRequestFundsClicked() {
await unipigWallet.requestFaucetFunds(wallet.address, 10)
await unipigWallet.requestFaucetFunds(10)
const updatedBalances: Balances = await unipigWallet.getBalances(
wallet.address
)
Expand All @@ -151,7 +150,7 @@ async function onTransferFundsClicked() {
const amount = parseInt(document.getElementById('send-amount').value, 10)
const recipient = document.getElementById('send-recipient').value

await unipigWallet.send(tokenType, wallet.address, recipient, amount)
await unipigWallet.send(tokenType, recipient, amount)
const updatedBalances: Balances = await unipigWallet.getBalances(
wallet.address
)
Expand All @@ -163,13 +162,7 @@ async function onSwapFundsClicked() {
const selectedIndex = document.getElementById('swap-token-type').selectedIndex
const tokenType = selectedIndex === 0 ? UNI_TOKEN_TYPE : PIGI_TOKEN_TYPE
const inputAmount = parseInt(document.getElementById('swap-amount').value, 10)
await unipigWallet.swap(
tokenType,
wallet.address,
inputAmount,
0,
+new Date() + 1000
)
await unipigWallet.swap(tokenType, inputAmount, 0, +new Date() + 1000)
const [senderBalance, uniswapBalance] = await Promise.all([
unipigWallet.getBalances(wallet.address),
unipigWallet.getBalances(UNISWAP_ADDRESS),
Expand Down
15 changes: 3 additions & 12 deletions packages/wallet/src/rollup-aggregator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
DB,
getLogger,
hexStrToBuf,
SignatureProvider,
} from '@pigi/core'

/* Internal Imports */
Expand All @@ -22,7 +23,6 @@ import {
generateTransferTx,
AGGREGATOR_API,
RollupTransaction,
SignatureProvider,
UNISWAP_ADDRESS,
AGGREGATOR_ADDRESS,
RollupTransition,
Expand Down Expand Up @@ -70,17 +70,11 @@ const generateFaucetTxs = async (

return [
{
signature: await signatureProvider.sign(
aggregatorAddress,
abiEncodeTransaction(txOne)
),
signature: await signatureProvider.sign(abiEncodeTransaction(txOne)),
transaction: txOne,
},
{
signature: await signatureProvider.sign(
aggregatorAddress,
abiEncodeTransaction(txTwo)
),
signature: await signatureProvider.sign(abiEncodeTransaction(txTwo)),
transaction: txTwo,
},
]
Expand Down Expand Up @@ -173,7 +167,6 @@ export class RollupAggregator extends SimpleServer {
let signature: Signature
if (!!stateReceipt.state) {
signature = await this.signatureProvider.sign(
AGGREGATOR_ADDRESS,
abiEncodeStateReceipt(stateReceipt)
)
} else {
Expand Down Expand Up @@ -316,7 +309,6 @@ export class RollupAggregator extends SimpleServer {
transitionIndex,
}
const senderSignature: string = await this.signatureProvider.sign(
AGGREGATOR_ADDRESS,
abiEncodeStateReceipt(senderReceipt)
)
receipts.push({
Expand All @@ -334,7 +326,6 @@ export class RollupAggregator extends SimpleServer {
transitionIndex,
}
const recipientSignature: string = await this.signatureProvider.sign(
AGGREGATOR_ADDRESS,
abiEncodeStateReceipt(recipientReceipt)
)
receipts.push({
Expand Down
6 changes: 1 addition & 5 deletions packages/wallet/src/rollup-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ export class RollupClient {
signatureProvider: SignatureProvider
): Promise<SignedStateReceipt[]> {
const signature = await signatureProvider.sign(
transaction.sender,
abiEncodeTransaction(transaction)
)
const receipts: SignedStateReceipt[] = await this.rpcClient.handle<
Expand All @@ -93,10 +92,7 @@ export class RollupClient {
transaction: RollupTransaction,
signatureProvider: SignatureProvider
): Promise<SignedStateReceipt> {
const signature = await signatureProvider.sign(
transaction.sender,
serializeObject(transaction)
)
const signature = await signatureProvider.sign(serializeObject(transaction))
const receipt: SignedStateReceipt = await this.rpcClient.handle<
SignedStateReceipt
>(AGGREGATOR_API.requestFaucetFunds, {
Expand Down
4 changes: 0 additions & 4 deletions packages/wallet/src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ export interface State {
balances: Balances
}

export interface SignatureProvider {
sign(address: string, message: string): Promise<string>
}

export interface StateUpdate {
transaction: SignedTransaction
stateRoot: string
Expand Down

0 comments on commit 3239a0e

Please sign in to comment.