Skip to content

Commit

Permalink
export signFake helper (#379)
Browse files Browse the repository at this point in the history
* export sign fake

* Update packages/chopsticks/src/utils/signFake.ts

Co-authored-by: Xiliang Chen <xlchen1291@gmail.com>

* fix

* fix lint

---------

Co-authored-by: Xiliang Chen <xlchen1291@gmail.com>
  • Loading branch information
shunjizhan and xlc committed Aug 24, 2023
1 parent 0d0ad4e commit 5f950d8
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
2 changes: 2 additions & 0 deletions packages/chopsticks/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import '@polkadot/api-augment'

export * from '@acala-network/chopsticks-core'
export { setupWithServer } from './setup-with-server'
export { fetchConfig } from './schema'
5 changes: 5 additions & 0 deletions packages/chopsticks/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './decoder'
export * from './generate-html-diff'
export * from './open-html'
export * from './override'
export * from './signFake'
33 changes: 33 additions & 0 deletions packages/chopsticks/src/utils/signFake.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ApiPromise } from '@polkadot/api'
import { GenericExtrinsic } from '@polkadot/types'
import { SignatureOptions } from '@polkadot/types/types'

export type SignFakeOptions = Partial<SignatureOptions>

export const signFakeWithApi = async (
api: ApiPromise,
tx: GenericExtrinsic,
addr: string,
options: SignFakeOptions = {},
) => {
const nonce = options.nonce ?? (await api.query.system.account(addr)).nonce

return signFake(tx, addr, {
nonce,
genesisHash: api.genesisHash,
runtimeVersion: api.runtimeVersion,
blockHash: api.genesisHash,
...options,
})
}

export const signFake = (tx: GenericExtrinsic, addr: string, options: SignatureOptions) => {
const mockSignature = new Uint8Array(64)
mockSignature.fill(0xcd)
mockSignature.set([0xde, 0xad, 0xbe, 0xef])
tx.signFake(addr, options)

tx.signature.set(mockSignature)

return tx
}
25 changes: 18 additions & 7 deletions packages/e2e/src/mock-signature.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, it } from 'vitest'
import { signFake, signFakeWithApi } from '@acala-network/chopsticks/utils'

import { api, dev, env, setupApi, testingPairs } from './helper'

Expand Down Expand Up @@ -39,7 +40,22 @@ describe('mock signature', () => {
await expect(tx.send()).rejects.toThrow('1010: {"invalid":{"badProof":null}}')
})

it('accept mock signature', async () => {
it('accept mock signature (with api)', async () => {
const { alice, bob } = testingPairs()
await dev.setStorage({
System: {
Account: [[[alice.address], { providers: 1, data: { free: 1000 * 1e12 } }]],
},
})

const tx = api.tx.balances.transfer(bob.address, 100)

await signFakeWithApi(api, tx, alice.address)

await expect(tx.send()).resolves.toBeTruthy()
})

it('accept mock signature (manually input options)', async () => {
const { alice, bob } = testingPairs()
await dev.setStorage({
System: {
Expand All @@ -50,18 +66,13 @@ describe('mock signature', () => {
const { nonce } = await api.query.system.account(alice.address)
const tx = api.tx.balances.transfer(bob.address, 100)

tx.signFake(alice.address, {
signFake(tx, alice.address, {
nonce,
genesisHash: api.genesisHash,
runtimeVersion: api.runtimeVersion,
blockHash: api.genesisHash,
})

const mockSignature = new Uint8Array(64)
mockSignature.fill(0xcd)
mockSignature.set([0xde, 0xad, 0xbe, 0xef])
tx.signature.set(mockSignature)

await expect(tx.send()).resolves.toBeTruthy()
})
})

0 comments on commit 5f950d8

Please sign in to comment.