@ocap/wallet 1.30.26 → 1.31.1

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/esm/index.d.mts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { CryptoMethod, DIDType, DIDTypeArg, DIDTypeStr, DidType } from "@arcblock/did";
2
+ import { WalletEnvelopeExtra } from "@ocap/mcrypto";
2
3
  import { BytesType, EncodingType } from "@ocap/util";
3
4
 
4
5
  //#region src/index.d.ts
@@ -37,6 +38,10 @@ interface WalletObject<T extends BytesType = string> {
37
38
  sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'Uint8Array'): Promise<Uint8Array>;
38
39
  sign(data: BytesType, hashBeforeSign?: boolean, encoding?: EncodingType): Promise<BytesType>;
39
40
  verify(data: BytesType, signature: BytesType, hashBeforeVerify?: boolean, extra?: any): Promise<boolean>;
41
+ signEnvelope(data: BytesType, extra: string | WalletEnvelopeExtra, hashBeforeSign?: boolean): Promise<{
42
+ signature: string;
43
+ extra: string;
44
+ }>;
40
45
  ethHash(data: string): string;
41
46
  ethSign(data: string, hashBeforeSign?: boolean): Promise<string>;
42
47
  ethVerify(data: string, signature: string, hashBeforeVerify?: boolean): Promise<boolean>;
package/esm/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { DidType, fromPublicKey as fromPublicKey$1, isCryptoMethod, toAddress, toTypeInfo } from "@arcblock/did";
2
2
  import * as JWT from "@arcblock/jwt";
3
- import { getHasher, getSigner } from "@ocap/mcrypto";
3
+ import { buildEd25519EnvelopePayload, getHasher, getSigner, normalizeChallengeHex, signEthereumEnvelope, types } from "@ocap/mcrypto";
4
4
  import { toHex, toUint8Array } from "@ocap/util";
5
5
 
6
6
  //#region src/index.ts
@@ -41,6 +41,27 @@ function Wallet(keyPair, t = "default") {
41
41
  const hash = hashBeforeVerify ? hasher(data, 1) : data;
42
42
  return signer.verify(hash, signature, keyPair.pk, extra);
43
43
  },
44
+ async signEnvelope(data, extra, hashBeforeSign = true) {
45
+ if (!keyPair.sk || !keyPair.pk) throw new Error("Cannot sign envelope without secretKey and publicKey");
46
+ const challenge = hashBeforeSign ? hasher(data, 1) : data;
47
+ const challengeHex = normalizeChallengeHex(challenge);
48
+ const extraStr = typeof extra === "string" ? extra : JSON.stringify({
49
+ ...extra,
50
+ challenge: extra.challenge ?? challengeHex
51
+ });
52
+ if (type.pk === types.KeyType.ETHEREUM || type.pk === types.KeyType.SECP256K1) return {
53
+ signature: signEthereumEnvelope(challenge, keyPair.sk, extraStr),
54
+ extra: extraStr
55
+ };
56
+ if (type.pk === types.KeyType.ED25519) {
57
+ const payload = buildEd25519EnvelopePayload(challenge, keyPair.pk, extraStr);
58
+ return {
59
+ signature: signer.sign(payload, keyPair.sk, "hex"),
60
+ extra: extraStr
61
+ };
62
+ }
63
+ throw new Error(`Wallet key type ${type.pk} does not support signEnvelope`);
64
+ },
44
65
  ethHash(data) {
45
66
  if (typeof signer.ethHash !== "function") throw new Error("ethHash is not supported by signer");
46
67
  return signer.ethHash(data);
package/lib/index.cjs CHANGED
@@ -43,6 +43,27 @@ function Wallet(keyPair, t = "default") {
43
43
  const hash = hashBeforeVerify ? hasher(data, 1) : data;
44
44
  return signer.verify(hash, signature, keyPair.pk, extra);
45
45
  },
46
+ async signEnvelope(data, extra, hashBeforeSign = true) {
47
+ if (!keyPair.sk || !keyPair.pk) throw new Error("Cannot sign envelope without secretKey and publicKey");
48
+ const challenge = hashBeforeSign ? hasher(data, 1) : data;
49
+ const challengeHex = (0, _ocap_mcrypto.normalizeChallengeHex)(challenge);
50
+ const extraStr = typeof extra === "string" ? extra : JSON.stringify({
51
+ ...extra,
52
+ challenge: extra.challenge ?? challengeHex
53
+ });
54
+ if (type.pk === _ocap_mcrypto.types.KeyType.ETHEREUM || type.pk === _ocap_mcrypto.types.KeyType.SECP256K1) return {
55
+ signature: (0, _ocap_mcrypto.signEthereumEnvelope)(challenge, keyPair.sk, extraStr),
56
+ extra: extraStr
57
+ };
58
+ if (type.pk === _ocap_mcrypto.types.KeyType.ED25519) {
59
+ const payload = (0, _ocap_mcrypto.buildEd25519EnvelopePayload)(challenge, keyPair.pk, extraStr);
60
+ return {
61
+ signature: signer.sign(payload, keyPair.sk, "hex"),
62
+ extra: extraStr
63
+ };
64
+ }
65
+ throw new Error(`Wallet key type ${type.pk} does not support signEnvelope`);
66
+ },
46
67
  ethHash(data) {
47
68
  if (typeof signer.ethHash !== "function") throw new Error("ethHash is not supported by signer");
48
69
  return signer.ethHash(data);
package/lib/index.d.cts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { CryptoMethod, DIDType, DIDTypeArg, DIDTypeStr, DidType } from "@arcblock/did";
2
+ import { WalletEnvelopeExtra } from "@ocap/mcrypto";
2
3
  import { BytesType, EncodingType } from "@ocap/util";
3
4
 
4
5
  //#region src/index.d.ts
@@ -37,6 +38,10 @@ interface WalletObject<T extends BytesType = string> {
37
38
  sign(data: BytesType, hashBeforeSign?: boolean, encoding?: 'Uint8Array'): Promise<Uint8Array>;
38
39
  sign(data: BytesType, hashBeforeSign?: boolean, encoding?: EncodingType): Promise<BytesType>;
39
40
  verify(data: BytesType, signature: BytesType, hashBeforeVerify?: boolean, extra?: any): Promise<boolean>;
41
+ signEnvelope(data: BytesType, extra: string | WalletEnvelopeExtra, hashBeforeSign?: boolean): Promise<{
42
+ signature: string;
43
+ extra: string;
44
+ }>;
40
45
  ethHash(data: string): string;
41
46
  ethSign(data: string, hashBeforeSign?: boolean): Promise<string>;
42
47
  ethVerify(data: string, signature: string, hashBeforeVerify?: boolean): Promise<boolean>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ocap/wallet",
3
- "version": "1.30.26",
3
+ "version": "1.31.1",
4
4
  "type": "module",
5
5
  "description": "Utility function to create and use an forge compatible crypto wallet",
6
6
  "keywords": [
@@ -57,9 +57,9 @@
57
57
  "url": "https://github.com/ArcBlock/blockchain/issues"
58
58
  },
59
59
  "dependencies": {
60
- "@arcblock/did": "1.30.26",
61
- "@arcblock/jwt": "1.30.26",
62
- "@ocap/mcrypto": "1.30.26",
63
- "@ocap/util": "1.30.26"
60
+ "@arcblock/did": "1.31.1",
61
+ "@arcblock/jwt": "1.31.1",
62
+ "@ocap/mcrypto": "1.31.1",
63
+ "@ocap/util": "1.31.1"
64
64
  }
65
65
  }