Skip to content

Commit 99c68a2

Browse files
committed
Add MagicSigner to inject into polkadot api instance
1 parent d39ada1 commit 99c68a2

File tree

4 files changed

+672
-53
lines changed

4 files changed

+672
-53
lines changed

packages/@magic-ext/polkadot/package.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,24 @@
1818
"types": "./dist/types/index.d.ts",
1919
"jsdelivr": "./dist/extension.js",
2020
"exports": {
21+
"types": "./dist/types/index.d.ts",
2122
"import": "./dist/es/index.mjs",
2223
"require": "./dist/cjs/index.js"
2324
},
2425
"externals": {
2526
"include": [
26-
"@magic-sdk/commons"
27+
"@magic-sdk/provider"
2728
]
2829
},
2930
"devDependencies": {
30-
"@magic-sdk/commons": "^17.2.0"
31+
"@magic-sdk/provider": "^21.2.0",
32+
"@polkadot/types": "^10.10.1",
33+
"@polkadot/util": "^12.5.1"
34+
},
35+
"dependencies": {
36+
"@polkadot/api": "^10.10.1"
37+
},
38+
"peerDependencies": {
39+
"@polkadot/api": "^10.10.1"
3140
}
3241
}

packages/@magic-ext/polkadot/src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { Extension } from '@magic-sdk/commons';
2-
import { PolkadotConfig, ConfigType } from './type';
1+
import { Extension } from '@magic-sdk/provider';
2+
import type { ConfigType, PolkadotConfig } from './type';
3+
4+
export { MagicSigner } from './magic-signer';
35

46
export class PolkadotExtension extends Extension.Internal<'polkadot', PolkadotConfig> {
57
name = 'polkadot' as const;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import type { Signer } from '@polkadot/api/types';
2+
import type { ISubmittableResult, SignerPayloadJSON, SignerPayloadRaw } from '@polkadot/types/types';
3+
import type { HexString } from '@polkadot/util/types';
4+
import type { H256 } from '@polkadot/types/interfaces';
5+
import { Extension, InstanceWithExtensions, SDKBase } from '@magic-sdk/provider';
6+
import { PolkadotExtension } from '.';
7+
8+
type MagicInstnace = InstanceWithExtensions<SDKBase, [PolkadotExtension, ...Extension[]]>;
9+
10+
export class MagicSigner implements Signer {
11+
private magic: MagicInstnace;
12+
private nextId = 0;
13+
14+
constructor(magic: MagicInstnace) {
15+
this.magic = magic;
16+
}
17+
18+
/**
19+
* @description signs an extrinsic payload from a serialized form
20+
*/
21+
signPayload = async (payload: SignerPayloadJSON) => {
22+
const signature = await this.magic.rpcProvider.send<string>('pdt_signPayload', [payload]);
23+
24+
return {
25+
id: this.nextId++,
26+
signature: signature as HexString,
27+
};
28+
};
29+
/**
30+
* @description signs a raw payload, only the bytes data as supplied
31+
*/
32+
signRaw = async (raw: SignerPayloadRaw) => {
33+
const signature = await this.magic.rpcProvider.send<HexString>('pdt_signRaw', [raw]);
34+
35+
return {
36+
id: this.nextId++,
37+
signature,
38+
};
39+
};
40+
41+
/**
42+
* @description Receives an update for the extrinsic signed by a `signer.sign`
43+
*/
44+
update?: (id: number, status: H256 | ISubmittableResult) => void;
45+
}

0 commit comments

Comments
 (0)