@anywalletkit/wallet-near 0.2.0 → 0.3.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.cts CHANGED
@@ -401,6 +401,13 @@ declare function actionKey(a: NearAction): NearActionKey;
401
401
  declare function isPlainObject(v: unknown): v is Record<string, unknown>;
402
402
  /** Function-call args as bytes: raw bytes pass through; plain objects / strings become UTF-8 JSON / UTF-8. Anything else throws. */
403
403
  declare function argsBytes(args: Uint8Array | Record<string, unknown> | string): Uint8Array;
404
+ /**
405
+ * The borsh tag of an access-key permission (0 FunctionCall, 1 FullAccess), read from a permission of
406
+ * exactly one known variant: `{ fullAccess: {} }` or `{ functionCall: { receiverId, methodNames, allowance? } }`.
407
+ * Anything else throws, so an empty, misspelled, unknown or doubled variant is never encoded as
408
+ * FullAccess. The encoder, `assertWellFormed` and the description all read this one tag (scan 2026-10-30 F9).
409
+ */
410
+ declare function accessKeyPermissionTag(permission: unknown): 0 | 1;
404
411
  declare function writeAction(w: BorshWriter, a: NearAction): void;
405
412
  declare function readAction(r: BorshReader): NearAction;
406
413
  /** Borsh bytes of an unsigned transaction. */
@@ -624,7 +631,8 @@ declare function parseSignature(signature: string): Uint8Array;
624
631
  /**
625
632
  * An object transaction has exactly the shape the encoder writes, before it is built or signed (also
626
633
  * `signBuilt`): every action one known key; a function call's `methodName` a string of the method
627
- * grammar, `args` bytes, a string or a plain object, `gas` / `deposit` bigints; every account id and
634
+ * grammar, `args` bytes, a string or a plain object, `gas` / `deposit` bigints; an AddKey permission
635
+ * exactly one known variant (scan 2026-10-30 F9); every account id and
628
636
  * method name well formed. What the policies read is then what gets encoded (scan 2026-10-27 F6, F28).
629
637
  */
630
638
  declare function assertWellFormed(tx: Pick<NearBuiltTransaction, 'receiverId' | 'actions'>): void;
@@ -648,8 +656,8 @@ declare class WalletAccountNear extends WalletAccountReadOnlyNear implements IWa
648
656
  readonly needsNetwork = true;
649
657
  /** Derivation path, or `external` for an external signer. */
650
658
  readonly path: string;
651
- /** The signer that holds the key. */
652
- readonly signer: Signer;
659
+ /** What holds the key: `seed`, `key` (one imported key) or `external`. */
660
+ readonly signerKind: Signer['kind'];
653
661
  private _disposed;
654
662
  private _readOnly;
655
663
  constructor(signer: Signer, publicKey: Uint8Array, path: string, config?: NearConfig, accountId?: string);
@@ -719,4 +727,4 @@ declare class WalletManagerNear extends WalletManager<WalletAccountNear, NearCon
719
727
  getFeeRates(): Promise<FeeRates>;
720
728
  }
721
729
 
