@ckb-ccc/rei 0.0.12
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/CHANGELOG.md +8 -0
- package/README.md +45 -0
- package/dist/advanced.d.ts +2 -0
- package/dist/advanced.d.ts.map +1 -0
- package/dist/advanced.js +1 -0
- package/dist/advancedBarrel.d.ts +103 -0
- package/dist/advancedBarrel.d.ts.map +1 -0
- package/dist/advancedBarrel.js +1 -0
- package/dist/barrel.d.ts +3 -0
- package/dist/barrel.d.ts.map +1 -0
- package/dist/barrel.js +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1 -0
- package/dist/package.json +3 -0
- package/dist/signer.d.ts +83 -0
- package/dist/signer.d.ts.map +1 -0
- package/dist/signer.js +144 -0
- package/dist/signersFactory.d.ts +8 -0
- package/dist/signersFactory.d.ts.map +1 -0
- package/dist/signersFactory.js +18 -0
- package/dist.commonjs/advanced.d.ts +2 -0
- package/dist.commonjs/advanced.d.ts.map +1 -0
- package/dist.commonjs/advanced.js +27 -0
- package/dist.commonjs/advancedBarrel.d.ts +103 -0
- package/dist.commonjs/advancedBarrel.d.ts.map +1 -0
- package/dist.commonjs/advancedBarrel.js +2 -0
- package/dist.commonjs/barrel.d.ts +3 -0
- package/dist.commonjs/barrel.d.ts.map +1 -0
- package/dist.commonjs/barrel.js +18 -0
- package/dist.commonjs/index.d.ts +2 -0
- package/dist.commonjs/index.d.ts.map +1 -0
- package/dist.commonjs/index.js +27 -0
- package/dist.commonjs/package.json +3 -0
- package/dist.commonjs/signer.d.ts +83 -0
- package/dist.commonjs/signer.d.ts.map +1 -0
- package/dist.commonjs/signer.js +148 -0
- package/dist.commonjs/signersFactory.d.ts +8 -0
- package/dist.commonjs/signersFactory.d.ts.map +1 -0
- package/dist.commonjs/signersFactory.js +22 -0
- package/package.json +55 -0
- package/src/advanced.ts +1 -0
- package/src/advancedBarrel.ts +103 -0
- package/src/barrel.ts +2 -0
- package/src/index.ts +1 -0
- package/src/signer.ts +180 -0
- package/src/signersFactory.ts +23 -0
- package/typedoc.json +6 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.Rei = void 0;
|
|
27
|
+
exports.Rei = __importStar(require("./barrel.js"));
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { ccc } from "@ckb-ccc/core";
|
|
2
|
+
import { Provider } from "./advancedBarrel.js";
|
|
3
|
+
/**
|
|
4
|
+
* Class representing a CKB signer that extends Signer from @ckb-ccc/core.
|
|
5
|
+
* @public
|
|
6
|
+
*/
|
|
7
|
+
export declare class ReiSigner extends ccc.Signer {
|
|
8
|
+
readonly provider: Provider;
|
|
9
|
+
/**
|
|
10
|
+
* Creates an instance of Signer.
|
|
11
|
+
* @param client - The client instance.
|
|
12
|
+
* @param provider - The provider instance.
|
|
13
|
+
*/
|
|
14
|
+
constructor(client: ccc.Client, provider: Provider);
|
|
15
|
+
/**
|
|
16
|
+
* Register a listener to be called when this signer is replaced.
|
|
17
|
+
*
|
|
18
|
+
* @returns A function for unregister
|
|
19
|
+
*/
|
|
20
|
+
onReplaced(listener: () => void): () => void;
|
|
21
|
+
/**
|
|
22
|
+
* Gets the signer type.
|
|
23
|
+
* @returns The type of the signer.
|
|
24
|
+
*/
|
|
25
|
+
get type(): ccc.SignerType;
|
|
26
|
+
/**
|
|
27
|
+
* Gets the sign type.
|
|
28
|
+
* @returns The sign type.
|
|
29
|
+
*/
|
|
30
|
+
get signType(): ccc.SignerSignType;
|
|
31
|
+
/**
|
|
32
|
+
* Connects to the provider by requesting authentication.
|
|
33
|
+
* @returns A promise that resolves when the connection is established.
|
|
34
|
+
*/
|
|
35
|
+
connect(): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Checks if the signer is connected.
|
|
38
|
+
* @returns A promise that resolves to true if connected, false otherwise.
|
|
39
|
+
*/
|
|
40
|
+
isConnected(): Promise<boolean>;
|
|
41
|
+
_getNetworkChanged(): Promise<boolean>;
|
|
42
|
+
/**
|
|
43
|
+
* Gets the internal address.
|
|
44
|
+
* @returns A promise that resolves to the internal address.
|
|
45
|
+
*/
|
|
46
|
+
getInternalAddress(): Promise<string>;
|
|
47
|
+
/**
|
|
48
|
+
* Gets the address object.
|
|
49
|
+
* @returns A promise that resolves to the address object.
|
|
50
|
+
*/
|
|
51
|
+
getAddressObj(): Promise<ccc.Address>;
|
|
52
|
+
/**
|
|
53
|
+
* Gets the address objects.
|
|
54
|
+
* @returns A promise that resolves to an array of address objects.
|
|
55
|
+
*/
|
|
56
|
+
getAddressObjs(): Promise<ccc.Address[]>;
|
|
57
|
+
/**
|
|
58
|
+
* Gets the identity of the signer.
|
|
59
|
+
* @returns A promise that resolves to the identity.
|
|
60
|
+
*/
|
|
61
|
+
getIdentity(): Promise<string>;
|
|
62
|
+
/**
|
|
63
|
+
* Signs a raw message with the personal account.
|
|
64
|
+
* @param message - The message to sign.
|
|
65
|
+
* @returns A promise that resolves to the signed message.
|
|
66
|
+
*/
|
|
67
|
+
signMessageRaw(message: string): Promise<ccc.Hex>;
|
|
68
|
+
/**
|
|
69
|
+
* prepare a transaction before signing.
|
|
70
|
+
*
|
|
71
|
+
* @param txLike - The transaction to prepare, represented as a TransactionLike object.
|
|
72
|
+
* @returns A promise that resolves to the prepared Transaction object.
|
|
73
|
+
*/
|
|
74
|
+
prepareTransaction(txLike: ccc.TransactionLike): Promise<ccc.Transaction>;
|
|
75
|
+
/**
|
|
76
|
+
* Signs a transaction without preparing information for it.
|
|
77
|
+
*
|
|
78
|
+
* @param txLike - The transaction to sign, represented as a TransactionLike object.
|
|
79
|
+
* @returns A promise that resolves to the signed Transaction object.
|
|
80
|
+
*/
|
|
81
|
+
signOnlyTransaction(txLike: ccc.TransactionLike): Promise<ccc.Transaction>;
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=signer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signer.d.ts","sourceRoot":"","sources":["../src/signer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAGpC,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAE/C;;;GAGG;AACH,qBAAa,SAAU,SAAQ,GAAG,CAAC,MAAM;aAQrB,QAAQ,EAAE,QAAQ;IAPpC;;;;OAIG;gBAED,MAAM,EAAE,GAAG,CAAC,MAAM,EACF,QAAQ,EAAE,QAAQ;IAKpC;;;;OAIG;IAEH,UAAU,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI;IAgB5C;;;OAGG;IACH,IAAI,IAAI,IAAI,GAAG,CAAC,UAAU,CAEzB;IAED;;;OAGG;IACH,IAAI,QAAQ,IAAI,GAAG,CAAC,cAAc,CAEjC;IAED;;;OAGG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAS9B;;;OAGG;IACG,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;IAQ/B,kBAAkB,IAAI,OAAO,CAAC,OAAO,CAAC;IAQ5C;;;OAGG;IACG,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC;IAI3C;;;OAGG;IACG,aAAa,IAAI,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC;IAI3C;;;OAGG;IACG,cAAc,IAAI,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC;IAI9C;;;OAGG;IACG,WAAW,IAAI,OAAO,CAAC,MAAM,CAAC;IAMpC;;;;OAIG;IACG,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;IAOvD;;;;;OAKG;IAEG,kBAAkB,CACtB,MAAM,EAAE,GAAG,CAAC,eAAe,GAC1B,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC;IAc3B;;;;;OAKG;IAEG,mBAAmB,CACvB,MAAM,EAAE,GAAG,CAAC,eAAe,GAC1B,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC;CAQ5B"}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ReiSigner = void 0;
|
|
4
|
+
const core_1 = require("@ckb-ccc/core");
|
|
5
|
+
const advanced_1 = require("@ckb-ccc/core/advanced");
|
|
6
|
+
/**
|
|
7
|
+
* Class representing a CKB signer that extends Signer from @ckb-ccc/core.
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
class ReiSigner extends core_1.ccc.Signer {
|
|
11
|
+
/**
|
|
12
|
+
* Creates an instance of Signer.
|
|
13
|
+
* @param client - The client instance.
|
|
14
|
+
* @param provider - The provider instance.
|
|
15
|
+
*/
|
|
16
|
+
constructor(client, provider) {
|
|
17
|
+
super(client);
|
|
18
|
+
this.provider = provider;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Register a listener to be called when this signer is replaced.
|
|
22
|
+
*
|
|
23
|
+
* @returns A function for unregister
|
|
24
|
+
*/
|
|
25
|
+
onReplaced(listener) {
|
|
26
|
+
const stop = [];
|
|
27
|
+
const replacer = async () => {
|
|
28
|
+
listener();
|
|
29
|
+
stop[0]?.();
|
|
30
|
+
};
|
|
31
|
+
stop.push(() => {
|
|
32
|
+
this.provider.off("accountsChanged", replacer);
|
|
33
|
+
this.provider.off("chainChanged", replacer);
|
|
34
|
+
});
|
|
35
|
+
this.provider.on("accountsChanged", replacer);
|
|
36
|
+
this.provider.on("chainChanged", replacer);
|
|
37
|
+
return stop[0];
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Gets the signer type.
|
|
41
|
+
* @returns The type of the signer.
|
|
42
|
+
*/
|
|
43
|
+
get type() {
|
|
44
|
+
return core_1.ccc.SignerType.CKB;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Gets the sign type.
|
|
48
|
+
* @returns The sign type.
|
|
49
|
+
*/
|
|
50
|
+
get signType() {
|
|
51
|
+
return core_1.ccc.SignerSignType.CkbSecp256k1;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Connects to the provider by requesting authentication.
|
|
55
|
+
* @returns A promise that resolves when the connection is established.
|
|
56
|
+
*/
|
|
57
|
+
async connect() {
|
|
58
|
+
const prefixClient = this.client.addressPrefix;
|
|
59
|
+
const netChange = await this._getNetworkChanged();
|
|
60
|
+
if (netChange) {
|
|
61
|
+
const data = prefixClient === "ckb" ? "mainnet" : "testnet";
|
|
62
|
+
await this.provider.request({ method: "ckb_switchNetwork", data });
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Checks if the signer is connected.
|
|
67
|
+
* @returns A promise that resolves to true if connected, false otherwise.
|
|
68
|
+
*/
|
|
69
|
+
async isConnected() {
|
|
70
|
+
const connected = await this.provider.isConnected();
|
|
71
|
+
if (!connected || (await this._getNetworkChanged())) {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
return connected;
|
|
75
|
+
}
|
|
76
|
+
async _getNetworkChanged() {
|
|
77
|
+
const prefixClient = this.client.addressPrefix;
|
|
78
|
+
const address = await this.getInternalAddress();
|
|
79
|
+
const { prefix } = advanced_1.cccA.addressPayloadFromString(address);
|
|
80
|
+
return prefixClient !== prefix;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Gets the internal address.
|
|
84
|
+
* @returns A promise that resolves to the internal address.
|
|
85
|
+
*/
|
|
86
|
+
async getInternalAddress() {
|
|
87
|
+
return this.provider.request({ method: "ckb_requestAccounts" });
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Gets the address object.
|
|
91
|
+
* @returns A promise that resolves to the address object.
|
|
92
|
+
*/
|
|
93
|
+
async getAddressObj() {
|
|
94
|
+
return core_1.ccc.Address.fromString(await this.getInternalAddress(), this.client);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Gets the address objects.
|
|
98
|
+
* @returns A promise that resolves to an array of address objects.
|
|
99
|
+
*/
|
|
100
|
+
async getAddressObjs() {
|
|
101
|
+
return [await this.getAddressObj()];
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Gets the identity of the signer.
|
|
105
|
+
* @returns A promise that resolves to the identity.
|
|
106
|
+
*/
|
|
107
|
+
async getIdentity() {
|
|
108
|
+
return this.provider.request({
|
|
109
|
+
method: "ckb_getPublicKey",
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Signs a raw message with the personal account.
|
|
114
|
+
* @param message - The message to sign.
|
|
115
|
+
* @returns A promise that resolves to the signed message.
|
|
116
|
+
*/
|
|
117
|
+
async signMessageRaw(message) {
|
|
118
|
+
return this.provider.request({
|
|
119
|
+
method: "ckb_signMessage",
|
|
120
|
+
data: { message },
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* prepare a transaction before signing.
|
|
125
|
+
*
|
|
126
|
+
* @param txLike - The transaction to prepare, represented as a TransactionLike object.
|
|
127
|
+
* @returns A promise that resolves to the prepared Transaction object.
|
|
128
|
+
*/
|
|
129
|
+
async prepareTransaction(txLike) {
|
|
130
|
+
const tx = core_1.ccc.Transaction.from(txLike);
|
|
131
|
+
await tx.addCellDepsOfKnownScripts(this.client, core_1.ccc.KnownScript.Secp256k1Blake160);
|
|
132
|
+
return core_1.ccc.reduceAsync(await this.getAddressObjs(), (tx, { script }) => tx.prepareSighashAllWitness(script, 65, this.client), tx);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Signs a transaction without preparing information for it.
|
|
136
|
+
*
|
|
137
|
+
* @param txLike - The transaction to sign, represented as a TransactionLike object.
|
|
138
|
+
* @returns A promise that resolves to the signed Transaction object.
|
|
139
|
+
*/
|
|
140
|
+
async signOnlyTransaction(txLike) {
|
|
141
|
+
const txFormat = advanced_1.cccA.JsonRpcTransformers.transactionFrom(txLike);
|
|
142
|
+
return this.provider.request({
|
|
143
|
+
method: "ckb_signTransaction",
|
|
144
|
+
data: { txSkeleton: txFormat },
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
exports.ReiSigner = ReiSigner;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ccc } from "@ckb-ccc/core";
|
|
2
|
+
/**
|
|
3
|
+
* Retrieves the Rei signer if available.
|
|
4
|
+
* @param client - The client instance.
|
|
5
|
+
* @returns The Signer instance if the Rei provider is available, otherwise undefined.
|
|
6
|
+
*/
|
|
7
|
+
export declare function getReiSigners(client: ccc.Client): ccc.SignerInfo[];
|
|
8
|
+
//# sourceMappingURL=signersFactory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signersFactory.d.ts","sourceRoot":"","sources":["../src/signersFactory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAIpC;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,GAAG,GAAG,CAAC,UAAU,EAAE,CAalE"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getReiSigners = void 0;
|
|
4
|
+
const signer_js_1 = require("./signer.js");
|
|
5
|
+
/**
|
|
6
|
+
* Retrieves the Rei signer if available.
|
|
7
|
+
* @param client - The client instance.
|
|
8
|
+
* @returns The Signer instance if the Rei provider is available, otherwise undefined.
|
|
9
|
+
*/
|
|
10
|
+
function getReiSigners(client) {
|
|
11
|
+
const windowRef = window;
|
|
12
|
+
if (typeof windowRef.ckb === "undefined") {
|
|
13
|
+
return [];
|
|
14
|
+
}
|
|
15
|
+
return [
|
|
16
|
+
{
|
|
17
|
+
signer: new signer_js_1.ReiSigner(client, windowRef.ckb),
|
|
18
|
+
name: "CKB",
|
|
19
|
+
},
|
|
20
|
+
];
|
|
21
|
+
}
|
|
22
|
+
exports.getReiSigners = getReiSigners;
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ckb-ccc/rei",
|
|
3
|
+
"version": "0.0.12",
|
|
4
|
+
"description": "CCC - CKBer's Codebase. Common Chains Connector's support for Rei",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"private": false,
|
|
7
|
+
"homepage": "https://github.com/ckb-ecofund/ccc",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git://github.com/ckb-ecofund/ccc.git"
|
|
11
|
+
},
|
|
12
|
+
"main": "dist.commonjs/index.js",
|
|
13
|
+
"module": "dist/index.js",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"import": "./dist/index.js",
|
|
17
|
+
"default": "./dist.commonjs/index.js"
|
|
18
|
+
},
|
|
19
|
+
"./barrel": {
|
|
20
|
+
"import": "./dist/barrel.js",
|
|
21
|
+
"default": "./dist.commonjs/barrel.js"
|
|
22
|
+
},
|
|
23
|
+
"./advanced": {
|
|
24
|
+
"import": "./dist/advanced.js",
|
|
25
|
+
"default": "./dist.commonjs/advanced.js"
|
|
26
|
+
},
|
|
27
|
+
"./advancedBarrel": {
|
|
28
|
+
"import": "./dist/advancedBarrel.js",
|
|
29
|
+
"default": "./dist.commonjs/advancedBarrel.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@eslint/js": "^9.1.1",
|
|
34
|
+
"copyfiles": "^2.4.1",
|
|
35
|
+
"eslint": "^9.1.0",
|
|
36
|
+
"eslint-config-prettier": "^9.1.0",
|
|
37
|
+
"eslint-plugin-prettier": "^5.1.3",
|
|
38
|
+
"prettier": "^3.2.5",
|
|
39
|
+
"prettier-plugin-organize-imports": "^3.2.4",
|
|
40
|
+
"rimraf": "^5.0.5",
|
|
41
|
+
"typescript": "^5.4.5",
|
|
42
|
+
"typescript-eslint": "^7.7.0"
|
|
43
|
+
},
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public"
|
|
46
|
+
},
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"@ckb-ccc/core": "0.0.12"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"build": "rimraf ./dist && rimraf ./dist.commonjs && tsc && tsc --project tsconfig.commonjs.json && copyfiles -u 2 misc/basedirs/**/* .",
|
|
52
|
+
"lint": "eslint",
|
|
53
|
+
"format": "prettier --write . && eslint --fix"
|
|
54
|
+
}
|
|
55
|
+
}
|
package/src/advanced.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as ReiA from "./advancedBarrel.js";
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { ccc } from "@ckb-ccc/core";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Interface representing a provider for interacting with accounts and signing messages.
|
|
5
|
+
* @interface
|
|
6
|
+
*/
|
|
7
|
+
export interface Provider {
|
|
8
|
+
/**
|
|
9
|
+
* Sends a request to the provider.
|
|
10
|
+
* @type {RequestMethod}
|
|
11
|
+
*/
|
|
12
|
+
request: RequestMethod;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Adds an event listener to the provider.
|
|
16
|
+
* @type {OnMethod}
|
|
17
|
+
*/
|
|
18
|
+
on: OnMethod;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Checks if rei wallet is connected.
|
|
22
|
+
* @returns A promise that resolves to true if connected, false otherwise.
|
|
23
|
+
*/
|
|
24
|
+
isConnected(): Promise<boolean>;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Removes an event listener from the provider.
|
|
28
|
+
* @param {string} eventName - The name of the event to remove the listener from.
|
|
29
|
+
* @param {(...args: unknown[]) => unknown} listener - The listener function to remove.
|
|
30
|
+
* @returns {Provider} The provider instance.
|
|
31
|
+
*/
|
|
32
|
+
off(eventName: string, listener: (...args: unknown[]) => unknown): void;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Interface representing a method to add event listeners to the provider.
|
|
37
|
+
* @interface
|
|
38
|
+
*/
|
|
39
|
+
export interface OnMethod {
|
|
40
|
+
/**
|
|
41
|
+
* Adds an event listener to the provider.
|
|
42
|
+
* @param {string} eventName - The name of the event.
|
|
43
|
+
* @param {(...args: unknown[]) => unknown} listener - The listener function.
|
|
44
|
+
* @returns {Provider} The provider instance.
|
|
45
|
+
*/
|
|
46
|
+
(eventName: string, listener: (...args: unknown[]) => unknown): void;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface RequestMethod {
|
|
50
|
+
/**
|
|
51
|
+
* Requests the accounts from the provider.
|
|
52
|
+
* @param request - The request object.
|
|
53
|
+
* @param request.method - The method name.
|
|
54
|
+
* @param request.data - The optional method parameters.
|
|
55
|
+
* @returns A promise that resolves to an array of account addresses.
|
|
56
|
+
*/
|
|
57
|
+
(request: {
|
|
58
|
+
method: "ckb_requestAccounts";
|
|
59
|
+
data?: undefined;
|
|
60
|
+
}): Promise<string>;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Signs a message with the personal account.
|
|
64
|
+
* @param request - The request object.
|
|
65
|
+
* @param request.method - The method name.
|
|
66
|
+
* @param request.data - The method parameters.
|
|
67
|
+
* @returns A promise that resolves to the signed message.
|
|
68
|
+
*/
|
|
69
|
+
(request: {
|
|
70
|
+
method: "ckb_signMessage";
|
|
71
|
+
data: { message: string };
|
|
72
|
+
}): Promise<ccc.Hex>;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Gets the identity of the signer.
|
|
76
|
+
* @param request - The request object.
|
|
77
|
+
* @param request.method - The method name.
|
|
78
|
+
* @returns A promise that resolves to the signed message.
|
|
79
|
+
*/
|
|
80
|
+
(request: { method: "ckb_getPublicKey" }): Promise<string>;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Get network.
|
|
84
|
+
* @param request - The request object.
|
|
85
|
+
* @param request.method - The method name.
|
|
86
|
+
* @param request.data - The method parameters.
|
|
87
|
+
* @returns A promise that resolves to the signed message.
|
|
88
|
+
*/
|
|
89
|
+
(request: {
|
|
90
|
+
method: "ckb_switchNetwork";
|
|
91
|
+
data: string;
|
|
92
|
+
}): Promise<{ type: string; network: string }>;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Signs transaction .
|
|
96
|
+
* @param request - The request object.
|
|
97
|
+
* @param request.method - The method name.
|
|
98
|
+
* @param request.data - The method parameters.
|
|
99
|
+
* @returns A promise that resolves to the signed message.
|
|
100
|
+
*/
|
|
101
|
+
|
|
102
|
+
(request: { method: "ckb_signTransaction"; data: any }): Promise<any>;
|
|
103
|
+
}
|
package/src/barrel.ts
ADDED
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as Rei from "./barrel.js";
|
package/src/signer.ts
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { ccc } from "@ckb-ccc/core";
|
|
2
|
+
import { cccA } from "@ckb-ccc/core/advanced";
|
|
3
|
+
|
|
4
|
+
import { Provider } from "./advancedBarrel.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Class representing a CKB signer that extends Signer from @ckb-ccc/core.
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
export class ReiSigner extends ccc.Signer {
|
|
11
|
+
/**
|
|
12
|
+
* Creates an instance of Signer.
|
|
13
|
+
* @param client - The client instance.
|
|
14
|
+
* @param provider - The provider instance.
|
|
15
|
+
*/
|
|
16
|
+
constructor(
|
|
17
|
+
client: ccc.Client,
|
|
18
|
+
public readonly provider: Provider,
|
|
19
|
+
) {
|
|
20
|
+
super(client);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Register a listener to be called when this signer is replaced.
|
|
25
|
+
*
|
|
26
|
+
* @returns A function for unregister
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
onReplaced(listener: () => void): () => void {
|
|
30
|
+
const stop: (() => void)[] = [];
|
|
31
|
+
const replacer = async () => {
|
|
32
|
+
listener();
|
|
33
|
+
stop[0]?.();
|
|
34
|
+
};
|
|
35
|
+
stop.push(() => {
|
|
36
|
+
this.provider.off("accountsChanged", replacer);
|
|
37
|
+
this.provider.off("chainChanged", replacer);
|
|
38
|
+
});
|
|
39
|
+
this.provider.on("accountsChanged", replacer);
|
|
40
|
+
this.provider.on("chainChanged", replacer);
|
|
41
|
+
|
|
42
|
+
return stop[0];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Gets the signer type.
|
|
47
|
+
* @returns The type of the signer.
|
|
48
|
+
*/
|
|
49
|
+
get type(): ccc.SignerType {
|
|
50
|
+
return ccc.SignerType.CKB;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Gets the sign type.
|
|
55
|
+
* @returns The sign type.
|
|
56
|
+
*/
|
|
57
|
+
get signType(): ccc.SignerSignType {
|
|
58
|
+
return ccc.SignerSignType.CkbSecp256k1;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Connects to the provider by requesting authentication.
|
|
63
|
+
* @returns A promise that resolves when the connection is established.
|
|
64
|
+
*/
|
|
65
|
+
async connect(): Promise<void> {
|
|
66
|
+
const prefixClient = this.client.addressPrefix;
|
|
67
|
+
const netChange = await this._getNetworkChanged();
|
|
68
|
+
if (netChange) {
|
|
69
|
+
const data = prefixClient === "ckb" ? "mainnet" : "testnet";
|
|
70
|
+
await this.provider.request({ method: "ckb_switchNetwork", data });
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Checks if the signer is connected.
|
|
76
|
+
* @returns A promise that resolves to true if connected, false otherwise.
|
|
77
|
+
*/
|
|
78
|
+
async isConnected(): Promise<boolean> {
|
|
79
|
+
const connected = await this.provider.isConnected();
|
|
80
|
+
if (!connected || (await this._getNetworkChanged())) {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
return connected;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async _getNetworkChanged(): Promise<boolean> {
|
|
87
|
+
const prefixClient = this.client.addressPrefix;
|
|
88
|
+
|
|
89
|
+
const address = await this.getInternalAddress();
|
|
90
|
+
const { prefix } = cccA.addressPayloadFromString(address);
|
|
91
|
+
return prefixClient !== prefix;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Gets the internal address.
|
|
96
|
+
* @returns A promise that resolves to the internal address.
|
|
97
|
+
*/
|
|
98
|
+
async getInternalAddress(): Promise<string> {
|
|
99
|
+
return this.provider.request({ method: "ckb_requestAccounts" });
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Gets the address object.
|
|
104
|
+
* @returns A promise that resolves to the address object.
|
|
105
|
+
*/
|
|
106
|
+
async getAddressObj(): Promise<ccc.Address> {
|
|
107
|
+
return ccc.Address.fromString(await this.getInternalAddress(), this.client);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Gets the address objects.
|
|
112
|
+
* @returns A promise that resolves to an array of address objects.
|
|
113
|
+
*/
|
|
114
|
+
async getAddressObjs(): Promise<ccc.Address[]> {
|
|
115
|
+
return [await this.getAddressObj()];
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Gets the identity of the signer.
|
|
120
|
+
* @returns A promise that resolves to the identity.
|
|
121
|
+
*/
|
|
122
|
+
async getIdentity(): Promise<string> {
|
|
123
|
+
return this.provider.request({
|
|
124
|
+
method: "ckb_getPublicKey",
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Signs a raw message with the personal account.
|
|
130
|
+
* @param message - The message to sign.
|
|
131
|
+
* @returns A promise that resolves to the signed message.
|
|
132
|
+
*/
|
|
133
|
+
async signMessageRaw(message: string): Promise<ccc.Hex> {
|
|
134
|
+
return this.provider.request({
|
|
135
|
+
method: "ckb_signMessage",
|
|
136
|
+
data: { message },
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* prepare a transaction before signing.
|
|
142
|
+
*
|
|
143
|
+
* @param txLike - The transaction to prepare, represented as a TransactionLike object.
|
|
144
|
+
* @returns A promise that resolves to the prepared Transaction object.
|
|
145
|
+
*/
|
|
146
|
+
|
|
147
|
+
async prepareTransaction(
|
|
148
|
+
txLike: ccc.TransactionLike,
|
|
149
|
+
): Promise<ccc.Transaction> {
|
|
150
|
+
const tx = ccc.Transaction.from(txLike);
|
|
151
|
+
await tx.addCellDepsOfKnownScripts(
|
|
152
|
+
this.client,
|
|
153
|
+
ccc.KnownScript.Secp256k1Blake160,
|
|
154
|
+
);
|
|
155
|
+
return ccc.reduceAsync(
|
|
156
|
+
await this.getAddressObjs(),
|
|
157
|
+
(tx: ccc.Transaction, { script }) =>
|
|
158
|
+
tx.prepareSighashAllWitness(script, 65, this.client),
|
|
159
|
+
tx,
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Signs a transaction without preparing information for it.
|
|
165
|
+
*
|
|
166
|
+
* @param txLike - The transaction to sign, represented as a TransactionLike object.
|
|
167
|
+
* @returns A promise that resolves to the signed Transaction object.
|
|
168
|
+
*/
|
|
169
|
+
|
|
170
|
+
async signOnlyTransaction(
|
|
171
|
+
txLike: ccc.TransactionLike,
|
|
172
|
+
): Promise<ccc.Transaction> {
|
|
173
|
+
const txFormat = cccA.JsonRpcTransformers.transactionFrom(txLike);
|
|
174
|
+
|
|
175
|
+
return this.provider.request({
|
|
176
|
+
method: "ckb_signTransaction",
|
|
177
|
+
data: { txSkeleton: txFormat },
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ccc } from "@ckb-ccc/core";
|
|
2
|
+
import { Provider } from "./advancedBarrel.js";
|
|
3
|
+
import { ReiSigner } from "./signer.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Retrieves the Rei signer if available.
|
|
7
|
+
* @param client - The client instance.
|
|
8
|
+
* @returns The Signer instance if the Rei provider is available, otherwise undefined.
|
|
9
|
+
*/
|
|
10
|
+
export function getReiSigners(client: ccc.Client): ccc.SignerInfo[] {
|
|
11
|
+
const windowRef = window as { ckb?: Provider };
|
|
12
|
+
|
|
13
|
+
if (typeof windowRef.ckb === "undefined") {
|
|
14
|
+
return [];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return [
|
|
18
|
+
{
|
|
19
|
+
signer: new ReiSigner(client, windowRef.ckb),
|
|
20
|
+
name: "CKB",
|
|
21
|
+
},
|
|
22
|
+
];
|
|
23
|
+
}
|