@meshconnect/uwc-types 0.32.0 → 0.33.0

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.
@@ -34,6 +34,17 @@ export interface EIP712TypedData {
34
34
  * @see https://eips.ethereum.org/EIPS/eip-712#definition-of-domainseparator
35
35
  */
36
36
  export declare function withEIP712Domain(typedData: EIP712TypedData): EIP712TypedData;
37
+ /**
38
+ * Wire form of a `personal_sign` message. The JSON-RPC spec types the param as
39
+ * hex `DATA`, and strict wallets hex-decode whatever arrives: Coinbase Wallet's
40
+ * in-app browser rejects raw text with `-32603` "Invalid message", and wallets
41
+ * that decode it anyway sign different bytes than the text the caller verifies.
42
+ * MetaMask and Trust Wallet hex-decode before hashing, so the EIP-191 digest is
43
+ * unchanged for them. Even-length `0x…` input passes through so a caller that
44
+ * pre-encoded is not double-encoded; odd-length `0x…` is not valid hex bytes,
45
+ * so it is UTF-8 encoded like any other text.
46
+ */
47
+ export declare function toPersonalSignData(message: string): string;
37
48
  /**
38
49
  * Standard signature format for most blockchains (EVM, Solana, etc.)
39
50
  */
@@ -1 +1 @@
1
- {"version":3,"file":"signature.d.ts","sourceRoot":"","sources":["../src/signature.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,yBAAyB,EACzB,iCAAiC,EAClC,MAAM,aAAa,CAAA;AAEpB;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC9B,MAAM,EAAE;QACN,IAAI,CAAC,EAAE,MAAM,CAAA;QACb,OAAO,CAAC,EAAE,MAAM,CAAA;QAChB,OAAO,CAAC,EAAE,MAAM,CAAA;QAChB,iBAAiB,CAAC,EAAE,MAAM,CAAA;QAC1B,IAAI,CAAC,EAAE,MAAM,CAAA;KACd,CAAA;IACD,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC,CAAA;IAC5D,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AAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAC9B;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAC9C;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAA;AAErC;;;;;GAKG;AACH,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,KAAK,CAAA;IACX,SAAS,EAAE,MAAM,CAAA;IACjB,SAAS,EAAE,MAAM,CAAA;IACjB,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,kBAAkB,CAAA;CAC5B;AAED;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG,iBAAiB,GAAG,aAAa,GAAG,YAAY,CAAA;AAE5E;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,SAAS,EAAE,aAAa,CAAA;IACxB,OAAO,EAAE,MAAM,CAAA;CAChB;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,EAAE,MAAM,CAAA;CAChB;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;;;;OAKG;IACH,WAAW,CACT,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,yBAAyB,GAAG,iCAAiC,GACtE,OAAO,CAAC,eAAe,CAAC,CAAA;CAC5B"}
package/dist/signature.js CHANGED
@@ -37,4 +37,20 @@ export function withEIP712Domain(typedData) {
37
37
  }
38
38
  };
39
39
  }
40
+ /**
41
+ * Wire form of a `personal_sign` message. The JSON-RPC spec types the param as
42
+ * hex `DATA`, and strict wallets hex-decode whatever arrives: Coinbase Wallet's
43
+ * in-app browser rejects raw text with `-32603` "Invalid message", and wallets
44
+ * that decode it anyway sign different bytes than the text the caller verifies.
45
+ * MetaMask and Trust Wallet hex-decode before hashing, so the EIP-191 digest is
46
+ * unchanged for them. Even-length `0x…` input passes through so a caller that
47
+ * pre-encoded is not double-encoded; odd-length `0x…` is not valid hex bytes,
48
+ * so it is UTF-8 encoded like any other text.
49
+ */
50
+ export function toPersonalSignData(message) {
51
+ if (/^0x(?:[0-9a-fA-F]{2})*$/.test(message))
52
+ return message;
53
+ const bytes = new TextEncoder().encode(message);
54
+ return `0x${Array.from(bytes, b => b.toString(16).padStart(2, '0')).join('')}`;
55
+ }
40
56
  //# sourceMappingURL=signature.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"signature.js","sourceRoot":"","sources":["../src/signature.ts"],"names":[],"mappings":"AAsBA;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,gBAAgB,CAAC,SAA0B;IACzD,IAAI,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,cAAc,CAAC;QAAE,OAAO,SAAS,CAAA;IAEpE,MAAM,EAAE,MAAM,EAAE,GAAG,SAAS,CAAA;IAC5B,MAAM,YAAY,GAA0C,EAAE,CAAA;IAE9D,2EAA2E;IAC3E,IAAI,MAAM,CAAC,IAAI,KAAK,SAAS;QAC3B,YAAY,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC,CAAA;IACrD,IAAI,MAAM,CAAC,OAAO,KAAK,SAAS;QAC9B,YAAY,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC,CAAA;IACxD,IAAI,MAAM,CAAC,OAAO,KAAK,SAAS;QAC9B,YAAY,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC,CAAA;IACzD,IAAI,MAAM,CAAC,iBAAiB,KAAK,SAAS;QACxC,YAAY,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,mBAAmB,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC,CAAA;IACnE,IAAI,MAAM,CAAC,IA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,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC,CAAA;IAEtD,OAAO;QACL,GAAG,SAAS;QACZ,KAAK,EAAE;YACL,YAAY,EAAE,YAAY;YAC1B,GAAG,SAAS,CAAC,KAAK;SACnB;KACF,CAAA;AACH,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,kBAAkB,CAAC,OAAe;IAChD,IAAI,yBAAyB,CAAC,IAAI,CAAC,OAAO,CAAC;QAAE,OAAO,OAAO,CAAA;IAC3D,MAAM,KAAK,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;IAC/C,OAAO,KAAK,KAAK,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,CAAA;AAChF,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meshconnect/uwc-types",