722
- export { ACTION_TAG, BorshReader, BorshWriter, BroadcastUnknownError, CAIP2, DANGEROUS_ACTIONS, type DescribeContext, FT_TRANSFER_GAS, IMPLICIT_ACCOUNT_MIN_DEPOSIT, MAX_GAS, MessageIsTransactionError, NEAR_BASE_PATH, NEP413_PREFIX, type NearAccessKey, type NearAccountView, type NearAction, type NearAddKeyAction, type NearBuiltTransaction, type NearConfig, type NearCreateAccountAction, type NearDelegateAction, type NearDeleteAccountAction, type NearDeleteKeyAction, type NearDeployContractAction, type NearDeployGlobalContractAction, type NearFeeEstimate, type NearFetch, type NearFunctionCallAction, type NearProvider, type NearPublicKey, type NearQuote, type NearReceipt, NearRpc, NearRpcError, type NearSignature, type NearSignedDelegateAction, type NearSignedTransaction, type NearSimulation, type NearStakeAction, type NearTransaction, type NearTransferAction, type NearTransferOptions, type NearTransferResult, type NearTxDescription, type NearTxKind, type NearTxWarning, type NearTxWarningCode, type NearUseGlobalContractAction, type Nep413Payload, type Nep413Signed, ONE_YOCTO, type RawAccessKeyView, type RawAccountView, type RawFinalOutcome, STORAGE_DEPOSIT_GAS, STORAGE_PRICE_PER_BYTE, SimulationError, TGAS, type UnsafeTransactionCode, UnsafeTransactionError, WalletAccountNear, WalletAccountReadOnlyNear, WalletManagerNear, YOCTO, actionKey, argsBytes, assertWellFormed, base58ToBytes, bytesToBase58, createClient, decodeAnyTransaction, decodeSignedTransaction, decodeTransaction, WalletManagerNear as default, describeTransaction, ed25519Key, encodeSignedTransaction, encodeTransaction, failureOf, fromBase64, implicitAccountId, isDangerous, isImplicitAccountId, isPlainObject, isValidAccountId, nep413Bytes, nep413Hash, outcomeFailed, parsePublicKey, parseSignature, publicKeyOfImplicit, publicKeyToString, readAction, readPublicKey, readSignature, receiptOf, toBase64, writeAction, writePublicKey, writeSignature };
730
+ export { ACTION_TAG, BorshReader, BorshWriter, BroadcastUnknownError, CAIP2, DANGEROUS_ACTIONS, type DescribeContext, FT_TRANSFER_GAS, IMPLICIT_ACCOUNT_MIN_DEPOSIT, MAX_GAS, MessageIsTransactionError, NEAR_BASE_PATH, NEP413_PREFIX, type NearAccessKey, type NearAccountView, type NearAction, type NearAddKeyAction, type NearBuiltTransaction, type NearConfig, type NearCreateAccountAction, type NearDelegateAction, type NearDeleteAccountAction, type NearDeleteKeyAction, type NearDeployContractAction, type NearDeployGlobalContractAction, type NearFeeEstimate, type NearFetch, type NearFunctionCallAction, type NearProvider, type NearPublicKey, type NearQuote, type NearReceipt, NearRpc, NearRpcError, type NearSignature, type NearSignedDelegateAction, type NearSignedTransaction, type NearSimulation, type NearStakeAction, type NearTransaction, type NearTransferAction, type NearTransferOptions, type NearTransferResult, type NearTxDescription, type NearTxKind, type NearTxWarning, type NearTxWarningCode, type NearUseGlobalContractAction, type Nep413Payload, type Nep413Signed, ONE_YOCTO, type RawAccessKeyView, type RawAccountView, type RawFinalOutcome, STORAGE_DEPOSIT_GAS, STORAGE_PRICE_PER_BYTE, SimulationError, TGAS, type UnsafeTransactionCode, UnsafeTransactionError, WalletAccountNear, WalletAccountReadOnlyNear, WalletManagerNear, YOCTO, accessKeyPermissionTag, actionKey, argsBytes, assertWellFormed, base58ToBytes, bytesToBase58, createClient, decodeAnyTransaction, decodeSignedTransaction, decodeTransaction, WalletManagerNear as default, describeTransaction, ed25519Key, encodeSignedTransaction, encodeTransaction, failureOf, fromBase64, implicitAccountId, isDangerous, isImplicitAccountId, isPlainObject, isValidAccountId, nep413Bytes, nep413Hash, outcomeFailed, parsePublicKey, parseSignature, publicKeyOfImplicit, publicKeyToString, readAction, readPublicKey, readSignature, receiptOf, toBase64, writeAction, writePublicKey, writeSignature };
package/dist/index.d.ts CHANGED
@@ -401,6 +401,13 @@ declare function actionKey(a: NearAction): NearActionKey;
401
401
  declare function isPlainObject(v: unknown): v is Record<string, unknown>;
