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

Loom provider with optional private key #307

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 10 additions & 2 deletions src/loom-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class LoomProvider {
*/
constructor(
client: Client,
privateKey: Uint8Array,
privateKey?: Uint8Array,
setupMiddlewaresFunction?: SetupMiddlewareFunction
) {
this._client = client
Expand All @@ -171,7 +171,10 @@ export class LoomProvider {

this.addDefaultMethods()
this.addDefaultEvents()
this.addAccounts([privateKey])

if (privateKey) {
this.addAccounts([privateKey])
}
}

/**
Expand Down Expand Up @@ -892,6 +895,11 @@ export class LoomProvider {
txTransaction: Transaction
): Promise<Uint8Array | void> {
const middleware = this._accountMiddlewares.get(fromPublicAddr)

if (!middleware) {
throw Error(`Middleware not found for public address ${fromPublicAddr}`)
}

return this._client.commitTxAsync<Transaction>(txTransaction, { middleware })
}

Expand Down
50 changes: 47 additions & 3 deletions src/tests/e2e/loom-provider-web3-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ const newContractAndClient = async () => {
}
]

const result = await deployContract(loomProvider, contractData)
const { contractAddress } = await deployContract(loomProvider, contractData)

const contract = new web3.eth.Contract(ABI, result.contractAddress, { from })
const contract = new web3.eth.Contract(ABI, contractAddress, { from })

return { contract, client, web3, from, privKey }
return { contract, client, web3, from, privKey, ABI, contractAddress }
}

test('LoomProvider + Web3 + Event with not matching topic', async t => {
Expand Down Expand Up @@ -118,6 +118,50 @@ test('LoomProvider + Web3 + Event with not matching topic', async t => {
}
})

test('LoomProvider transaction without private key should fail', async t => {
t.plan(1)
const { client, ABI, contractAddress } = await newContractAndClient()
const loomProvider = new LoomProvider(client)
const web3 = new Web3(loomProvider)
const contract = new web3.eth.Contract(ABI, contractAddress)

try {
const newValue = 1
await contract.methods.get(newValue).send()
await waitForMillisecondsAsync(1000)
} catch (err) {
t.assert(err, 'Should fail on try to transaction without a private key set')
}

if (client) {
client.disconnect()
}
})

test('LoomProvider static call without private key should not fail', async t => {
t.plan(1)

const { client, ABI, contractAddress } = await newContractAndClient()
const loomProvider = new LoomProvider(client)
const web3 = new Web3(loomProvider)
const contract = new web3.eth.Contract(ABI, contractAddress)

// Some dummy address needed or the call will fail
const from = '0x0000000000000000000000000000000000000000'

try {
const resultOfGet = await contract.methods.get().call({ from })
t.equal(+resultOfGet, 10, `SimpleStore.get should return correct value`)
await waitForMillisecondsAsync(1000)
} catch (err) {
t.fail(err)
}

if (client) {
client.disconnect()
}
})

test('LoomProvider + Web3 + Multiple event topics', async t => {
t.plan(3)
const { contract, client } = await newContractAndClient()
Expand Down