-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Goal The goal of this PR is <!-- insert goal here --> Closes #2203 # Discussion - added e2e tests for more usecase for ethereum keys - Some clean up and refactoring - Added eslint rules to enforce correct usage # Checklist - [X] e2e Tests added?
- Loading branch information
Showing
19 changed files
with
765 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import '@frequency-chain/api-augment'; | ||
import assert from 'assert'; | ||
import { DOLLARS, createAndFundKeypair, createKeys } from '../scaffolding/helpers'; | ||
import { KeyringPair } from '@polkadot/keyring/types'; | ||
import { Extrinsic, ExtrinsicHelper } from '../scaffolding/extrinsicHelpers'; | ||
import { getFundingSource } from '../scaffolding/funding'; | ||
import { getUnifiedAddress } from '../scaffolding/ethereum'; | ||
|
||
const fundingSource: KeyringPair = getFundingSource('frequency-balance-ethereum'); | ||
|
||
describe('Balance transfer ethereum', function () { | ||
describe('setup', function () { | ||
let senderSr25519Keys: KeyringPair; | ||
let senderEthereumKeys: KeyringPair; | ||
let ethereumKeys: KeyringPair; | ||
let ethereumKeys2: KeyringPair; | ||
let sr25519Keys: KeyringPair; | ||
|
||
before(async function () { | ||
senderSr25519Keys = await createAndFundKeypair(fundingSource, 30n * DOLLARS); | ||
senderEthereumKeys = await createAndFundKeypair(fundingSource, 30n * DOLLARS, undefined, undefined, 'ethereum'); | ||
ethereumKeys = await createKeys('another-key-1', 'ethereum'); | ||
ethereumKeys2 = await createKeys('another-key-2', 'ethereum'); | ||
sr25519Keys = await createKeys('another-sr25519', 'sr25519'); | ||
}); | ||
|
||
it('should transfer from sr25519 to ethereum style key', async function () { | ||
const transferAmount = 10n * DOLLARS; | ||
const extrinsic = new Extrinsic( | ||
() => ExtrinsicHelper.api.tx.balances.transferKeepAlive(getUnifiedAddress(ethereumKeys), transferAmount), | ||
senderSr25519Keys, | ||
ExtrinsicHelper.api.events.balances.Transfer | ||
); | ||
const { target } = await extrinsic.signAndSend(); | ||
assert.notEqual(target, undefined, 'should have returned Transfer event'); | ||
const accountInfo = await ExtrinsicHelper.getAccountInfo(ethereumKeys); | ||
assert(accountInfo.data.free.toBigInt() >= transferAmount); | ||
}); | ||
|
||
it('should transfer from sr25519 to ethereum 20 byte address', async function () { | ||
const transferAmount = 10n * DOLLARS; | ||
const extrinsic = new Extrinsic( | ||
// this is using MultiAddress::Address20 type in Rust since addressRaw is 20 bytes ethereum address | ||
() => ExtrinsicHelper.api.tx.balances.transferKeepAlive(ethereumKeys2.addressRaw, transferAmount), | ||
senderSr25519Keys, | ||
ExtrinsicHelper.api.events.balances.Transfer | ||
); | ||
const { target } = await extrinsic.signAndSend(); | ||
assert.notEqual(target, undefined, 'should have returned Transfer event'); | ||
const accountInfo = await ExtrinsicHelper.getAccountInfo(ethereumKeys2); | ||
assert(accountInfo.data.free.toBigInt() >= transferAmount); | ||
}); | ||
|
||
it('should transfer from an ethereum key to sr25519 key', async function () { | ||
const transferAmount = 10n * DOLLARS; | ||
const extrinsic = new Extrinsic( | ||
() => ExtrinsicHelper.api.tx.balances.transferKeepAlive(getUnifiedAddress(sr25519Keys), transferAmount), | ||
senderEthereumKeys, | ||
ExtrinsicHelper.api.events.balances.Transfer | ||
); | ||
const { target } = await extrinsic.signAndSend(); | ||
assert.notEqual(target, undefined, 'should have returned Transfer event'); | ||
const accountInfo = await ExtrinsicHelper.getAccountInfo(sr25519Keys); | ||
assert(accountInfo.data.free.toBigInt() >= transferAmount); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import '@frequency-chain/api-augment'; | ||
import assert from 'assert'; | ||
import { | ||
createKeys, | ||
createAndFundKeypair, | ||
generateAddKeyPayload, | ||
CENTS, | ||
signPayload, | ||
MultiSignatureType, | ||
} from '../scaffolding/helpers'; | ||
import { KeyringPair } from '@polkadot/keyring/types'; | ||
import { AddKeyData, ExtrinsicHelper } from '../scaffolding/extrinsicHelpers'; | ||
import { u64 } from '@polkadot/types'; | ||
import { Codec } from '@polkadot/types/types'; | ||
import { getFundingSource } from '../scaffolding/funding'; | ||
import { getUnifiedPublicKey } from '../scaffolding/ethereum'; | ||
|
||
const maxU64 = 18_446_744_073_709_551_615n; | ||
const fundingSource = getFundingSource('msa-key-management-ethereum'); | ||
|
||
describe('MSA Key management Ethereum', function () { | ||
describe('addPublicKeyToMsa Ethereum', function () { | ||
let keys: KeyringPair; | ||
let msaId: u64; | ||
let secondaryKey: KeyringPair; | ||
const defaultPayload: AddKeyData = {}; | ||
let payload: AddKeyData; | ||
let ownerSig: MultiSignatureType; | ||
let newSig: MultiSignatureType; | ||
let badSig: MultiSignatureType; | ||
let addKeyData: Codec; | ||
|
||
before(async function () { | ||
// Setup an MSA with one key and a secondary funded key | ||
keys = await createAndFundKeypair(fundingSource, 5n * CENTS, undefined, undefined, 'ethereum'); | ||
const { target } = await ExtrinsicHelper.createMsa(keys).signAndSend(); | ||
assert.notEqual(target?.data.msaId, undefined, 'MSA Id not in expected event'); | ||
msaId = target!.data.msaId; | ||
|
||
secondaryKey = await createAndFundKeypair(fundingSource, 5n * CENTS, undefined, undefined, 'ethereum'); | ||
|
||
// Default payload making it easier to test `addPublicKeyToMsa` | ||
defaultPayload.msaId = msaId; | ||
defaultPayload.newPublicKey = getUnifiedPublicKey(secondaryKey); | ||
}); | ||
|
||
beforeEach(async function () { | ||
payload = await generateAddKeyPayload(defaultPayload); | ||
}); | ||
|
||
it('should fail to add public key if origin is not one of the signers of the payload (MsaOwnershipInvalidSignature) for a Ethereum key', async function () { | ||
const badKeys: KeyringPair = createKeys(); | ||
addKeyData = ExtrinsicHelper.api.registry.createType('PalletMsaAddKeyData', payload); | ||
newSig = signPayload(secondaryKey, addKeyData); | ||
badSig = signPayload(badKeys, addKeyData); | ||
const op = ExtrinsicHelper.addPublicKeyToMsa(keys, badSig, newSig, payload); | ||
await assert.rejects(op.fundAndSend(fundingSource), { | ||
name: 'MsaOwnershipInvalidSignature', | ||
}); | ||
}); | ||
|
||
it('should fail to add public key if origin does not own MSA (NotMsaOwner) for a Ethereum key', async function () { | ||
const newPayload = await generateAddKeyPayload({ | ||
...defaultPayload, | ||
msaId: new u64(ExtrinsicHelper.api.registry, maxU64), | ||
}); | ||
addKeyData = ExtrinsicHelper.api.registry.createType('PalletMsaAddKeyData', newPayload); | ||
ownerSig = signPayload(keys, addKeyData); | ||
newSig = signPayload(secondaryKey, addKeyData); | ||
const op = ExtrinsicHelper.addPublicKeyToMsa(keys, ownerSig, newSig, newPayload); | ||
await assert.rejects(op.fundAndSend(fundingSource), { | ||
name: 'NotMsaOwner', | ||
}); | ||
}); | ||
|
||
it('should successfully add a new public key to an existing MSA & disallow duplicate signed payload submission (SignatureAlreadySubmitted) for a Ethereum key', async function () { | ||
addKeyData = ExtrinsicHelper.api.registry.createType('PalletMsaAddKeyData', payload); | ||
|
||
ownerSig = signPayload(keys, addKeyData); | ||
newSig = signPayload(secondaryKey, addKeyData); | ||
const addPublicKeyOp = ExtrinsicHelper.addPublicKeyToMsa(keys, ownerSig, newSig, payload); | ||
|
||
const { target: publicKeyEvents } = await addPublicKeyOp.fundAndSend(fundingSource); | ||
|
||
assert.notEqual(publicKeyEvents, undefined, 'should have added public key'); | ||
|
||
await assert.rejects( | ||
addPublicKeyOp.fundAndSend(fundingSource), | ||
'should reject sending the same signed payload twice' | ||
); | ||
}); | ||
|
||
it('should fail if attempting to add the same key more than once (KeyAlreadyRegistered) for a Ethereum key', async function () { | ||
const addKeyData = ExtrinsicHelper.api.registry.createType('PalletMsaAddKeyData', payload); | ||
|
||
const ownerSig = signPayload(keys, addKeyData); | ||
const newSig = signPayload(secondaryKey, addKeyData); | ||
const addPublicKeyOp = ExtrinsicHelper.addPublicKeyToMsa(keys, ownerSig, newSig, payload); | ||
|
||
await assert.rejects(addPublicKeyOp.fundAndSend(fundingSource), { | ||
name: 'KeyAlreadyRegistered', | ||
}); | ||
}); | ||
|
||
it('should allow new keypair to act for/on MSA for a Ethereum key', async function () { | ||
const thirdKey = createKeys(); | ||
const newPayload = await generateAddKeyPayload({ | ||
...defaultPayload, | ||
newPublicKey: getUnifiedPublicKey(thirdKey), | ||
}); | ||
addKeyData = ExtrinsicHelper.api.registry.createType('PalletMsaAddKeyData', newPayload); | ||
ownerSig = signPayload(secondaryKey, addKeyData); | ||
newSig = signPayload(thirdKey, addKeyData); | ||
const op = ExtrinsicHelper.addPublicKeyToMsa(secondaryKey, ownerSig, newSig, newPayload); | ||
const { target: event } = await op.fundAndSend(fundingSource); | ||
assert.notEqual(event, undefined, 'should have added public key'); | ||
|
||
// Cleanup | ||
await assert.doesNotReject(ExtrinsicHelper.deletePublicKey(keys, getUnifiedPublicKey(thirdKey)).signAndSend()); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.