@novasamatech/product-sdk 0.7.8-0 → 0.7.8-2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/signer.d.ts +17 -0
- package/dist/signer.js +71 -0
- package/package.json +2 -2
package/dist/signer.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { CodecType, ProductAccountId as ProductAccountIdCodec, Transport } from '@novasamatech/host-api';
|
|
2
|
+
import type { PolkadotSigner } from 'polkadot-api';
|
|
3
|
+
export type ProductAccountId = CodecType<typeof ProductAccountIdCodec>;
|
|
4
|
+
export type ProductSignerConfig = {
|
|
5
|
+
/** Native token symbol — used by hosts that compute CheckMetadataHash or render fees. Defaults to "". */
|
|
6
|
+
tokenSymbol?: string;
|
|
7
|
+
/** Native token decimals — see tokenSymbol. Defaults to 0. */
|
|
8
|
+
tokenDecimals?: number;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Builds a `PolkadotSigner` that delegates to the host via `host_create_transaction`
|
|
12
|
+
* (the new signing flow described in https://github.com/polkadot-js/api/issues/6213).
|
|
13
|
+
*
|
|
14
|
+
* The factory is async because `PolkadotSigner.publicKey` must be a synchronous
|
|
15
|
+
* `Uint8Array` on the returned object — it is fetched up front via `host_account_get`.
|
|
16
|
+
*/
|
|
17
|
+
export declare const createProductSigner: (accountId: ProductAccountId, config?: ProductSignerConfig, transport?: Transport) => Promise<PolkadotSigner>;
|
package/dist/signer.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { assertEnumVariant, createHostApi, enumValue, fromHex, toHex } from '@novasamatech/host-api';
|
|
2
|
+
import { decAnyMetadata, unifyMetadata } from '@polkadot-api/substrate-bindings';
|
|
3
|
+
import { sandboxTransport } from './sandboxTransport.js';
|
|
4
|
+
const UNSUPPORTED_VERSION_ERROR = 'Unsupported message version';
|
|
5
|
+
/**
|
|
6
|
+
* Builds a `PolkadotSigner` that delegates to the host via `host_create_transaction`
|
|
7
|
+
* (the new signing flow described in https://github.com/polkadot-js/api/issues/6213).
|
|
8
|
+
*
|
|
9
|
+
* The factory is async because `PolkadotSigner.publicKey` must be a synchronous
|
|
10
|
+
* `Uint8Array` on the returned object — it is fetched up front via `host_account_get`.
|
|
11
|
+
*/
|
|
12
|
+
export const createProductSigner = async (accountId, config = {}, transport = sandboxTransport) => {
|
|
13
|
+
const hostApi = createHostApi(transport);
|
|
14
|
+
const accountResponse = await hostApi.accountGet(enumValue('v1', accountId));
|
|
15
|
+
const account = accountResponse.match(response => {
|
|
16
|
+
assertEnumVariant(response, 'v1', UNSUPPORTED_VERSION_ERROR);
|
|
17
|
+
return response.value;
|
|
18
|
+
}, err => {
|
|
19
|
+
assertEnumVariant(err, 'v1', UNSUPPORTED_VERSION_ERROR);
|
|
20
|
+
throw err.value;
|
|
21
|
+
});
|
|
22
|
+
const tokenSymbol = config.tokenSymbol ?? 'DOT';
|
|
23
|
+
const tokenDecimals = config.tokenDecimals ?? 12;
|
|
24
|
+
return {
|
|
25
|
+
publicKey: account.publicKey,
|
|
26
|
+
async signTx(callData, signedExtensions, metadata, atBlockNumber) {
|
|
27
|
+
const decMeta = unifyMetadata(decAnyMetadata(metadata));
|
|
28
|
+
const { version: versions } = decMeta.extrinsic;
|
|
29
|
+
const latestVersion = versions.reduce((acc, v) => Math.max(acc, v), 0);
|
|
30
|
+
const txExtVersion = latestVersion === 4 ? 0 : latestVersion;
|
|
31
|
+
const txPayload = {
|
|
32
|
+
version: 1,
|
|
33
|
+
signer: null,
|
|
34
|
+
callData,
|
|
35
|
+
extensions: Object.values(signedExtensions).map(({ identifier, value, additionalSigned }) => ({
|
|
36
|
+
id: identifier,
|
|
37
|
+
extra: value,
|
|
38
|
+
additionalSigned: additionalSigned,
|
|
39
|
+
})),
|
|
40
|
+
txExtVersion,
|
|
41
|
+
context: {
|
|
42
|
+
metadata: metadata,
|
|
43
|
+
tokenSymbol,
|
|
44
|
+
tokenDecimals,
|
|
45
|
+
bestBlockHeight: atBlockNumber,
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
const response = await hostApi.createTransaction(enumValue('v1', [accountId, txPayload]));
|
|
49
|
+
return response.match(response => {
|
|
50
|
+
assertEnumVariant(response, 'v1', UNSUPPORTED_VERSION_ERROR);
|
|
51
|
+
return response.value;
|
|
52
|
+
}, err => {
|
|
53
|
+
assertEnumVariant(err, 'v1', UNSUPPORTED_VERSION_ERROR);
|
|
54
|
+
throw err.value;
|
|
55
|
+
});
|
|
56
|
+
},
|
|
57
|
+
async signBytes(data) {
|
|
58
|
+
const response = await hostApi.signRaw(enumValue('v1', {
|
|
59
|
+
account: accountId,
|
|
60
|
+
payload: { tag: 'Bytes', value: data },
|
|
61
|
+
}));
|
|
62
|
+
return response.match(response => {
|
|
63
|
+
assertEnumVariant(response, 'v1', UNSUPPORTED_VERSION_ERROR);
|
|
64
|
+
return fromHex(response.value.signature);
|
|
65
|
+
}, err => {
|
|
66
|
+
assertEnumVariant(err, 'v1', UNSUPPORTED_VERSION_ERROR);
|
|
67
|
+
throw err.value;
|
|
68
|
+
});
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@novasamatech/product-sdk",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.7.8-
|
|
4
|
+
"version": "0.7.8-2",
|
|
5
5
|
"description": "Polkadot product SDK: integrate and run your product inside Polkadot browser.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
7
7
|
"repository": {
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"@polkadot/extension-inject": "^0.63.1",
|
|
29
29
|
"@polkadot-api/json-rpc-provider-proxy": "^0.4.0",
|
|
30
|
-
"@novasamatech/host-api": "0.7.8-
|
|
30
|
+
"@novasamatech/host-api": "0.7.8-2",
|
|
31
31
|
"polkadot-api": ">=2",
|
|
32
32
|
"neverthrow": "^8.2.0"
|
|
33
33
|
},
|