@ckb-ccc/core 0.0.5-alpha.1 → 0.0.5-alpha.11
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/address/index.d.ts +1 -1
- package/dist/address/index.d.ts.map +1 -1
- package/dist/address/index.js +4 -4
- package/dist/ckb/transaction.d.ts +4 -6
- package/dist/ckb/transaction.d.ts.map +1 -1
- package/dist/ckb/transaction.js +14 -15
- package/dist/client/client.d.ts +2 -2
- package/dist/client/client.d.ts.map +1 -1
- package/dist/client/client.js +2 -1
- package/dist/client/clientPublicMainnet.d.ts +1 -1
- package/dist/client/clientPublicMainnet.d.ts.map +1 -1
- package/dist/client/clientPublicMainnet.js +1 -1
- package/dist/client/clientPublicTestnet.d.ts +1 -1
- package/dist/client/clientPublicTestnet.d.ts.map +1 -1
- package/dist/client/clientPublicTestnet.js +1 -1
- package/dist/client/jsonRpc/index.d.ts +3 -3
- package/dist/client/jsonRpc/index.d.ts.map +1 -1
- package/dist/client/jsonRpc/index.js +4 -4
- package/dist/signer/btc/index.d.ts +1 -0
- package/dist/signer/btc/index.d.ts.map +1 -1
- package/dist/signer/btc/index.js +1 -0
- package/dist/signer/btc/signerBtc.d.ts +9 -1
- package/dist/signer/btc/signerBtc.d.ts.map +1 -1
- package/dist/signer/btc/signerBtc.js +19 -3
- package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts +0 -6
- package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts.map +1 -1
- package/dist/signer/btc/signerBtcPublicKeyReadonly.js +0 -8
- package/dist/signer/btc/verify.d.ts +3 -0
- package/dist/signer/btc/verify.d.ts.map +1 -0
- package/dist/signer/btc/verify.js +6 -0
- package/dist/signer/ckb/index.d.ts +1 -0
- package/dist/signer/ckb/index.d.ts.map +1 -1
- package/dist/signer/ckb/index.js +1 -0
- package/dist/signer/ckb/signerCkbScriptReadonly.d.ts +3 -7
- package/dist/signer/ckb/signerCkbScriptReadonly.d.ts.map +1 -1
- package/dist/signer/ckb/signerCkbScriptReadonly.js +8 -10
- package/dist/signer/ckb/verifyJoyId.d.ts +3 -0
- package/dist/signer/ckb/verifyJoyId.d.ts.map +1 -0
- package/dist/signer/ckb/verifyJoyId.js +12 -0
- package/dist/signer/dummy/alwaysError.d.ts +9 -0
- package/dist/signer/dummy/alwaysError.d.ts.map +1 -0
- package/dist/signer/dummy/alwaysError.js +10 -0
- package/dist/signer/dummy/dummy.d.ts +12 -0
- package/dist/signer/dummy/dummy.d.ts.map +1 -0
- package/dist/signer/dummy/dummy.js +19 -0
- package/dist/signer/dummy/index.d.ts +4 -0
- package/dist/signer/dummy/index.d.ts.map +1 -0
- package/dist/signer/dummy/index.js +3 -0
- package/dist/signer/dummy/openLink.d.ts +9 -0
- package/dist/signer/dummy/openLink.d.ts.map +1 -0
- package/dist/signer/dummy/openLink.js +10 -0
- package/dist/signer/evm/index.d.ts +1 -0
- package/dist/signer/evm/index.d.ts.map +1 -1
- package/dist/signer/evm/index.js +1 -0
- package/dist/signer/evm/signerEvm.d.ts +3 -1
- package/dist/signer/evm/signerEvm.d.ts.map +1 -1
- package/dist/signer/evm/signerEvm.js +10 -4
- package/dist/signer/evm/signerEvmAddressReadonly.d.ts +0 -6
- package/dist/signer/evm/signerEvmAddressReadonly.d.ts.map +1 -1
- package/dist/signer/evm/signerEvmAddressReadonly.js +0 -8
- package/dist/signer/evm/verify.d.ts +3 -0
- package/dist/signer/evm/verify.d.ts.map +1 -0
- package/dist/signer/evm/verify.js +6 -0
- package/dist/signer/index.d.ts +1 -0
- package/dist/signer/index.d.ts.map +1 -1
- package/dist/signer/index.js +1 -0
- package/dist/signer/{signer.d.ts → signer/index.d.ts} +69 -25
- package/dist/signer/signer/index.d.ts.map +1 -0
- package/dist/signer/{signer.js → signer/index.js} +101 -16
- package/dist/utils/index.d.ts +60 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +20 -0
- package/dist.commonjs/address/index.d.ts +1 -1
- package/dist.commonjs/address/index.d.ts.map +1 -1
- package/dist.commonjs/address/index.js +4 -4
- package/dist.commonjs/ckb/transaction.d.ts +4 -6
- package/dist.commonjs/ckb/transaction.d.ts.map +1 -1
- package/dist.commonjs/ckb/transaction.js +14 -15
- package/dist.commonjs/client/client.d.ts +2 -2
- package/dist.commonjs/client/client.d.ts.map +1 -1
- package/dist.commonjs/client/client.js +2 -1
- package/dist.commonjs/client/clientPublicMainnet.d.ts +1 -1
- package/dist.commonjs/client/clientPublicMainnet.d.ts.map +1 -1
- package/dist.commonjs/client/clientPublicMainnet.js +1 -1
- package/dist.commonjs/client/clientPublicTestnet.d.ts +1 -1
- package/dist.commonjs/client/clientPublicTestnet.d.ts.map +1 -1
- package/dist.commonjs/client/clientPublicTestnet.js +1 -1
- package/dist.commonjs/client/jsonRpc/index.d.ts +3 -3
- package/dist.commonjs/client/jsonRpc/index.d.ts.map +1 -1
- package/dist.commonjs/client/jsonRpc/index.js +4 -4
- package/dist.commonjs/signer/btc/index.d.ts +1 -0
- package/dist.commonjs/signer/btc/index.d.ts.map +1 -1
- package/dist.commonjs/signer/btc/index.js +1 -0
- package/dist.commonjs/signer/btc/signerBtc.d.ts +9 -1
- package/dist.commonjs/signer/btc/signerBtc.d.ts.map +1 -1
- package/dist.commonjs/signer/btc/signerBtc.js +18 -2
- package/dist.commonjs/signer/btc/signerBtcPublicKeyReadonly.d.ts +0 -6
- package/dist.commonjs/signer/btc/signerBtcPublicKeyReadonly.d.ts.map +1 -1
- package/dist.commonjs/signer/btc/signerBtcPublicKeyReadonly.js +0 -8
- package/dist.commonjs/signer/btc/verify.d.ts +3 -0
- package/dist.commonjs/signer/btc/verify.d.ts.map +1 -0
- package/dist.commonjs/signer/btc/verify.js +10 -0
- package/dist.commonjs/signer/ckb/index.d.ts +1 -0
- package/dist.commonjs/signer/ckb/index.d.ts.map +1 -1
- package/dist.commonjs/signer/ckb/index.js +1 -0
- package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.d.ts +3 -7
- package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.d.ts.map +1 -1
- package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.js +7 -9
- package/dist.commonjs/signer/ckb/verifyJoyId.d.ts +3 -0
- package/dist.commonjs/signer/ckb/verifyJoyId.d.ts.map +1 -0
- package/dist.commonjs/signer/ckb/verifyJoyId.js +16 -0
- package/dist.commonjs/signer/dummy/alwaysError.d.ts +9 -0
- package/dist.commonjs/signer/dummy/alwaysError.d.ts.map +1 -0
- package/dist.commonjs/signer/dummy/alwaysError.js +14 -0
- package/dist.commonjs/signer/dummy/dummy.d.ts +12 -0
- package/dist.commonjs/signer/dummy/dummy.d.ts.map +1 -0
- package/dist.commonjs/signer/dummy/dummy.js +23 -0
- package/dist.commonjs/signer/dummy/index.d.ts +4 -0
- package/dist.commonjs/signer/dummy/index.d.ts.map +1 -0
- package/dist.commonjs/signer/dummy/index.js +19 -0
- package/dist.commonjs/signer/dummy/openLink.d.ts +9 -0
- package/dist.commonjs/signer/dummy/openLink.d.ts.map +1 -0
- package/dist.commonjs/signer/dummy/openLink.js +14 -0
- package/dist.commonjs/signer/evm/index.d.ts +1 -0
- package/dist.commonjs/signer/evm/index.d.ts.map +1 -1
- package/dist.commonjs/signer/evm/index.js +1 -0
- package/dist.commonjs/signer/evm/signerEvm.d.ts +3 -1
- package/dist.commonjs/signer/evm/signerEvm.d.ts.map +1 -1
- package/dist.commonjs/signer/evm/signerEvm.js +9 -3
- package/dist.commonjs/signer/evm/signerEvmAddressReadonly.d.ts +0 -6
- package/dist.commonjs/signer/evm/signerEvmAddressReadonly.d.ts.map +1 -1
- package/dist.commonjs/signer/evm/signerEvmAddressReadonly.js +0 -8
- package/dist.commonjs/signer/evm/verify.d.ts +3 -0
- package/dist.commonjs/signer/evm/verify.d.ts.map +1 -0
- package/dist.commonjs/signer/evm/verify.js +10 -0
- package/dist.commonjs/signer/index.d.ts +1 -0
- package/dist.commonjs/signer/index.d.ts.map +1 -1
- package/dist.commonjs/signer/index.js +1 -0
- package/dist.commonjs/signer/{signer.d.ts → signer/index.d.ts} +69 -25
- package/dist.commonjs/signer/signer/index.d.ts.map +1 -0
- package/dist.commonjs/signer/signer/index.js +211 -0
- package/dist.commonjs/utils/index.d.ts +60 -0
- package/dist.commonjs/utils/index.d.ts.map +1 -1
- package/dist.commonjs/utils/index.js +23 -1
- package/package.json +6 -3
- package/src/address/index.ts +4 -7
- package/src/ckb/transaction.ts +18 -18
- package/src/client/client.ts +7 -6
- package/src/client/clientPublicMainnet.ts +1 -1
- package/src/client/clientPublicTestnet.ts +1 -1
- package/src/client/jsonRpc/index.ts +3 -3
- package/src/signer/btc/index.ts +1 -0
- package/src/signer/btc/signerBtc.ts +22 -7
- package/src/signer/btc/signerBtcPublicKeyReadonly.ts +0 -9
- package/src/signer/btc/verify.ts +14 -0
- package/src/signer/ckb/index.ts +1 -0
- package/src/signer/ckb/signerCkbScriptReadonly.ts +10 -11
- package/src/signer/ckb/verifyJoyId.ts +20 -0
- package/src/signer/dummy/alwaysError.ts +17 -0
- package/src/signer/dummy/dummy.ts +28 -0
- package/src/signer/dummy/index.ts +3 -0
- package/src/signer/dummy/openLink.ts +17 -0
- package/src/signer/evm/index.ts +1 -0
- package/src/signer/evm/signerEvm.ts +16 -10
- package/src/signer/evm/signerEvmAddressReadonly.ts +0 -9
- package/src/signer/evm/verify.ts +16 -0
- package/src/signer/index.ts +1 -0
- package/src/signer/{signer.ts → signer/index.ts} +135 -25
- package/src/utils/index.ts +108 -0
- package/dist/signer/signer.d.ts.map +0 -1
- package/dist.commonjs/signer/signer.d.ts.map +0 -1
- package/dist.commonjs/signer/signer.js +0 -125
- package/dist.commonjs/tsconfig.commonjs.tsbuildinfo +0 -1
|
@@ -1,13 +1,21 @@
|
|
|
1
1
|
import { Address } from "../../address";
|
|
2
2
|
import { Script, ScriptLike } from "../../ckb";
|
|
3
3
|
import { Client } from "../../client";
|
|
4
|
-
import { Signer } from "../signer";
|
|
4
|
+
import { Signer, SignerSignType, SignerType } from "../signer";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* A class extending Signer that provides read-only access to a CKB script.
|
|
8
8
|
* This class does not support signing operations.
|
|
9
9
|
*/
|
|
10
10
|
export class SignerCkbScriptReadonly extends Signer {
|
|
11
|
+
get type(): SignerType {
|
|
12
|
+
return SignerType.CKB;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
get signType(): SignerSignType {
|
|
16
|
+
return SignerSignType.Unknown;
|
|
17
|
+
}
|
|
18
|
+
|
|
11
19
|
private readonly script: Script;
|
|
12
20
|
|
|
13
21
|
/**
|
|
@@ -22,15 +30,6 @@ export class SignerCkbScriptReadonly extends Signer {
|
|
|
22
30
|
this.script = Script.from(script);
|
|
23
31
|
}
|
|
24
32
|
|
|
25
|
-
/**
|
|
26
|
-
* Construct a new signer with the client replaced.
|
|
27
|
-
*
|
|
28
|
-
* @returns A promise that resolves the new Signer.
|
|
29
|
-
*/
|
|
30
|
-
async replaceClient(client: Client): Promise<SignerCkbScriptReadonly> {
|
|
31
|
-
return new SignerCkbScriptReadonly(client, this.script);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
33
|
/**
|
|
35
34
|
* Connects to the client. This implementation does nothing as the class is read-only.
|
|
36
35
|
*
|
|
@@ -73,6 +72,6 @@ export class SignerCkbScriptReadonly extends Signer {
|
|
|
73
72
|
*/
|
|
74
73
|
|
|
75
74
|
async getAddressObjs(): Promise<Address[]> {
|
|
76
|
-
return [
|
|
75
|
+
return [Address.fromScript(this.script, this.client)];
|
|
77
76
|
}
|
|
78
77
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { verifySignature } from "@joyid/ckb";
|
|
2
|
+
import { BytesLike } from "../../bytes";
|
|
3
|
+
import { hexFrom } from "../../hex";
|
|
4
|
+
|
|
5
|
+
export function verifyMessageJoyId(
|
|
6
|
+
message: string | BytesLike,
|
|
7
|
+
signature: string,
|
|
8
|
+
identity: string,
|
|
9
|
+
): Promise<boolean> {
|
|
10
|
+
const challenge =
|
|
11
|
+
typeof message === "string" ? message : hexFrom(message).slice(2);
|
|
12
|
+
const { publicKey, keyType } = JSON.parse(identity);
|
|
13
|
+
|
|
14
|
+
return verifySignature({
|
|
15
|
+
challenge,
|
|
16
|
+
pubkey: publicKey,
|
|
17
|
+
keyType,
|
|
18
|
+
...JSON.parse(signature),
|
|
19
|
+
});
|
|
20
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Client } from "../../client";
|
|
2
|
+
import { SignerType } from "../signer";
|
|
3
|
+
import { SignerDummy } from "./dummy";
|
|
4
|
+
|
|
5
|
+
export class SignerAlwaysError extends SignerDummy {
|
|
6
|
+
constructor(
|
|
7
|
+
client: Client,
|
|
8
|
+
type: SignerType,
|
|
9
|
+
private readonly message: string,
|
|
10
|
+
) {
|
|
11
|
+
super(client, type);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async connect(): Promise<void> {
|
|
15
|
+
throw new Error(this.message);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Address } from "../../address";
|
|
2
|
+
import { Client } from "../../client";
|
|
3
|
+
import { Signer, SignerSignType, SignerType } from "../signer";
|
|
4
|
+
|
|
5
|
+
export abstract class SignerDummy extends Signer {
|
|
6
|
+
get signType(): SignerSignType {
|
|
7
|
+
return SignerSignType.Unknown;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
constructor(
|
|
11
|
+
client: Client,
|
|
12
|
+
public readonly type: SignerType,
|
|
13
|
+
) {
|
|
14
|
+
super(client);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async isConnected(): Promise<boolean> {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async getInternalAddress(): Promise<string> {
|
|
22
|
+
throw new Error("Can't get address from SignerDummy");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async getAddressObjs(): Promise<Address[]> {
|
|
26
|
+
throw new Error("Can't get addresses from SignerDummy");
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Client } from "../../client";
|
|
2
|
+
import { SignerType } from "../signer";
|
|
3
|
+
import { SignerDummy } from "./dummy";
|
|
4
|
+
|
|
5
|
+
export class SignerOpenLink extends SignerDummy {
|
|
6
|
+
constructor(
|
|
7
|
+
client: Client,
|
|
8
|
+
type: SignerType,
|
|
9
|
+
private readonly link: string,
|
|
10
|
+
) {
|
|
11
|
+
super(client, type);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async connect(): Promise<void> {
|
|
15
|
+
window.open(this.link, "_blank")?.focus();
|
|
16
|
+
}
|
|
17
|
+
}
|
package/src/signer/evm/index.ts
CHANGED
|
@@ -4,13 +4,22 @@ import { Script, Transaction, TransactionLike, WitnessArgs } from "../../ckb";
|
|
|
4
4
|
import { KnownScript } from "../../client";
|
|
5
5
|
import { hexFrom } from "../../hex";
|
|
6
6
|
import { numToBytes } from "../../num";
|
|
7
|
-
import {
|
|
7
|
+
import { reduceAsync } from "../../utils";
|
|
8
|
+
import { Signer, SignerSignType, SignerType } from "../signer";
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* An abstract class extending Signer for Ethereum Virtual Machine (EVM) based signing operations.
|
|
11
12
|
* This class provides methods to get EVM account, internal address, and signing transactions.
|
|
12
13
|
*/
|
|
13
14
|
export abstract class SignerEvm extends Signer {
|
|
15
|
+
get type(): SignerType {
|
|
16
|
+
return SignerType.EVM;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
get signType(): SignerSignType {
|
|
20
|
+
return SignerSignType.EvmPersonal;
|
|
21
|
+
}
|
|
22
|
+
|
|
14
23
|
/**
|
|
15
24
|
* Gets the EVM account associated with the signer.
|
|
16
25
|
*
|
|
@@ -63,14 +72,11 @@ export abstract class SignerEvm extends Signer {
|
|
|
63
72
|
* @returns A promise that resolves to the prepared Transaction object.
|
|
64
73
|
*/
|
|
65
74
|
async prepareTransaction(txLike: TransactionLike): Promise<Transaction> {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
tx.prepareSighashAllWitness(script, 85, this.client),
|
|
72
|
-
),
|
|
73
|
-
Promise.resolve(Transaction.from(txLike)),
|
|
75
|
+
return reduceAsync(
|
|
76
|
+
await this.getAddressObjs(),
|
|
77
|
+
(tx: Transaction, { script }) =>
|
|
78
|
+
tx.prepareSighashAllWitness(script, 85, this.client),
|
|
79
|
+
Transaction.from(txLike),
|
|
74
80
|
);
|
|
75
81
|
}
|
|
76
82
|
|
|
@@ -113,7 +119,7 @@ export abstract class SignerEvm extends Signer {
|
|
|
113
119
|
}
|
|
114
120
|
|
|
115
121
|
const signature = bytesFrom(
|
|
116
|
-
await this.
|
|
122
|
+
await this.signMessageRaw(messageTransformer(info.message)),
|
|
117
123
|
);
|
|
118
124
|
if (signature[signature.length - 1] >= 27) {
|
|
119
125
|
signature[signature.length - 1] -= 27;
|
|
@@ -21,15 +21,6 @@ export class SignerEvmAddressReadonly extends SignerEvm {
|
|
|
21
21
|
this.address = hexFrom(address);
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
/**
|
|
25
|
-
* Construct a new signer with the client replaced.
|
|
26
|
-
*
|
|
27
|
-
* @returns A promise that resolves the new Signer.
|
|
28
|
-
*/
|
|
29
|
-
async replaceClient(client: Client): Promise<SignerEvmAddressReadonly> {
|
|
30
|
-
return new SignerEvmAddressReadonly(client, this.address);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
24
|
/**
|
|
34
25
|
* Connects to the client. This implementation does nothing as the class is read-only.
|
|
35
26
|
*
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { verifyMessage } from "ethers";
|
|
2
|
+
import { BytesLike, bytesFrom } from "../../bytes";
|
|
3
|
+
|
|
4
|
+
export function verifyMessageEvmPersonal(
|
|
5
|
+
message: string | BytesLike,
|
|
6
|
+
signature: string,
|
|
7
|
+
address: string,
|
|
8
|
+
): boolean {
|
|
9
|
+
return (
|
|
10
|
+
address.toLowerCase() ===
|
|
11
|
+
verifyMessage(
|
|
12
|
+
typeof message === "string" ? message : bytesFrom(message),
|
|
13
|
+
signature,
|
|
14
|
+
).toLowerCase()
|
|
15
|
+
);
|
|
16
|
+
}
|
package/src/signer/index.ts
CHANGED
|
@@ -1,9 +1,36 @@
|
|
|
1
|
-
import { Address } from "
|
|
2
|
-
import { BytesLike } from "
|
|
3
|
-
import { Transaction, TransactionLike } from "
|
|
4
|
-
import { Client } from "
|
|
5
|
-
import { Hex } from "
|
|
6
|
-
import { Num } from "
|
|
1
|
+
import { Address } from "../../address";
|
|
2
|
+
import { BytesLike } from "../../bytes";
|
|
3
|
+
import { Transaction, TransactionLike } from "../../ckb";
|
|
4
|
+
import { Client } from "../../client";
|
|
5
|
+
import { Hex } from "../../hex";
|
|
6
|
+
import { Num } from "../../num";
|
|
7
|
+
import { verifyMessageBtcEcdsa } from "../btc";
|
|
8
|
+
import { verifyMessageJoyId } from "../ckb/verifyJoyId";
|
|
9
|
+
import { verifyMessageEvmPersonal } from "../evm/verify";
|
|
10
|
+
|
|
11
|
+
export enum SignerSignType {
|
|
12
|
+
Unknown = "Unknown",
|
|
13
|
+
BtcEcdsa = "BtcEcdsa",
|
|
14
|
+
EvmPersonal = "EvmPersonal",
|
|
15
|
+
JoyId = "JoyId",
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* An enumeration of signer display types in wallet.
|
|
20
|
+
*/
|
|
21
|
+
export enum SignerType {
|
|
22
|
+
EVM = "EVM",
|
|
23
|
+
BTC = "BTC",
|
|
24
|
+
CKB = "CKB",
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export class Signature {
|
|
28
|
+
constructor(
|
|
29
|
+
public signature: string,
|
|
30
|
+
public identity: string,
|
|
31
|
+
public signType: SignerSignType,
|
|
32
|
+
) {}
|
|
33
|
+
}
|
|
7
34
|
|
|
8
35
|
/**
|
|
9
36
|
* An abstract class representing a generic signer.
|
|
@@ -12,16 +39,47 @@ import { Num } from "../num";
|
|
|
12
39
|
export abstract class Signer {
|
|
13
40
|
constructor(protected client_: Client) {}
|
|
14
41
|
|
|
42
|
+
abstract get type(): SignerType;
|
|
43
|
+
abstract get signType(): SignerSignType;
|
|
44
|
+
|
|
15
45
|
get client(): Client {
|
|
16
46
|
return this.client_;
|
|
17
47
|
}
|
|
18
48
|
|
|
49
|
+
static async verifyMessage(
|
|
50
|
+
message: string | BytesLike,
|
|
51
|
+
signature: Signature,
|
|
52
|
+
): Promise<boolean> {
|
|
53
|
+
switch (signature.signType) {
|
|
54
|
+
case SignerSignType.EvmPersonal:
|
|
55
|
+
return verifyMessageEvmPersonal(
|
|
56
|
+
message,
|
|
57
|
+
signature.signature,
|
|
58
|
+
signature.identity,
|
|
59
|
+
);
|
|
60
|
+
case SignerSignType.BtcEcdsa:
|
|
61
|
+
return verifyMessageBtcEcdsa(
|
|
62
|
+
message,
|
|
63
|
+
signature.signature,
|
|
64
|
+
signature.identity,
|
|
65
|
+
);
|
|
66
|
+
case SignerSignType.JoyId:
|
|
67
|
+
return verifyMessageJoyId(
|
|
68
|
+
message,
|
|
69
|
+
signature.signature,
|
|
70
|
+
signature.identity,
|
|
71
|
+
);
|
|
72
|
+
case SignerSignType.Unknown:
|
|
73
|
+
throw new Error("Unknown signer sign type");
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
19
77
|
/**
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* @returns A promise that resolves the new Signer.
|
|
78
|
+
* Replace the current client.
|
|
23
79
|
*/
|
|
24
|
-
|
|
80
|
+
async replaceClient(client: Client): Promise<void> {
|
|
81
|
+
this.client_ = client;
|
|
82
|
+
}
|
|
25
83
|
|
|
26
84
|
/**
|
|
27
85
|
* Connects to the signer.
|
|
@@ -51,6 +109,15 @@ export abstract class Signer {
|
|
|
51
109
|
*/
|
|
52
110
|
abstract getInternalAddress(): Promise<string>;
|
|
53
111
|
|
|
112
|
+
/**
|
|
113
|
+
* Gets the identity for verifying signature, usually it's address
|
|
114
|
+
*
|
|
115
|
+
* @returns A promise that resolves to a string representing the identity
|
|
116
|
+
*/
|
|
117
|
+
async getIdentity(): Promise<string> {
|
|
118
|
+
return this.getInternalAddress();
|
|
119
|
+
}
|
|
120
|
+
|
|
54
121
|
/**
|
|
55
122
|
* Gets an array of Address objects associated with the signer.
|
|
56
123
|
*
|
|
@@ -101,14 +168,67 @@ export abstract class Signer {
|
|
|
101
168
|
}
|
|
102
169
|
|
|
103
170
|
/**
|
|
104
|
-
* Signs a message.
|
|
171
|
+
* Signs a message.
|
|
105
172
|
*
|
|
106
173
|
* @param _ - The message to sign, as a string or BytesLike object.
|
|
107
|
-
* @returns A promise that resolves to the
|
|
174
|
+
* @returns A promise that resolves to the signature info.
|
|
108
175
|
* @throws Will throw an error if not implemented.
|
|
109
176
|
*/
|
|
110
|
-
signMessage(
|
|
111
|
-
|
|
177
|
+
async signMessage(message: string | BytesLike): Promise<Signature> {
|
|
178
|
+
return {
|
|
179
|
+
signature: await this.signMessageRaw(message),
|
|
180
|
+
identity: await this.getIdentity(),
|
|
181
|
+
signType: this.signType,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Signs a message and returns signature only. This method is not implemented and should be overridden by subclasses.
|
|
187
|
+
*
|
|
188
|
+
* @param _ - The message to sign, as a string or BytesLike object.
|
|
189
|
+
* @returns A promise that resolves to the signature as a string.
|
|
190
|
+
* @throws Will throw an error if not implemented.
|
|
191
|
+
*/
|
|
192
|
+
signMessageRaw(_: string | BytesLike): Promise<string> {
|
|
193
|
+
throw Error("Signer.signMessageRaw not implemented");
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Verify a signature.
|
|
198
|
+
*
|
|
199
|
+
* @param _ - The original message.
|
|
200
|
+
* @param _ - The signature to verify.
|
|
201
|
+
* @returns A promise that resolves to the verification result.
|
|
202
|
+
* @throws Will throw an error if not implemented.
|
|
203
|
+
*/
|
|
204
|
+
async verifyMessage(
|
|
205
|
+
message: string | BytesLike,
|
|
206
|
+
signature: string | Signature,
|
|
207
|
+
): Promise<boolean> {
|
|
208
|
+
if (typeof signature === "string") {
|
|
209
|
+
return this.verifyMessageRaw(message, signature);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (
|
|
213
|
+
signature.identity !== (await this.getIdentity()) ||
|
|
214
|
+
![SignerSignType.Unknown, this.signType].includes(signature.signType)
|
|
215
|
+
) {
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return this.verifyMessageRaw(message, signature.signature);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Verify a string signature. This method is not implemented and should be overridden by subclasses.
|
|
224
|
+
*
|
|
225
|
+
* @param _0 - The original message.
|
|
226
|
+
* @param _1 - The signature to verify.
|
|
227
|
+
* @returns A promise that resolves to the verification result.
|
|
228
|
+
* @throws Will throw an error if not implemented.
|
|
229
|
+
*/
|
|
230
|
+
verifyMessageRaw(_0: string | BytesLike, _1: string): Promise<boolean> {
|
|
231
|
+
throw Error("Signer.verifyMessageRaw not implemented");
|
|
112
232
|
}
|
|
113
233
|
|
|
114
234
|
/**
|
|
@@ -144,7 +264,7 @@ export abstract class Signer {
|
|
|
144
264
|
}
|
|
145
265
|
|
|
146
266
|
/**
|
|
147
|
-
* Signs a transaction without
|
|
267
|
+
* Signs a transaction without preparing information for it. This method is not implemented and should be overridden by subclasses.
|
|
148
268
|
*
|
|
149
269
|
* @param _ - The transaction to sign, represented as a TransactionLike object.
|
|
150
270
|
* @returns A promise that resolves to the signed Transaction object.
|
|
@@ -155,22 +275,12 @@ export abstract class Signer {
|
|
|
155
275
|
}
|
|
156
276
|
}
|
|
157
277
|
|
|
158
|
-
/**
|
|
159
|
-
* An enumeration of signer types.
|
|
160
|
-
*/
|
|
161
|
-
export enum SignerType {
|
|
162
|
-
EVM = "EVM",
|
|
163
|
-
BTC = "BTC",
|
|
164
|
-
CKB = "CKB",
|
|
165
|
-
}
|
|
166
|
-
|
|
167
278
|
/**
|
|
168
279
|
* A class representing information about a signer, including its type and the signer instance.
|
|
169
280
|
*/
|
|
170
281
|
export class SignerInfo {
|
|
171
282
|
constructor(
|
|
172
283
|
public name: string,
|
|
173
|
-
public type: SignerType,
|
|
174
284
|
public signer: Signer,
|
|
175
285
|
) {}
|
|
176
286
|
}
|
package/src/utils/index.ts
CHANGED
|
@@ -1,21 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A type safe way to apply a transformer on a value if it's not empty.
|
|
3
|
+
*
|
|
4
|
+
* @param transformer - The transformer.
|
|
5
|
+
* @param value - The value to be transformed.
|
|
6
|
+
* @returns If the value is empty, it remains its type. Otherwise it will be transformed.
|
|
7
|
+
*/
|
|
1
8
|
export function apply<T, R>(
|
|
2
9
|
transformer: (val: T) => R,
|
|
3
10
|
value: undefined,
|
|
4
11
|
): undefined;
|
|
12
|
+
/**
|
|
13
|
+
* A type safe way to apply a transformer on a value if it's not empty.
|
|
14
|
+
*
|
|
15
|
+
* @param transformer - The transformer.
|
|
16
|
+
* @param value - The value to be transformed.
|
|
17
|
+
* @returns If the value is empty, it remains its type. Otherwise it will be transformed.
|
|
18
|
+
*/
|
|
5
19
|
export function apply<T, R>(transformer: (val: T) => R, value: null): null;
|
|
20
|
+
/**
|
|
21
|
+
* A type safe way to apply a transformer on a value if it's not empty.
|
|
22
|
+
*
|
|
23
|
+
* @param transformer - The transformer.
|
|
24
|
+
* @param value - The value to be transformed.
|
|
25
|
+
* @returns If the value is empty, it remains its type. Otherwise it will be transformed.
|
|
26
|
+
*/
|
|
6
27
|
export function apply<T, R>(transformer: (val: T) => R, value: T): R;
|
|
28
|
+
/**
|
|
29
|
+
* A type safe way to apply a transformer on a value if it's not empty.
|
|
30
|
+
*
|
|
31
|
+
* @param transformer - The transformer.
|
|
32
|
+
* @param value - The value to be transformed.
|
|
33
|
+
* @returns If the value is empty, it remains its type. Otherwise it will be transformed.
|
|
34
|
+
*/
|
|
7
35
|
export function apply<T, R>(
|
|
8
36
|
transformer: (val: T) => R,
|
|
9
37
|
value: T | undefined,
|
|
10
38
|
): R | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* A type safe way to apply a transformer on a value if it's not empty.
|
|
41
|
+
*
|
|
42
|
+
* @param transformer - The transformer.
|
|
43
|
+
* @param value - The value to be transformed.
|
|
44
|
+
* @returns If the value is empty, it remains its type. Otherwise it will be transformed.
|
|
45
|
+
*/
|
|
11
46
|
export function apply<T, R>(
|
|
12
47
|
transformer: (val: T) => R,
|
|
13
48
|
value: T | null,
|
|
14
49
|
): R | null;
|
|
50
|
+
/**
|
|
51
|
+
* A type safe way to apply a transformer on a value if it's not empty.
|
|
52
|
+
*
|
|
53
|
+
* @param transformer - The transformer.
|
|
54
|
+
* @param value - The value to be transformed.
|
|
55
|
+
* @returns If the value is empty, it remains its type. Otherwise it will be transformed.
|
|
56
|
+
*/
|
|
15
57
|
export function apply<T, R>(
|
|
16
58
|
transformer: (val: T) => R,
|
|
17
59
|
value: undefined | null,
|
|
18
60
|
): undefined | null;
|
|
61
|
+
/**
|
|
62
|
+
* A type safe way to apply a transformer on a value if it's not empty.
|
|
63
|
+
*
|
|
64
|
+
* @param transformer - The transformer.
|
|
65
|
+
* @param value - The value to be transformed.
|
|
66
|
+
* @returns If the value is empty, it remains its type. Otherwise it will be transformed.
|
|
67
|
+
*/
|
|
19
68
|
export function apply<T, R>(
|
|
20
69
|
transformer: (val: T) => R,
|
|
21
70
|
value: T | undefined | null,
|
|
@@ -26,3 +75,62 @@ export function apply<T, R>(
|
|
|
26
75
|
|
|
27
76
|
return transformer(value);
|
|
28
77
|
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Similar to Array.reduce, but the accumulator can returns Promise.
|
|
81
|
+
*
|
|
82
|
+
* @param values - The array to be reduced.
|
|
83
|
+
* @param accumulator - A callback to be called for each value. If it returns null, the previous result will be kept.
|
|
84
|
+
* @returns The accumulated result.
|
|
85
|
+
*/
|
|
86
|
+
export async function reduceAsync<T, V>(
|
|
87
|
+
values: T[],
|
|
88
|
+
accumulator: (
|
|
89
|
+
a: T,
|
|
90
|
+
b: T,
|
|
91
|
+
) => Promise<T | undefined | null | void> | T | undefined | null | void,
|
|
92
|
+
): Promise<T>;
|
|
93
|
+
/**
|
|
94
|
+
* Similar to Array.reduce, but the accumulator can returns Promise.
|
|
95
|
+
*
|
|
96
|
+
* @param values - The array to be reduced.
|
|
97
|
+
* @param accumulator - A callback to be called for each value. If it returns null, the previous result will be kept.
|
|
98
|
+
* @param init - The initial value.
|
|
99
|
+
* @returns The accumulated result.
|
|
100
|
+
*/
|
|
101
|
+
export async function reduceAsync<T, V>(
|
|
102
|
+
values: V[],
|
|
103
|
+
accumulator: (
|
|
104
|
+
a: T,
|
|
105
|
+
b: V,
|
|
106
|
+
) => Promise<T | undefined | null | void> | T | undefined | null | void,
|
|
107
|
+
init: T,
|
|
108
|
+
): Promise<T>;
|
|
109
|
+
export async function reduceAsync<T, V>(
|
|
110
|
+
values: (V | T)[],
|
|
111
|
+
accumulator: (
|
|
112
|
+
a: T,
|
|
113
|
+
b: T | V,
|
|
114
|
+
) => Promise<T | undefined | null | void> | T | undefined | null | void,
|
|
115
|
+
init?: T,
|
|
116
|
+
): Promise<T> {
|
|
117
|
+
if (init === undefined) {
|
|
118
|
+
if (values.length === 0) {
|
|
119
|
+
throw new TypeError("Reduce of empty array with no initial value");
|
|
120
|
+
}
|
|
121
|
+
init = values[0] as T;
|
|
122
|
+
values = values.slice(1);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return values.reduce(
|
|
126
|
+
(current: Promise<T>, b: T | V) =>
|
|
127
|
+
current.then((v) =>
|
|
128
|
+
Promise.resolve(accumulator(v, b)).then((r) => r ?? v),
|
|
129
|
+
),
|
|
130
|
+
Promise.resolve(init),
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function isWebview(userAgent: string): boolean {
|
|
135
|
+
return /webview|wv|ip((?!.*Safari)|(?=.*like Safari))/i.test(userAgent);
|
|
136
|
+
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"signer.d.ts","sourceRoot":"","sources":["../../src/signer/signer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACrC,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AACrC,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAC;AACtD,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,EAAE,GAAG,EAAE,MAAM,QAAQ,CAAC;AAC7B,OAAO,EAAE,GAAG,EAAE,MAAM,QAAQ,CAAC;AAE7B;;;GAGG;AACH,8BAAsB,MAAM;IACd,SAAS,CAAC,OAAO,EAAE,MAAM;gBAAf,OAAO,EAAE,MAAM;IAErC,IAAI,MAAM,IAAI,MAAM,CAEnB;IAED;;;;OAIG;IACH,QAAQ,CAAC,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAEvD;;;;OAIG;IACH,QAAQ,CAAC,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAEjC;;;;OAIG;IACG,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAEjC;;;;OAIG;IACH,QAAQ,CAAC,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;IAExC;;;;OAIG;IACH,QAAQ,CAAC,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC;IAE9C;;;;OAIG;IACH,QAAQ,CAAC,cAAc,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;IAE7C;;;;;OAKG;IACG,wBAAwB,CAAC,WAAW,CAAC,EAAE,OAA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