402
402
  /** Function-call args as bytes: raw bytes pass through; plain objects / strings become UTF-8 JSON / UTF-8. Anything else throws. */
403
403
  declare function argsBytes(args: Uint8Array | Record<string, unknown> | string): Uint8Array;
404
+ /**
405
+ * The borsh tag of an access-key permission (0 FunctionCall, 1 FullAccess), read from a permission of
406
+ * exactly one known variant: `{ fullAccess: {} }` or `{ functionCall: { receiverId, methodNames, allowance? } }`.
407
+ * Anything else throws, so an empty, misspelled, unknown or doubled variant is never encoded as
408
+ * FullAccess. The encoder, `assertWellFormed` and the description all read this one tag (scan 2026-10-30 F9).
409
+ */
410
+ declare function accessKeyPermissionTag(permission: unknown): 0 | 1;
404
411
  declare function writeAction(w: BorshWriter, a: NearAction): void;
405
412
  declare function readAction(r: BorshReader): NearAction;
406
413
  /** Borsh bytes of an unsigned transaction. */
@@ -624,7 +631,8 @@ declare function parseSignature(signature: string): Uint8Array;
624
631
  /**
625
632
  * An object transaction has exactly the shape the encoder writes, before it is built or signed (also
626
633
  * `signBuilt`): every action one known key; a function call's `methodName` a string of the method
627
- * grammar, `args` bytes, a string or a plain object, `gas` / `deposit` bigints; every account id and
634
+ * grammar, `args` bytes, a string or a plain object, `gas` / `deposit` bigints; an AddKey permission
635
+ * exactly one known variant (scan 2026-10-30 F9); every account id and
628
636
  * method name well formed. What the policies read is then what gets encoded (scan 2026-10-27 F6, F28).
629
637
  */
630
638
  declare function assertWellFormed(tx: Pick<NearBuiltTransaction, 'receiverId' | 'actions'>): void;
@@ -648,8 +656,8 @@ declare class WalletAccountNear extends WalletAccountReadOnlyNear implements IWa
648
656
  readonly needsNetwork = true;
649
657
  /** Derivation path, or `external` for an external signer. */
650
658
  readonly path: string;
651
- /** The signer that holds the key. */
652
- readonly signer: Signer;
659
+ /** What holds the key: `seed`, `key` (one imported key) or `external`. */
660
+ readonly signerKind: Signer['kind'];
653
661
  private _disposed;
654
662
  private _readOnly;
655
663
  constructor(signer: Signer, publicKey: Uint8Array, path: string, config?: NearConfig, accountId?: string);
@@ -719,4 +727,4 @@ declare class WalletManagerNear extends WalletManager<WalletAccountNear, NearCon
719
727
  getFeeRates(): Promise<FeeRates>;
720
728
  }
721
729
 