3
- "version": "0.32.0",
3
+ "version": "0.33.0",
4
4
  "description": "TypeScript types for Universal Wallet Connector",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -1,5 +1,5 @@
1
1
  import { describe, it, expect } from 'vitest'
2
- import { withEIP712Domain } from './signature'
2
+ import { toPersonalSignData, withEIP712Domain } from './signature'
3
3
  import type { EIP712TypedData } from './signature'
4
4
 
5
5
  const baseTypes = {
@@ -195,3 +195,40 @@ describe('withEIP712Domain', () => {
195
195
  expect(keys[0]).toBe('EIP712Domain')
196
196
  })
197
197
  })
198
+
199
+ const hexToUtf8 = (hex: string) =>
200
+ new TextDecoder().decode(
201
+ Uint8Array.from(hex.slice(2).match(/../g) ?? [], byte =>
202
+ Number.parseInt(byte, 16)
203
+ )
204
+ )
205
+
206
+ describe('toPersonalSignData', () => {
207
+ it('hex-encodes ASCII text', () => {
208
+ expect(toPersonalSignData('Hello')).toBe('0x48656c6c6f')
209
+ })
210
+
211
+ it('hex-encodes multi-byte UTF-8 and newlines so decoding returns the exact text', () => {
212
+ const message = 'Verify your wallet\nfor Revolut ✨ 日本'
213
+
214
+ const wire = toPersonalSignData(message)
215
+
216
+ expect(wire).toMatch(/^0x(?:[0-9a-f]{2})+$/)
217
+ expect(hexToUtf8(wire)).toBe(message)
218
+ })
219
+
220
+ it('passes through even-length 0x-hex in either case', () => {
221
+ expect(toPersonalSignData('0xdeadbeef')).toBe('0xdeadbeef')
222
+ expect(toPersonalSignData('0xDEADBEEF')).toBe('0xDEADBEEF')
223
+ })
224
+
225
+ it('UTF-8 encodes an odd-length 0x string instead of treating it as hex bytes', () => {
226
+ expect(toPersonalSignData('0xabc')).toBe('0x3078616263')
227
+ })
228
+
229
+ it('UTF-8 encodes a 0x string with non-hex characters', () => {
230
+ const message = '0xnot-hex'
231
+
232
+ expect(hexToUtf8(toPersonalSignData(message))).toBe(message)
233
+ })
234
+ })
package/src/signature.ts CHANGED
@@ -62,6 +62,22 @@ export function withEIP712Domain(typedData: EIP712TypedData): EIP712TypedData {
62
62
  }
63
63
  }
64
64
 
65
+ /**
66
+ * Wire form of a `personal_sign` message. The JSON-RPC spec types the param as
67
+ * hex `DATA`, and strict wallets hex-decode whatever arrives: Coinbase Wallet's
68
+ * in-app browser rejects raw text with `-32603` "Invalid message", and wallets
69
+ * that decode it anyway sign different bytes than the text the caller verifies.
70
+ * MetaMask and Trust Wallet hex-decode before hashing, so the EIP-191 digest is
71
+ * unchanged for them. Even-length `0x…` input passes through so a caller that
72
+ * pre-encoded is not double-encoded; odd-length `0x…` is not valid hex bytes,
73
+ * so it is UTF-8 encoded like any other text.
74
+ */
75
+ export function toPersonalSignData(message: string): string {
76
+ if (/^0x(?:[0-9a-fA-F]{2})*$/.test(message)) return message
77
+ const bytes = new TextEncoder().encode(message)
78
+ return `0x${Array.from(bytes, b => b.toString(16).padStart(2, '0')).join('')}`
79
+ }
80
+
65
81
  /**
66
82
  * Standard signature format for most blockchains (EVM, Solana, etc.)
67
83
  */