Skip to content

Commit

Permalink
Added util function getSovereignAccountAddresses (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekenigs authored Apr 4, 2024
1 parent a3f14bf commit da06ea5
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/tender-apes-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@moonbeam-network/xcm-utils': patch
---

Added util function getSovereignAccountAddresses
3 changes: 2 additions & 1 deletion package-lock.json

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

3 changes: 2 additions & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
},
"peerDependencies": {
"@polkadot/api": "^10.10.1",
"@polkadot/apps-config": "^0.132.1"
"@polkadot/apps-config": "^0.132.1",
"@polkadot/util": "^12.5.1"
}
}
1 change: 1 addition & 0 deletions packages/utils/src/polkadot/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './polkadot.api';
export * from './polkadot.address';
25 changes: 25 additions & 0 deletions packages/utils/src/polkadot/polkadot.address.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { getSovereignAccountAddresses } from './polkadot.address';

describe('utils - polkadot address', () => {
describe('getSovereignAccountAddresses', () => {
it('should get correct addresses for paraId 1000', async () => {
expect(getSovereignAccountAddresses(1000)).toStrictEqual({
generic:
'0x7369626ce8030000000000000000000000000000000000000000000000000000',
moonbeam: '0x7369626ce8030000000000000000000000000000',
relay:
'0x70617261e8030000000000000000000000000000000000000000000000000000',
});
});

it('should get correct addresses for paraId 3019', async () => {
expect(getSovereignAccountAddresses(3019)).toStrictEqual({
generic:
'0x7369626ccb0b0000000000000000000000000000000000000000000000000000',
moonbeam: '0x7369626ccb0b0000000000000000000000000000',
relay:
'0x70617261cb0b0000000000000000000000000000000000000000000000000000',
});
});
});
});
18 changes: 18 additions & 0 deletions packages/utils/src/polkadot/polkadot.address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { bnToU8a, u8aToHex, stringToU8a } from '@polkadot/util';

export function getSovereignAccountAddresses(paraId: number) {
const paraIdU8a = bnToU8a(paraId, { bitLength: 32 });
const relay = u8aToHex(
new Uint8Array([...stringToU8a('para'), ...paraIdU8a]),
).padEnd(66, '0');
const generic = u8aToHex(
new Uint8Array([...stringToU8a('sibl'), ...paraIdU8a]),
).padEnd(66, '0');
const moonbeam = generic.slice(0, 42);

return {
generic,
moonbeam,
relay,
};
}

0 comments on commit da06ea5

Please sign in to comment.