722
- export { ACTION_TAG, BorshReader, BorshWriter, BroadcastUnknownError, CAIP2, DANGEROUS_ACTIONS, type DescribeContext, FT_TRANSFER_GAS, IMPLICIT_ACCOUNT_MIN_DEPOSIT, MAX_GAS, MessageIsTransactionError, NEAR_BASE_PATH, NEP413_PREFIX, type NearAccessKey, type NearAccountView, type NearAction, type NearAddKeyAction, type NearBuiltTransaction, type NearConfig, type NearCreateAccountAction, type NearDelegateAction, type NearDeleteAccountAction, type NearDeleteKeyAction, type NearDeployContractAction, type NearDeployGlobalContractAction, type NearFeeEstimate, type NearFetch, type NearFunctionCallAction, type NearProvider, type NearPublicKey, type NearQuote, type NearReceipt, NearRpc, NearRpcError, type NearSignature, type NearSignedDelegateAction, type NearSignedTransaction, type NearSimulation, type NearStakeAction, type NearTransaction, type NearTransferAction, type NearTransferOptions, type NearTransferResult, type NearTxDescription, type NearTxKind, type NearTxWarning, type NearTxWarningCode, type NearUseGlobalContractAction, type Nep413Payload, type Nep413Signed, ONE_YOCTO, type RawAccessKeyView, type RawAccountView, type RawFinalOutcome, STORAGE_DEPOSIT_GAS, STORAGE_PRICE_PER_BYTE, SimulationError, TGAS, type UnsafeTransactionCode, UnsafeTransactionError, WalletAccountNear, WalletAccountReadOnlyNear, WalletManagerNear, YOCTO, actionKey, argsBytes, assertWellFormed, base58ToBytes, bytesToBase58, createClient, decodeAnyTransaction, decodeSignedTransaction, decodeTransaction, WalletManagerNear as default, describeTransaction, ed25519Key, encodeSignedTransaction, encodeTransaction, failureOf, fromBase64, implicitAccountId, isDangerous, isImplicitAccountId, isPlainObject, isValidAccountId, nep413Bytes, nep413Hash, outcomeFailed, parsePublicKey, parseSignature, publicKeyOfImplicit, publicKeyToString, readAction, readPublicKey, readSignature, receiptOf, toBase64, writeAction, writePublicKey, writeSignature };
730
+ export { ACTION_TAG, BorshReader, BorshWriter, BroadcastUnknownError, CAIP2, DANGEROUS_ACTIONS, type DescribeContext, FT_TRANSFER_GAS, IMPLICIT_ACCOUNT_MIN_DEPOSIT, MAX_GAS, MessageIsTransactionError, NEAR_BASE_PATH, NEP413_PREFIX, type NearAccessKey, type NearAccountView, type NearAction, type NearAddKeyAction, type NearBuiltTransaction, type NearConfig, type NearCreateAccountAction, type NearDelegateAction, type NearDeleteAccountAction, type NearDeleteKeyAction, type NearDeployContractAction, type NearDeployGlobalContractAction, type NearFeeEstimate, type NearFetch, type NearFunctionCallAction, type NearProvider, type NearPublicKey, type NearQuote, type NearReceipt, NearRpc, NearRpcError, type NearSignature, type NearSignedDelegateAction, type NearSignedTransaction, type NearSimulation, type NearStakeAction, type NearTransaction, type NearTransferAction, type NearTransferOptions, type NearTransferResult, type NearTxDescription, type NearTxKind, type NearTxWarning, type NearTxWarningCode, type NearUseGlobalContractAction, type Nep413Payload, type Nep413Signed, ONE_YOCTO, type RawAccessKeyView, type RawAccountView, type RawFinalOutcome, STORAGE_DEPOSIT_GAS, STORAGE_PRICE_PER_BYTE, SimulationError, TGAS, type UnsafeTransactionCode, UnsafeTransactionError, WalletAccountNear, WalletAccountReadOnlyNear, WalletManagerNear, YOCTO, accessKeyPermissionTag, actionKey, argsBytes, assertWellFormed, base58ToBytes, bytesToBase58, createClient, decodeAnyTransaction, decodeSignedTransaction, decodeTransaction, WalletManagerNear as default, describeTransaction, ed25519Key, encodeSignedTransaction, encodeTransaction, failureOf, fromBase64, implicitAccountId, isDangerous, isImplicitAccountId, isPlainObject, isValidAccountId, nep413Bytes, nep413Hash, outcomeFailed, parsePublicKey, parseSignature, publicKeyOfImplicit, publicKeyToString, readAction, readPublicKey, readSignature, receiptOf, toBase64, writeAction, writePublicKey, writeSignature };
package/dist/index.js CHANGED
@@ -239,17 +239,34 @@ function argsBytes(args) {
239
239
  if (!isPlainObject(args)) throw new TypeError("function-call args must be bytes, a string or a plain object");
240
240
  return utf8.encode(JSON.stringify(args));
241
241
  }
