@liquidium/client 0.3.1 → 0.3.2

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.cts CHANGED
@@ -208,7 +208,7 @@ interface SendBtcTransactionRequest {
208
208
  * - `signPsbt` - reserved for PSBT-based actions when exposed
209
209
  */
210
210
  interface WalletAdapter {
211
- /** Signs an SDK plaintext message and returns the wallet signature. */
211
+ /** Signs an SDK plaintext message and returns the wallet signature. BTC adapters may return base64 BIP-322 or hex-encoded signature bytes. */
212
212
  signMessage?: (request: SignMessageRequest) => Promise<string>;
213
213
  /** Signs an SDK-provided BTC PSBT and returns the signed PSBT as base64. */
214
214
  signPsbt?: (request: SignPsbtRequest) => Promise<string>;
@@ -219,7 +219,7 @@ interface WalletAdapter {
219
219
  }
220
220
  /** Signature payload submitted to a sign-message action. */
221
221
  interface SignatureInfo {
222
- /** Wallet signature over the action message. */
222
+ /** Wallet signature over the action message. BTC signatures may be base64 BIP-322 or hex-encoded bytes. */
223
223
  signature: string;
224
224
  /** Chain used to produce the signature. */
225
225
  chain: Chain;
package/dist/index.d.ts CHANGED
@@ -208,7 +208,7 @@ interface SendBtcTransactionRequest {
208
208
  * - `signPsbt` - reserved for PSBT-based actions when exposed
209
209
  */
210
210
  interface WalletAdapter {
211
- /** Signs an SDK plaintext message and returns the wallet signature. */
211
+ /** Signs an SDK plaintext message and returns the wallet signature. BTC adapters may return base64 BIP-322 or hex-encoded signature bytes. */
212
212
  signMessage?: (request: SignMessageRequest) => Promise<string>;
213
213
  /** Signs an SDK-provided BTC PSBT and returns the signed PSBT as base64. */
214
214
  signPsbt?: (request: SignPsbtRequest) => Promise<string>;
@@ -219,7 +219,7 @@ interface WalletAdapter {
219
219
  }
220
220
  /** Signature payload submitted to a sign-message action. */
221
221
  interface SignatureInfo {
222
- /** Wallet signature over the action message. */
222
+ /** Wallet signature over the action message. BTC signatures may be base64 BIP-322 or hex-encoded bytes. */
223
223
  signature: string;
224
224
  /** Chain used to produce the signature. */
225
225
  chain: Chain;
package/dist/index.js CHANGED
@@ -1,7 +1,8 @@
1
- import { encodeFunctionData, createPublicClient, http, isAddress, getAddress } from 'viem';
1
+ import { encodeFunctionData, createPublicClient, http, isAddress, getAddress, isHex, bytesToHex } from 'viem';
2
2
  import { mainnet } from 'viem/chains';
3
3
  import { Actor, HttpAgent } from '@icp-sdk/core/agent';
4
4
  import { Principal } from '@icp-sdk/core/principal';
5
+ import { base64, base64nopad } from '@scure/base';
5
6
  import { idlLabelToId } from '@icp-sdk/core/candid';
6
7
  import { encodeIcrcAccount } from '@dfinity/ledger-icrc';
7
8
  import { Principal as Principal$1 } from '@dfinity/principal';
@@ -1064,17 +1065,68 @@ function executeWith(options) {
1064
1065
  }
1065
1066
  };
1066
1067
  }
1067
-
1068
- // src/core/utils/signature.ts
1068
+ var HEX_PREFIX = "0x";
1069
+ var HEX_BYTE_CHAR_LENGTH = 2;
1070
+ var BASE64_PADDING = "=";
1071
+ function normalizeWalletSignature(signature, chain) {
1072
+ switch (chain) {
1073
+ case Chain.BTC:
1074
+ return normalizeBtcSignature(signature);
1075
+ case Chain.ETH:
1076
+ return normalizeHexSignature(signature);
1077
+ }
1078
+ }
1069
1079
  function normalizeHexSignature(signature) {
1070
- if (!signature.startsWith("0x")) {
1080
+ if (!isPrefixedHex(signature)) {
1071
1081
  return signature;
1072
1082
  }
1073
- const signatureWithoutPrefix = signature.slice(2);
1074
- if (!/^[0-9a-fA-F]+$/.test(signatureWithoutPrefix)) {
1075
- return signature;
1083
+ return signature.slice(HEX_PREFIX.length);
1084
+ }
1085
+ function normalizeBtcSignature(signature) {
1086
+ const signatureWithoutPrefix = signature.startsWith(HEX_PREFIX) ? signature.slice(HEX_PREFIX.length) : signature;
1087
+ if (isHexBytes(signatureWithoutPrefix)) {
1088
+ return signatureWithoutPrefix;
1089
+ }
1090
+ if (signature.startsWith(HEX_PREFIX)) {
1091
+ throw invalidBtcSignatureFormatError();
1092
+ }
1093
+ return bytesToUnprefixedHex(decodeBase64Signature(signature));
1094
+ }
1095
+ function isHexBytes(signature) {
1096
+ return signature.length > 0 && signature.length % HEX_BYTE_CHAR_LENGTH === 0 && isHex(`${HEX_PREFIX}${signature}`);
1097
+ }
1098
+ function decodeBase64Signature(signature) {
1099
+ try {
1100
+ const bytes = decodeBase64Bytes(signature);
1101
+ if (bytes.length === 0) {
1102
+ throw invalidBtcSignatureFormatError();
1103
+ }
1104
+ return bytes;
1105
+ } catch (error) {
1106
+ if (error instanceof LiquidiumError) {
1107
+ throw error;
1108
+ }
1109
+ throw invalidBtcSignatureFormatError(error);
1110
+ }
1111
+ }
1112
+ function bytesToUnprefixedHex(bytes) {
1113
+ return normalizeHexSignature(bytesToHex(bytes));
1114
+ }
1115
+ function isPrefixedHex(signature) {
1116
+ return signature.length > HEX_PREFIX.length && isHex(signature);
1117
+ }
1118
+ function decodeBase64Bytes(signature) {
1119
+ if (signature.includes(BASE64_PADDING)) {
1120
+ return base64.decode(signature);
1076
1121
  }
1077
- return signatureWithoutPrefix;
1122
+ return base64nopad.decode(signature);
1123
+ }
1124
+ function invalidBtcSignatureFormatError(cause) {
1125
+ return new LiquidiumError(
1126
+ LiquidiumErrorCode.SIGNATURE_ERROR,
1127
+ "BTC signature must be hex or base64-encoded bytes",
1128
+ cause
1129
+ );
1078
1130
  }
1079
1131
 
1080
1132
  // src/modules/accounts/mappers.ts
@@ -1092,7 +1144,10 @@ function mapCreateAccountRequestToRegisterProfileRequest(request) {
1092
1144
  },
1093
1145
  signature_info: {
1094
1146
  Wallet: {
1095
- signature: normalizeHexSignature(request.signatureInfo.signature),
1147
+ signature: normalizeWalletSignature(
1148
+ request.signatureInfo.signature,
1149
+ request.signatureInfo.chain
1150
+ ),
1096
1151
  chain: mapAccountChainToLendingChainVariant(
1097
1152
  request.signatureInfo.chain
1098
1153
  ),
@@ -4868,7 +4923,10 @@ var LendingModule = class {
4868
4923
  },
4869
4924
  signature_info: {
4870
4925
  Wallet: {
4871
- signature: normalizeHexSignature(signatureInfo.signature),
4926
+ signature: normalizeWalletSignature(
4927
+ signatureInfo.signature,
4928
+ signatureInfo.chain
4929
+ ),
4872
4930
  chain: mapWalletChainToLendingChain(signatureInfo.chain),
4873
4931
  account: request.signerWalletAddress
4874
4932
  }
@@ -5006,7 +5064,10 @@ var LendingModule = class {
5006
5064
  },
5007
5065
  signature_info: {
5008
5066
  Wallet: {
5009
- signature: normalizeHexSignature(signatureInfo.signature),
5067
+ signature: normalizeWalletSignature(
5068
+ signatureInfo.signature,
5069
+ signatureInfo.chain
5070
+ ),
5010
5071
  chain: mapWalletChainToLendingChain(signatureInfo.chain),
5011
5072
  account: request.signerWalletAddress
5012
5073
  }