@meshconnect/uwc-types 0.32.0 → 0.33.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/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/signature.d.ts +11 -0
- package/dist/signature.d.ts.map +1 -1
- package/dist/signature.js +16 -0
- package/dist/signature.js.map +1 -1
- package/dist/solana-transaction.d.ts +40 -0
- package/dist/solana-transaction.d.ts.map +1 -0
- package/dist/solana-transaction.js +80 -0
- package/dist/solana-transaction.js.map +1 -0
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/signature.test.ts +38 -1
- package/src/signature.ts +16 -0
- package/src/solana-transaction.test.ts +166 -0
- package/src/solana-transaction.ts +108 -0
package/dist/index.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ export * from './signature';
|
|
|
13
13
|
export * from './transactions';
|
|
14
14
|
export * from './errors';
|
|
15
15
|
export * from './solana-message';
|
|
16
|
+
export * from './solana-transaction';
|
|
16
17
|
export * from './observability';
|
|
17
18
|
export * from './bounded-dedup';
|
|
18
19
|
export * from './safe-error-message';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,eAAe,CAAA;AAC7B,cAAc,mBAAmB,CAAA;AACjC,cAAc,WAAW,CAAA;AACzB,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,4BAA4B,CAAA;AAC1C,cAAc,kBAAkB,CAAA;AAChC,cAAc,WAAW,CAAA;AACzB,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA;AAC3B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,UAAU,CAAA;AACxB,cAAc,kBAAkB,CAAA;AAChC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,sBAAsB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,eAAe,CAAA;AAC7B,cAAc,mBAAmB,CAAA;AACjC,cAAc,WAAW,CAAA;AACzB,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,4BAA4B,CAAA;AAC1C,cAAc,kBAAkB,CAAA;AAChC,cAAc,WAAW,CAAA;AACzB,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA;AAC3B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,UAAU,CAAA;AACxB,cAAc,kBAAkB,CAAA;AAChC,cAAc,sBAAsB,CAAA;AACpC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,sBAAsB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -13,6 +13,7 @@ export * from './signature';
|
|
|
13
13
|
export * from './transactions';
|
|
14
14
|
export * from './errors';
|
|
15
15
|
export * from './solana-message';
|
|
16
|
+
export * from './solana-transaction';
|
|
16
17
|
export * from './observability';
|
|
17
18
|
export * from './bounded-dedup';
|
|
18
19
|
export * from './safe-error-message';
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,eAAe,CAAA;AAC7B,cAAc,mBAAmB,CAAA;AACjC,cAAc,WAAW,CAAA;AACzB,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,4BAA4B,CAAA;AAC1C,cAAc,kBAAkB,CAAA;AAChC,cAAc,WAAW,CAAA;AACzB,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA;AAC3B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,UAAU,CAAA;AACxB,cAAc,kBAAkB,CAAA;AAChC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,sBAAsB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,eAAe,CAAA;AAC7B,cAAc,mBAAmB,CAAA;AACjC,cAAc,WAAW,CAAA;AACzB,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,4BAA4B,CAAA;AAC1C,cAAc,kBAAkB,CAAA;AAChC,cAAc,WAAW,CAAA;AACzB,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA;AAC3B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,UAAU,CAAA;AACxB,cAAc,kBAAkB,CAAA;AAChC,cAAc,sBAAsB,CAAA;AACpC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,sBAAsB,CAAA"}
|
package/dist/signature.d.ts
CHANGED
|
@@ -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
|
*/
|
package/dist/signature.d.ts.map
CHANGED
|
@@ -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,WAAW,EAAE,MAAM,CAAA;IACnB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CACjC;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,eAAe,GAAG,eAAe,CAyB5E;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,UAAU,CAAA;IAChB,SAAS,EAAE,MAAM,CAAA;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CAAA;IAC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;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
|
package/dist/signature.js.map
CHANGED
|
@@ -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,IAAI,KAAK,SAAS;QAC3B,YAAY,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC,CAAA;IAEtD,OAAO;QACL,GAAG,SAAS;QACZ,KAAK,EAAE;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nB;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"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { SolanaAddressLookupTable } from './transactions';
|
|
2
|
+
/**
|
|
3
|
+
* Solana transaction helpers shared by the injected and WalletConnect
|
|
4
|
+
* connectors. No @solana/web3.js: callers pass in what needs it.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Solana's maximum serialized transaction size (web3.js `PACKET_DATA_SIZE`).
|
|
8
|
+
* Signature slots are already allocated in an unsigned transaction, so signing
|
|
9
|
+
* does not change its length.
|
|
10
|
+
*/
|
|
11
|
+
export declare const SOLANA_TRANSACTION_SIZE_LIMIT = 1232;
|
|
12
|
+
/**
|
|
13
|
+
* Serialize a transaction, refusing one the network would reject for its size
|
|
14
|
+
* before any wallet is asked to sign it. web3.js only throws (`encoding
|
|
15
|
+
* overruns Uint8Array`) when the message alone is over the limit; a transaction
|
|
16
|
+
* a few bytes over still serializes and would reach the wallet.
|
|
17
|
+
*/
|
|
18
|
+
export declare function serializeWithinSizeLimit(transaction: {
|
|
19
|
+
serialize(): Uint8Array;
|
|
20
|
+
}): Uint8Array;
|
|
21
|
+
/** A lookup table in web3.js's `AddressLookupTableAccount` constructor shape. */
|
|
22
|
+
export interface ParsedLookupTable<Key> {
|
|
23
|
+
key: Key;
|
|
24
|
+
state: {
|
|
25
|
+
deactivationSlot: bigint;
|
|
26
|
+
lastExtendedSlot: number;
|
|
27
|
+
lastExtendedSlotStartIndex: number;
|
|
28
|
+
addresses: Key[];
|
|
29
|
+
authority?: Key;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Rebuild the caller's address lookup tables, addresses included, so
|
|
34
|
+
* `compileToV0Message` can swap inlined 32-byte account keys for one-byte
|
|
35
|
+
* table indexes. Without the addresses every account is inlined and a large
|
|
36
|
+
* route (a multi-hop swap) goes over the size limit. Invalid data throws an
|
|
37
|
+
* error naming the table and field.
|
|
38
|
+
*/
|
|
39
|
+
export declare function parseLookupTables<Key>(states: SolanaAddressLookupTable[], toKey: (value: string) => Key): ParsedLookupTable<Key>[];
|
|
40
|
+
//# sourceMappingURL=solana-transaction.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"solana-transaction.d.ts","sourceRoot":"","sources":["../src/solana-transaction.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,gBAAgB,CAAA;AAE9D;;;GAGG;AAEH;;;;GAIG;AACH,eAAO,MAAM,6BAA6B,OAAO,CAAA;AAQjD;;;;;GAKG;AACH,wBAAgB,wBAAwB,CAAC,WAAW,EAAE;IACpD,SAAS,IAAI,UAAU,CAAA;CACxB,GAAG,UAAU,CAiBb;AAED,iFAAiF;AACjF,MAAM,WAAW,iBAAiB,CAAC,GAAG;IACpC,GAAG,EAAE,GAAG,CAAA;IACR,KAAK,EAAE;QACL,gBAAgB,EAAE,MAAM,CAAA;QACxB,gBAAgB,EAAE,MAAM,CAAA;QACxB,0BAA0B,EAAE,MAAM,CAAA;QAClC,SAAS,EAAE,GAAG,EAAE,CAAA;QAChB,SAAS,CAAC,EAAE,GAAG,CAAA;KAChB,CAAA;CACF;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EACnC,MAAM,EAAE,wBAAwB,EAAE,EAClC,KAAK,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,GAAG,GAC5B,iBAAiB,CAAC,GAAG,CAAC,EAAE,CA2B1B"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Solana transaction helpers shared by the injected and WalletConnect
|
|
3
|
+
* connectors. No @solana/web3.js: callers pass in what needs it.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Solana's maximum serialized transaction size (web3.js `PACKET_DATA_SIZE`).
|
|
7
|
+
* Signature slots are already allocated in an unsigned transaction, so signing
|
|
8
|
+
* does not change its length.
|
|
9
|
+
*/
|
|
10
|
+
export const SOLANA_TRANSACTION_SIZE_LIMIT = 1232;
|
|
11
|
+
const tooLarge = (size) => new Error(`Solana transaction is too large to send: ${size}, over the ${SOLANA_TRANSACTION_SIZE_LIMIT}-byte limit. ` +
|
|
12
|
+
'A large route needs its address lookup tables passed in `states`.');
|
|
13
|
+
/**
|
|
14
|
+
* Serialize a transaction, refusing one the network would reject for its size
|
|
15
|
+
* before any wallet is asked to sign it. web3.js only throws (`encoding
|
|
16
|
+
* overruns Uint8Array`) when the message alone is over the limit; a transaction
|
|
17
|
+
* a few bytes over still serializes and would reach the wallet.
|
|
18
|
+
*/
|
|
19
|
+
export function serializeWithinSizeLimit(transaction) {
|
|
20
|
+
let bytes;
|
|
21
|
+
try {
|
|
22
|
+
bytes = transaction.serialize();
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
if (error instanceof RangeError &&
|
|
26
|
+
/encoding overruns/.test(error.message)) {
|
|
27
|
+
throw tooLarge('the message alone does not fit');
|
|
28
|
+
}
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
if (bytes.length > SOLANA_TRANSACTION_SIZE_LIMIT) {
|
|
32
|
+
throw tooLarge(`${bytes.length} bytes`);
|
|
33
|
+
}
|
|
34
|
+
return bytes;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Rebuild the caller's address lookup tables, addresses included, so
|
|
38
|
+
* `compileToV0Message` can swap inlined 32-byte account keys for one-byte
|
|
39
|
+
* table indexes. Without the addresses every account is inlined and a large
|
|
40
|
+
* route (a multi-hop swap) goes over the size limit. Invalid data throws an
|
|
41
|
+
* error naming the table and field.
|
|
42
|
+
*/
|
|
43
|
+
export function parseLookupTables(states, toKey) {
|
|
44
|
+
return states.map((state, tableIndex) => {
|
|
45
|
+
const label = `address lookup table ${tableIndex}`;
|
|
46
|
+
const key = (value, field) => {
|
|
47
|
+
try {
|
|
48
|
+
return toKey(value);
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
throw new Error(`Invalid ${label} ${field}: ${String(value)}`);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
if (!Array.isArray(state.addresses)) {
|
|
55
|
+
throw new Error(`Invalid ${label}: addresses must be an array`);
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
key: key(state.key, 'key'),
|
|
59
|
+
state: {
|
|
60
|
+
deactivationSlot: toSlot(state.deactivationSlot, label),
|
|
61
|
+
lastExtendedSlot: state.lastExtendedSlot,
|
|
62
|
+
lastExtendedSlotStartIndex: state.lastExtendedStartIndex,
|
|
63
|
+
addresses: state.addresses.map((address, index) => key(address, `address ${index}`)),
|
|
64
|
+
...(state.authority && { authority: key(state.authority, 'authority') })
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
// The slot crosses JSON as a number or string even though the type says
|
|
70
|
+
// bigint, so normalise it to the bigint web3.js expects. Only the string form
|
|
71
|
+
// keeps u64::MAX exact: as a JSON number it arrives as 2^64.
|
|
72
|
+
const toSlot = (value, label) => {
|
|
73
|
+
try {
|
|
74
|
+
return BigInt(value);
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
throw new Error(`Invalid ${label} deactivation slot: ${String(value)}`);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
//# sourceMappingURL=solana-transaction.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"solana-transaction.js","sourceRoot":"","sources":["../src/solana-transaction.ts"],"names":[],"mappings":"AAEA;;;GAGG;AAEH;;;;GAIG;AACH,MAAM,CAAC,MAAM,6BAA6B,GAAG,IAAI,CAAA;AAEjD,MAAM,QAAQ,GAAG,CAAC,IAAY,EAAS,EAAE,CACvC,IAAI,KAAK,CACP,4CAA4C,IAAI,cAAc,6BAA6B,eAAe;IACxG,mEAAmE,CACtE,CAAA;AAEH;;;;;GAKG;AACH,MAAM,UAAU,wBAAwB,CAAC,WAExC;IACC,IAAI,KAAiB,CAAA;IACrB,IAAI,CAAC;QACH,KAAK,GAAG,WAAW,CAAC,SAAS,EAAE,CAAA;IACjC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IACE,KAAK,YAAY,UAAU;YAC3B,mBAAmB,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,EACvC,CAAC;YACD,MAAM,QAAQ,CAAC,gCAAgC,CAAC,CAAA;QAClD,CAAC;QACD,MAAM,KAAK,CAAA;IACb,CAAC;IACD,IAAI,KAAK,CAAC,MAAM,GAAG,6BAA6B,EAAE,CAAC;QACjD,MAAM,QAAQ,CAAC,GAAG,KAAK,CAAC,MAAM,QAAQ,CAAC,CAAA;IACzC,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC;AAcD;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB,CAC/B,MAAkC,EAClC,KAA6B;IAE7B,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,UAAU,EAAE,EAAE;QACtC,MAAM,KAAK,GAAG,wBAAwB,UAAU,EAAE,CAAA;QAClD,MAAM,GAAG,GAAG,CAAC,KAAa,EAAE,KAAa,EAAO,EAAE;YAChD,IAAI,CAAC;gBACH,OAAO,KAAK,CAAC,KAAK,CAAC,CAAA;YACr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ED
|
@@ -13,6 +13,7 @@ export * from './signature'
|
|
|
13
13
|
export * from './transactions'
|
|
14
14
|
export * from './errors'
|
|
15
15
|
export * from './solana-message'
|
|
16
|
+
export * from './solana-transaction'
|
|
16
17
|
export * from './observability'
|
|
17
18
|
export * from './bounded-dedup'
|
|
18
19
|
export * from './safe-error-message'
|
package/src/signature.test.ts
CHANGED
|
@@ -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
|
*/
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
import type { SolanaAddressLookupTable } from './transactions'
|
|
3
|
+
import {
|
|
4
|
+
SOLANA_TRANSACTION_SIZE_LIMIT,
|
|
5
|
+
parseLookupTables,
|
|
6
|
+
serializeWithinSizeLimit
|
|
7
|
+
} from './solana-transaction'
|
|
8
|
+
|
|
9
|
+
const ACTIVE = BigInt('18446744073709551615')
|
|
10
|
+
|
|
11
|
+
// Stands in for `new PublicKey(value)`: tags valid keys, throws like it does.
|
|
12
|
+
const toKey = (value: string) => {
|
|
13
|
+
if (!/^key-\d+$/.test(value)) throw new Error('Invalid public key input')
|
|
14
|
+
return { key: value }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const table = (
|
|
18
|
+
over: Partial<SolanaAddressLookupTable> = {}
|
|
19
|
+
): SolanaAddressLookupTable => ({
|
|
20
|
+
key: 'key-0',
|
|
21
|
+
addresses: ['key-1', 'key-2'],
|
|
22
|
+
deactivationSlot: ACTIVE,
|
|
23
|
+
lastExtendedSlot: 280_000_000,
|
|
24
|
+
lastExtendedStartIndex: 3,
|
|
25
|
+
...over
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
describe('parseLookupTables', () => {
|
|
29
|
+
it('keeps every address, in order, in web3.js AddressLookupTableAccount shape', () => {
|
|
30
|
+
expect(parseLookupTables([table()], toKey)).toEqual([
|
|
31
|
+
{
|
|
32
|
+
key: { key: 'key-0' },
|
|
33
|
+
state: {
|
|
34
|
+
deactivationSlot: ACTIVE,
|
|
35
|
+
lastExtendedSlot: 280_000_000,
|
|
36
|
+
lastExtendedSlotStartIndex: 3,
|
|
37
|
+
addresses: [{ key: 'key-1' }, { key: 'key-2' }]
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
])
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('parses several tables', () => {
|
|
44
|
+
const parsed = parseLookupTables(
|
|
45
|
+
[table(), table({ key: 'key-9', addresses: ['key-8'] })],
|
|
46
|
+
toKey
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
expect(parsed.map(t => t.key.key)).toEqual(['key-0', 'key-9'])
|
|
50
|
+
expect(parsed[1]!.state.addresses).toEqual([{ key: 'key-8' }])
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('returns no tables for no states', () => {
|
|
54
|
+
expect(parseLookupTables([], toKey)).toEqual([])
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
it.each([
|
|
58
|
+
['a bigint', ACTIVE, ACTIVE],
|
|
59
|
+
['a number', 280_000_000, 280_000_000n],
|
|
60
|
+
['a string (keeps u64::MAX exact)', '18446744073709551615', ACTIVE],
|
|
61
|
+
// JSON.parse turns u64::MAX into 2^64. web3.js does not read the slot
|
|
62
|
+
// when compiling, so the table still applies.
|
|
63
|
+
[
|
|
64
|
+
'a JSON number (u64::MAX rounds up)',
|
|
65
|
+
JSON.parse('18446744073709551615'),
|
|
66
|
+
2n ** 64n
|
|
67
|
+
]
|
|
68
|
+
])('normalises a deactivation slot sent as %s', (_, slot, expected) => {
|
|
69
|
+
const [parsed] = parseLookupTables(
|
|
70
|
+
[table({ deactivationSlot: slot as unknown as bigint })],
|
|
71
|
+
toKey
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
expect(parsed!.state.deactivationSlot).toBe(expected)
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
it('keeps a valid authority', () => {
|
|
78
|
+
const [parsed] = parseLookupTables([table({ authority: 'key-7' })], toKey)
|
|
79
|
+
|
|
80
|
+
expect(parsed!.state.authority).toEqual({ key: 'key-7' })
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
it.each([
|
|
84
|
+
['omitted', undefined],
|
|
85
|
+
['empty', '']
|
|
86
|
+
])('leaves out an %s authority', (_, authority) => {
|
|
87
|
+
const [parsed] = parseLookupTables([table({ authority })], toKey)
|
|
88
|
+
|
|
89
|
+
expect(parsed!.state).not.toHaveProperty('authority')
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
it.each([
|
|
93
|
+
[
|
|
94
|
+
'a bad table key',
|
|
95
|
+
[table(), table({ key: 'nope' })],
|
|
96
|
+
'Invalid address lookup table 1 key: nope'
|
|
97
|
+
],
|
|
98
|
+
[
|
|
99
|
+
'a bad address, naming its index',
|
|
100
|
+
[table({ addresses: ['key-1', 'bad'] })],
|
|
101
|
+
'Invalid address lookup table 0 address 1: bad'
|
|
102
|
+
],
|
|
103
|
+
[
|
|
104
|
+
'a bad authority',
|
|
105
|
+
[table({ authority: '0x123' })],
|
|
106
|
+
'Invalid address lookup table 0 authority: 0x123'
|
|
107
|
+
],
|
|
108
|
+
[
|
|
109
|
+
'addresses that are not an array',
|
|
110
|
+
[table({ addresses: 'nope' as unknown as string[] })],
|
|
111
|
+
'Invalid address lookup table 0: addresses must be an array'
|
|
112
|
+
],
|
|
113
|
+
[
|
|
114
|
+
'a deactivation slot that is not an integer',
|
|
115
|
+
[table({ deactivationSlot: 'soon' as unknown as bigint })],
|
|
116
|
+
'Invalid address lookup table 0 deactivation slot: soon'
|
|
117
|
+
]
|
|
118
|
+
])('throws a clear error for %s', (_, states, message) => {
|
|
119
|
+
expect(() => parseLookupTables(states, toKey)).toThrow(message)
|
|
120
|
+
})
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
describe('serializeWithinSizeLimit', () => {
|
|
124
|
+
const ofLength = (length: number) => ({
|
|
125
|
+
serialize: () => new Uint8Array(length)
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
it('allows up to the limit', () => {
|
|
129
|
+
expect(SOLANA_TRANSACTION_SIZE_LIMIT).toBe(1232)
|
|
130
|
+
expect(serializeWithinSizeLimit(ofLength(1232))).toHaveLength(1232)
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
it('refuses anything over the limit, saying how big it is', () => {
|
|
134
|
+
expect(() => serializeWithinSizeLimit(ofLength(1233))).toThrow(
|
|
135
|
+
'Solana transaction is too large to send: 1233 bytes, over the 1232-byte limit. A large route needs its address lookup tables passed in `states`.'
|
|
136
|
+
)
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
it("turns web3.js's encoding overrun into the same clear error", () => {
|
|
140
|
+
const overrun = {
|
|
141
|
+
serialize: () => {
|
|
142
|
+
throw new RangeError('encoding overruns Uint8Array')
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
expect(() => serializeWithinSizeLimit(overrun)).toThrow(
|
|
147
|
+
'Solana transaction is too large to send: the message alone does not fit, over the 1232-byte limit.'
|
|
148
|
+
)
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
it.each([
|
|
152
|
+
['an unrelated error', new Error('boom')],
|
|
153
|
+
[
|
|
154
|
+
'a RangeError about something else',
|
|
155
|
+
new RangeError('offset is out of range')
|
|
156
|
+
]
|
|
157
|
+
])('rethrows %s unchanged', (_, error) => {
|
|
158
|
+
const failing = {
|
|
159
|
+
serialize: () => {
|
|
160
|
+
throw error
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
expect(() => serializeWithinSizeLimit(failing)).toThrow(error)
|
|
165
|
+
})
|
|
166
|
+
})
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import type { SolanaAddressLookupTable } from './transactions'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Solana transaction helpers shared by the injected and WalletConnect
|
|
5
|
+
* connectors. No @solana/web3.js: callers pass in what needs it.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Solana's maximum serialized transaction size (web3.js `PACKET_DATA_SIZE`).
|
|
10
|
+
* Signature slots are already allocated in an unsigned transaction, so signing
|
|
11
|
+
* does not change its length.
|
|
12
|
+
*/
|
|
13
|
+
export const SOLANA_TRANSACTION_SIZE_LIMIT = 1232
|
|
14
|
+
|
|
15
|
+
const tooLarge = (size: string): Error =>
|
|
16
|
+
new Error(
|
|
17
|
+
`Solana transaction is too large to send: ${size}, over the ${SOLANA_TRANSACTION_SIZE_LIMIT}-byte limit. ` +
|
|
18
|
+
'A large route needs its address lookup tables passed in `states`.'
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Serialize a transaction, refusing one the network would reject for its size
|
|
23
|
+
* before any wallet is asked to sign it. web3.js only throws (`encoding
|
|
24
|
+
* overruns Uint8Array`) when the message alone is over the limit; a transaction
|
|
25
|
+
* a few bytes over still serializes and would reach the wallet.
|
|
26
|
+
*/
|
|
27
|
+
export function serializeWithinSizeLimit(transaction: {
|
|
28
|
+
serialize(): Uint8Array
|
|
29
|
+
}): Uint8Array {
|
|
30
|
+
let bytes: Uint8Array
|
|
31
|
+
try {
|
|
32
|
+
bytes = transaction.serialize()
|
|
33
|
+
} catch (error) {
|
|
34
|
+
if (
|
|
35
|
+
error instanceof RangeError &&
|
|
36
|
+
/encoding overruns/.test(error.message)
|
|
37
|
+
) {
|
|
38
|
+
throw tooLarge('the message alone does not fit')
|
|
39
|
+
}
|
|
40
|
+
throw error
|
|
41
|
+
}
|
|
42
|
+
if (bytes.length > SOLANA_TRANSACTION_SIZE_LIMIT) {
|
|
43
|
+
throw tooLarge(`${bytes.length} bytes`)
|
|
44
|
+
}
|
|
45
|
+
return bytes
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** A lookup table in web3.js's `AddressLookupTableAccount` constructor shape. */
|
|
49
|
+
export interface ParsedLookupTable<Key> {
|
|
50
|
+
key: Key
|
|
51
|
+
state: {
|
|
52
|
+
deactivationSlot: bigint
|
|
53
|
+
lastExtendedSlot: number
|
|
54
|
+
lastExtendedSlotStartIndex: number
|
|
55
|
+
addresses: Key[]
|
|
56
|
+
authority?: Key
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Rebuild the caller's address lookup tables, addresses included, so
|
|
62
|
+
* `compileToV0Message` can swap inlined 32-byte account keys for one-byte
|
|
63
|
+
* table indexes. Without the addresses every account is inlined and a large
|
|
64
|
+
* route (a multi-hop swap) goes over the size limit. Invalid data throws an
|
|
65
|
+
* error naming the table and field.
|
|
66
|
+
*/
|
|
67
|
+
export function parseLookupTables<Key>(
|
|
68
|
+
states: SolanaAddressLookupTable[],
|
|
69
|
+
toKey: (value: string) => Key
|
|
70
|
+
): ParsedLookupTable<Key>[] {
|
|
71
|
+
return states.map((state, tableIndex) => {
|
|
72
|
+
const label = `address lookup table ${tableIndex}`
|
|
73
|
+
const key = (value: string, field: string): Key => {
|
|
74
|
+
try {
|
|
75
|
+
return toKey(value)
|
|
76
|
+
} catch {
|
|
77
|
+
throw new Error(`Invalid ${label} ${field}: ${String(value)}`)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (!Array.isArray(state.addresses)) {
|
|
81
|
+
throw new Error(`Invalid ${label}: addresses must be an array`)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
key: key(state.key, 'key'),
|
|
86
|
+
state: {
|
|
87
|
+
deactivationSlot: toSlot(state.deactivationSlot, label),
|
|
88
|
+
lastExtendedSlot: state.lastExtendedSlot,
|
|
89
|
+
lastExtendedSlotStartIndex: state.lastExtendedStartIndex,
|
|
90
|
+
addresses: state.addresses.map((address, index) =>
|
|
91
|
+
key(address, `address ${index}`)
|
|
92
|
+
),
|
|
93
|
+
...(state.authority && { authority: key(state.authority, 'authority') })
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// The slot crosses JSON as a number or string even though the type says
|
|
100
|
+
// bigint, so normalise it to the bigint web3.js expects. Only the string form
|
|
101
|
+
// keeps u64::MAX exact: as a JSON number it arrives as 2^64.
|
|
102
|
+
const toSlot = (value: bigint, label: string): bigint => {
|
|
103
|
+
try {
|
|
104
|
+
return BigInt(value)
|
|
105
|
+
} catch {
|
|
106
|
+
throw new Error(`Invalid ${label} deactivation slot: ${String(value)}`)
|
|
107
|
+
}
|
|
108
|
+
}
|