242
+ var FUNCTION_CALL_PERMISSION_KEYS = /* @__PURE__ */ new Set(["allowance", "receiverId", "methodNames"]);
243
+ function accessKeyPermissionTag(permission) {
244
+ const keys = isPlainObject(permission) ? Object.keys(permission) : [];
245
+ if (keys.length !== 1) throw new TypeError(`an access-key permission must have exactly one variant, fullAccess or functionCall (has: ${keys.join(", ") || "none"})`);
246
+ const variant = permission[keys[0]];
247
+ if (keys[0] === "fullAccess") {
248
+ if (!isPlainObject(variant)) throw new TypeError("fullAccess must be a plain object");
249
+ return 1;
250
+ }
251
+ if (keys[0] === "functionCall") {
252
+ if (!isPlainObject(variant) || Object.keys(variant).some((k) => !FUNCTION_CALL_PERMISSION_KEYS.has(k))) throw new TypeError("functionCall must be a plain { receiverId, methodNames, allowance? } object");
253
+ if (typeof variant.receiverId !== "string") throw new TypeError("functionCall.receiverId must be a string");
254
+ if (!Array.isArray(variant.methodNames) || !variant.methodNames.every((m) => typeof m === "string")) throw new TypeError("functionCall.methodNames must be an array of strings");
255
+ if (variant.allowance !== void 0 && typeof variant.allowance !== "bigint") throw new TypeError("functionCall.allowance must be a bigint or absent");
256
+ return 0;
257
+ }
258
+ throw new TypeError(`unknown access-key permission ${JSON.stringify(keys[0])}`);
259
+ }
242
260
  function writeAccessKey(w, k) {
243
261
  w.u64(k.nonce);
244
- if ("functionCall" in k.permission) {
262
+ const tag = accessKeyPermissionTag(k.permission);
263
+ w.u8(tag);
264
+ if (tag === 0) {
245
265
  const p = k.permission.functionCall;
246
- w.u8(0);
247
266
  w.option(p.allowance, (a) => w.u128(a));
248
267
  w.string(p.receiverId);
249
268
  w.u32(p.methodNames.length);
250
269
  for (const m of p.methodNames) w.string(m);
251
- } else {
252
- w.u8(1);
253
270
  }
254
271
  }
255
272
  function readAccessKey(r) {
@@ -489,8 +506,15 @@ function describeTransaction(tx, ctx = {}) {
489
506
  if (DANGEROUS_ACTIONS.has(key)) {
490
507
  dangerous ??= KIND_OF_DANGEROUS[key];
491
508
  warnings.push({ code: "dangerous-action", message: `${key} changes the account, its keys or its code.` });
492
- if (key === "addKey" && "fullAccess" in a.addKey.accessKey.permission) {
493
- warnings.push({ code: "full-access-key", message: "Adds a FULL ACCESS key: whoever holds it owns the account." });
509
+ if (key === "addKey") {
510
+ let tag;
511
+ try {
512
+ tag = accessKeyPermissionTag(a.addKey?.accessKey?.permission);
513
+ } catch {
514
+ tag = void 0;
515
+ }
516
+ if (tag === 1) warnings.push({ code: "full-access-key", message: "Adds a FULL ACCESS key: whoever holds it owns the account." });
517
+ else if (tag === void 0) warnings.push({ code: "dangerous-action", message: "Malformed access-key permission: neither fullAccess nor functionCall. The wallet refuses to sign it." });
494
518
  }
495
519
  continue;
496
520
  }
@@ -888,6 +912,15 @@ function assertShape(actions, depth = 0) {
888
912
  if (!(f.args instanceof Uint8Array) && typeof f.args !== "string" && !isPlainObject(f.args)) throw new UnsafeTransactionError("malformed-action", "Function-call args must be bytes, a string or a plain object.");
889
913
  if (f.gas !== void 0 && typeof f.gas !== "bigint" || f.deposit !== void 0 && typeof f.deposit !== "bigint") throw new UnsafeTransactionError("malformed-action", "Function-call gas and deposit must be bigints.");
890
914
  }
915
+ if ("addKey" in a) {
916
+ const k = a.addKey;
917
+ if (!isPlainObject(k) || !isPlainObject(k.accessKey)) throw new UnsafeTransactionError("malformed-action", "addKey must carry an accessKey object.");
918
+ try {
919
+ accessKeyPermissionTag(k.accessKey.permission);
920
+ } catch (e) {
921
+ throw new UnsafeTransactionError("malformed-action", `AddKey: ${e.message}.`);
922
+ }
923
+ }
891
924
  if ("signedDelegate" in a && depth === 0) assertShape(a.signedDelegate.delegateAction.actions, depth + 1);
892
925
  }
893
926
  }
@@ -950,13 +983,20 @@ var WalletAccountNear = class _WalletAccountNear extends WalletAccountReadOnlyNe
950
983
  needsNetwork = true;
951
984
  /** Derivation path, or `external` for an external signer. */
952
985
  path;
953
- /** The signer that holds the key. */
954
- signer;
986
+ /**
987
+ * The signer that holds the key, in a private field: not a property of the account, so no code
988
+ * the account is handed to (a protocol module, a logger) can reach it and sign around the
989
+ * account's checks (audit 2026-10-29 A1).
990
+ */
991
+ #signer;
992
+ /** What holds the key: `seed`, `key` (one imported key) or `external`. */
993
+ signerKind;
955
994
  _disposed = false;
956
995
  _readOnly;
957
996
  constructor(signer, publicKey, path, config = {}, accountId) {
958
997
  super(accountId ?? implicitAccountId(publicKey), config, ed25519Key(publicKey));
959
- this.signer = requireCurve(signer, "ed25519");
998
+ this.#signer = requireCurve(signer, "ed25519");
999
+ this.signerKind = this.#signer.kind;
960
1000
  this.path = path;
961
1001
  }
962
1002
  /** Wrap a raw derived key (takes ownership; zeroized on dispose). */
@@ -970,14 +1010,14 @@ var WalletAccountNear = class _WalletAccountNear extends WalletAccountReadOnlyNe
970
1010
  }
971
1011
  /** The same key bound to a named account (`alice.near`) that lists it as a full-access key. */
972
1012
  withAccountId(accountId) {
973
- return new _WalletAccountNear(this.signer, this.publicKey.data, this.path, this._config, accountId);
1013
+ return new _WalletAccountNear(this.#signer, this.publicKey.data, this.path, this._config, accountId);
974
1014
  }
975
1015
  key() {
976
1016
  if (this._disposed) throw new SignerError("This account has been disposed.");
977
1017
  }
978
1018
  async #signBytes(bytes, context) {
979
1019
  this.key();
980
- return this.signer.sign(bytes, { scheme: "ed25519", context });
1020
+ return this.#signer.sign(bytes, { scheme: "ed25519", context });
981
1021
  }
982
1022
  /**
983
1023
  * Raw ed25519 signature over the message bytes, as NEAR tooling prints it: "ed25519:<base58>".
@@ -1064,7 +1104,7 @@ var WalletAccountNear = class _WalletAccountNear extends WalletAccountReadOnlyNe
1064
1104
  /** Dispose the signer (zeroizes a local key). Idempotent. */
1065
1105
  dispose() {
1066
1106
  this._disposed = true;
1067
- this.signer.dispose();
1107
+ this.#signer.dispose();
1068
1108
  }
1069
1109
  };
1070
1110
  function decodeSigned(b64) {
@@ -1152,6 +1192,7 @@ export {
1152
1192
  WalletAccountReadOnlyNear,
1153
1193
  WalletManagerNear,
1154
1194
  YOCTO,
1195
+ accessKeyPermissionTag,
1155
1196
  actionKey,
1156
1197
  argsBytes,
1157
1198
  assertWellFormed,