Skip to content

Commit b2cddaf

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

File tree

5 files changed

+683
-56
lines changed

5 files changed

+683
-56
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: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
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+
import { POLKADOT_METHODS } from './type';
4+
5+
export { MagicSigner } from './magic-signer';
36

47
export class PolkadotExtension extends Extension.Internal<'polkadot', PolkadotConfig> {
58
name = 'polkadot' as const;
@@ -19,7 +22,7 @@ export class PolkadotExtension extends Extension.Internal<'polkadot', PolkadotCo
1922
return this.request({
2023
id: 42,
2124
jsonrpc: '2.0',
22-
method: 'pdt_sendTransaction',
25+
method: POLKADOT_METHODS.SEND_TRANSACTION,
2326
params: { to, value },
2427
});
2528
};
@@ -28,7 +31,7 @@ export class PolkadotExtension extends Extension.Internal<'polkadot', PolkadotCo
2831
return this.request({
2932
id: 42,
3033
jsonrpc: '2.0',
31-
method: 'pdt_contractCall',
34+
method: POLKADOT_METHODS.CONTRACT_CALL,
3235
params: { contractAddress, value, maxGas, data },
3336
});
3437
};
@@ -37,7 +40,7 @@ export class PolkadotExtension extends Extension.Internal<'polkadot', PolkadotCo
3740
return this.request({
3841
id: 42,
3942
jsonrpc: '2.0',
40-
method: 'pdt_getAccount',
43+
method: POLKADOT_METHODS.GET_ACCOUNT,
4144
params: [],
4245
});
4346
};
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+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,10 @@ export interface ConfigType {
66
rpcUrl: string;
77
chainType: string;
88
}
9+
10+
export const POLKADOT_METHODS = {
11+
SIGN: 'pdt_sign',
12+
SEND_TRANSACTION: 'pdt_sendTransaction',
13+
CONTRACT_CALL: 'pdt_contractCall',
14+
GET_ACCOUNT: 'pdt_getAccount',
15+
} as const;

0 commit comments

Comments
 (0)