@truecarry/mcp 0.1.5 → 0.1.7
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/cli.js +809 -127
- package/dist/contracts/w5_ownable/WalletOwnable.d.ts +87 -0
- package/dist/contracts/w5_ownable/WalletOwnable.d.ts.map +1 -0
- package/dist/contracts/w5_ownable/WalletOwnable.source.d.ts +11 -0
- package/dist/contracts/w5_ownable/WalletOwnable.source.d.ts.map +1 -0
- package/dist/contracts/w5_ownable/WalletOwnableAdapter.d.ts +111 -0
- package/dist/contracts/w5_ownable/WalletOwnableAdapter.d.ts.map +1 -0
- package/dist/contracts/w5_ownable/actions.d.ts +43 -0
- package/dist/contracts/w5_ownable/actions.d.ts.map +1 -0
- package/dist/contracts/w5_ownable/index.d.ts +12 -0
- package/dist/contracts/w5_ownable/index.d.ts.map +1 -0
- package/dist/factory.d.ts.map +1 -1
- package/dist/index.cjs +1506 -2
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1504 -2
- package/dist/serverless.cjs +108356 -0
- package/dist/serverless.d.ts +38 -0
- package/dist/serverless.d.ts.map +1 -0
- package/dist/serverless.js +108355 -0
- package/dist/services/KeyManager.d.ts +57 -0
- package/dist/services/KeyManager.d.ts.map +1 -0
- package/dist/services/McpWalletService.d.ts +8 -0
- package/dist/services/McpWalletService.d.ts.map +1 -1
- package/dist/tools/dns-tools.d.ts +49 -0
- package/dist/tools/dns-tools.d.ts.map +1 -0
- package/package.json +11 -1
package/dist/index.js
CHANGED
|
@@ -2,6 +2,7 @@ import { createRequire } from "node:module";
|
|
|
2
2
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
3
3
|
import { randomFillSync } from "node:crypto";
|
|
4
4
|
import { z } from "zod";
|
|
5
|
+
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
5
6
|
|
|
6
7
|
//#region \0rolldown/runtime.js
|
|
7
8
|
var __create = Object.create;
|
|
@@ -21705,6 +21706,16 @@ function asAddressFriendly(data) {
|
|
|
21705
21706
|
} catch {}
|
|
21706
21707
|
throw new Error(`Can not convert to AddressFriendly from "${data}"`);
|
|
21707
21708
|
}
|
|
21709
|
+
function formatWalletAddress(address, isTestnet = false) {
|
|
21710
|
+
if (typeof address === "string") return import_dist$2.Address.parse(address).toString({
|
|
21711
|
+
bounceable: false,
|
|
21712
|
+
testOnly: isTestnet
|
|
21713
|
+
});
|
|
21714
|
+
return address.toString({
|
|
21715
|
+
bounceable: false,
|
|
21716
|
+
testOnly: isTestnet
|
|
21717
|
+
});
|
|
21718
|
+
}
|
|
21708
21719
|
function isValidAddress(address) {
|
|
21709
21720
|
if (typeof address !== "string") return false;
|
|
21710
21721
|
try {
|
|
@@ -21958,6 +21969,9 @@ function Base64ToBigInt(data) {
|
|
|
21958
21969
|
for (let i = 0; i < len; i++) result = (result << 8n) + BigInt(binary.charCodeAt(i));
|
|
21959
21970
|
return result;
|
|
21960
21971
|
}
|
|
21972
|
+
function HexToBigInt(data) {
|
|
21973
|
+
return BigInt(data);
|
|
21974
|
+
}
|
|
21961
21975
|
/**
|
|
21962
21976
|
* Convert hash to Uint8Array
|
|
21963
21977
|
* @param data Hash (hex string starting with 0x)
|
|
@@ -65897,6 +65911,31 @@ var EventRouter = class {
|
|
|
65897
65911
|
* LICENSE file in the root directory of this source tree.
|
|
65898
65912
|
*
|
|
65899
65913
|
*/
|
|
65914
|
+
const tonProofPrefix = "ton-proof-item-v2/";
|
|
65915
|
+
const tonConnectPrefix = "ton-connect";
|
|
65916
|
+
async function CreateTonProofMessageBytes(message) {
|
|
65917
|
+
const wc = Buffer.alloc(4);
|
|
65918
|
+
wc.writeUInt32BE(message.workchain);
|
|
65919
|
+
const ts = Buffer.alloc(8);
|
|
65920
|
+
ts.writeBigUInt64LE(BigInt(message.timestamp));
|
|
65921
|
+
const dl = Buffer.alloc(4);
|
|
65922
|
+
dl.writeUInt32LE(message.domain.lengthBytes);
|
|
65923
|
+
const messageHash = (0, import_dist$1.sha256_sync)(Buffer.concat([
|
|
65924
|
+
Buffer.from(tonProofPrefix),
|
|
65925
|
+
wc,
|
|
65926
|
+
HexToUint8Array(message.addressHash),
|
|
65927
|
+
dl,
|
|
65928
|
+
Buffer.from(message.domain.value),
|
|
65929
|
+
ts,
|
|
65930
|
+
Buffer.from(message.payload)
|
|
65931
|
+
]));
|
|
65932
|
+
const res = (0, import_dist$1.sha256_sync)(Buffer.concat([
|
|
65933
|
+
Buffer.from([255, 255]),
|
|
65934
|
+
Buffer.from(tonConnectPrefix),
|
|
65935
|
+
Buffer.from(messageHash)
|
|
65936
|
+
]));
|
|
65937
|
+
return Buffer.from(res);
|
|
65938
|
+
}
|
|
65900
65939
|
function CreateTonProofMessage({ address, domain, payload, stateInit, timestamp }) {
|
|
65901
65940
|
return {
|
|
65902
65941
|
workchain: address.workChain,
|
|
@@ -67360,6 +67399,710 @@ var MessageHandlerRegistry = class {
|
|
|
67360
67399
|
*/
|
|
67361
67400
|
const messageHandlerRegistry = new MessageHandlerRegistry();
|
|
67362
67401
|
|
|
67402
|
+
//#endregion
|
|
67403
|
+
//#region ../../node_modules/@scure/bip39/node_modules/@noble/hashes/utils.js
|
|
67404
|
+
/**
|
|
67405
|
+
* Utilities for hex, bytes, CSPRNG.
|
|
67406
|
+
* @module
|
|
67407
|
+
*/
|
|
67408
|
+
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
67409
|
+
/** Checks if something is Uint8Array. Be careful: nodejs Buffer will return true. */
|
|
67410
|
+
function isBytes(a) {
|
|
67411
|
+
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
|
|
67412
|
+
}
|
|
67413
|
+
/** Asserts something is positive integer. */
|
|
67414
|
+
function anumber(n, title = "") {
|
|
67415
|
+
if (!Number.isSafeInteger(n) || n < 0) {
|
|
67416
|
+
const prefix = title && `"${title}" `;
|
|
67417
|
+
throw new Error(`${prefix}expected integer >= 0, got ${n}`);
|
|
67418
|
+
}
|
|
67419
|
+
}
|
|
67420
|
+
/** Asserts something is Uint8Array. */
|
|
67421
|
+
function abytes(value, length, title = "") {
|
|
67422
|
+
const bytes = isBytes(value);
|
|
67423
|
+
const len = value?.length;
|
|
67424
|
+
const needsLen = length !== void 0;
|
|
67425
|
+
if (!bytes || needsLen && len !== length) {
|
|
67426
|
+
const prefix = title && `"${title}" `;
|
|
67427
|
+
const ofLen = needsLen ? ` of length ${length}` : "";
|
|
67428
|
+
const got = bytes ? `length=${len}` : `type=${typeof value}`;
|
|
67429
|
+
throw new Error(prefix + "expected Uint8Array" + ofLen + ", got " + got);
|
|
67430
|
+
}
|
|
67431
|
+
return value;
|
|
67432
|
+
}
|
|
67433
|
+
/** Asserts something is hash */
|
|
67434
|
+
function ahash(h) {
|
|
67435
|
+
if (typeof h !== "function" || typeof h.create !== "function") throw new Error("Hash must wrapped by utils.createHasher");
|
|
67436
|
+
anumber(h.outputLen);
|
|
67437
|
+
anumber(h.blockLen);
|
|
67438
|
+
}
|
|
67439
|
+
/** Asserts a hash instance has not been destroyed / finished */
|
|
67440
|
+
function aexists(instance, checkFinished = true) {
|
|
67441
|
+
if (instance.destroyed) throw new Error("Hash instance has been destroyed");
|
|
67442
|
+
if (checkFinished && instance.finished) throw new Error("Hash#digest() has already been called");
|
|
67443
|
+
}
|
|
67444
|
+
/** Asserts output is properly-sized byte array */
|
|
67445
|
+
function aoutput(out, instance) {
|
|
67446
|
+
abytes(out, void 0, "digestInto() output");
|
|
67447
|
+
const min = instance.outputLen;
|
|
67448
|
+
if (out.length < min) throw new Error("\"digestInto() output\" expected to be of length >=" + min);
|
|
67449
|
+
}
|
|
67450
|
+
/** Zeroize a byte array. Warning: JS provides no guarantees. */
|
|
67451
|
+
function clean(...arrays) {
|
|
67452
|
+
for (let i = 0; i < arrays.length; i++) arrays[i].fill(0);
|
|
67453
|
+
}
|
|
67454
|
+
/** Create DataView of an array for easy byte-level manipulation. */
|
|
67455
|
+
function createView(arr) {
|
|
67456
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
67457
|
+
}
|
|
67458
|
+
/** Is current platform little-endian? Most are. Big-Endian platform: IBM */
|
|
67459
|
+
const isLE = new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68;
|
|
67460
|
+
const hasHexBuiltin = typeof Uint8Array.from([]).toHex === "function" && typeof Uint8Array.fromHex === "function";
|
|
67461
|
+
/**
|
|
67462
|
+
* There is no setImmediate in browser and setTimeout is slow.
|
|
67463
|
+
* Call of async fn will return Promise, which will be fullfiled only on
|
|
67464
|
+
* next scheduler queue processing step and this is exactly what we need.
|
|
67465
|
+
*/
|
|
67466
|
+
const nextTick = async () => {};
|
|
67467
|
+
/** Returns control to thread each 'tick' ms to avoid blocking. */
|
|
67468
|
+
async function asyncLoop(iters, tick, cb) {
|
|
67469
|
+
let ts = Date.now();
|
|
67470
|
+
for (let i = 0; i < iters; i++) {
|
|
67471
|
+
cb(i);
|
|
67472
|
+
const diff = Date.now() - ts;
|
|
67473
|
+
if (diff >= 0 && diff < tick) continue;
|
|
67474
|
+
await /* @__PURE__ */ nextTick();
|
|
67475
|
+
ts += diff;
|
|
67476
|
+
}
|
|
67477
|
+
}
|
|
67478
|
+
/**
|
|
67479
|
+
* Converts string to bytes using UTF8 encoding.
|
|
67480
|
+
* Built-in doesn't validate input to be string: we do the check.
|
|
67481
|
+
* @example utf8ToBytes('abc') // Uint8Array.from([97, 98, 99])
|
|
67482
|
+
*/
|
|
67483
|
+
function utf8ToBytes(str) {
|
|
67484
|
+
if (typeof str !== "string") throw new Error("string expected");
|
|
67485
|
+
return new Uint8Array(new TextEncoder().encode(str));
|
|
67486
|
+
}
|
|
67487
|
+
/**
|
|
67488
|
+
* Helper for KDFs: consumes uint8array or string.
|
|
67489
|
+
* When string is passed, does utf8 decoding, using TextDecoder.
|
|
67490
|
+
*/
|
|
67491
|
+
function kdfInputToBytes(data, errorTitle = "") {
|
|
67492
|
+
if (typeof data === "string") return utf8ToBytes(data);
|
|
67493
|
+
return abytes(data, void 0, errorTitle);
|
|
67494
|
+
}
|
|
67495
|
+
/** Merges default options and passed options. */
|
|
67496
|
+
function checkOpts(defaults, opts) {
|
|
67497
|
+
if (opts !== void 0 && {}.toString.call(opts) !== "[object Object]") throw new Error("options must be object or undefined");
|
|
67498
|
+
return Object.assign(defaults, opts);
|
|
67499
|
+
}
|
|
67500
|
+
/** Creates function with outputLen, blockLen, create properties from a class constructor. */
|
|
67501
|
+
function createHasher(hashCons, info = {}) {
|
|
67502
|
+
const hashC = (msg, opts) => hashCons(opts).update(msg).digest();
|
|
67503
|
+
const tmp = hashCons(void 0);
|
|
67504
|
+
hashC.outputLen = tmp.outputLen;
|
|
67505
|
+
hashC.blockLen = tmp.blockLen;
|
|
67506
|
+
hashC.create = (opts) => hashCons(opts);
|
|
67507
|
+
Object.assign(hashC, info);
|
|
67508
|
+
return Object.freeze(hashC);
|
|
67509
|
+
}
|
|
67510
|
+
/** Creates OID opts for NIST hashes, with prefix 06 09 60 86 48 01 65 03 04 02. */
|
|
67511
|
+
const oidNist = (suffix) => ({ oid: Uint8Array.from([
|
|
67512
|
+
6,
|
|
67513
|
+
9,
|
|
67514
|
+
96,
|
|
67515
|
+
134,
|
|
67516
|
+
72,
|
|
67517
|
+
1,
|
|
67518
|
+
101,
|
|
67519
|
+
3,
|
|
67520
|
+
4,
|
|
67521
|
+
2,
|
|
67522
|
+
suffix
|
|
67523
|
+
]) });
|
|
67524
|
+
|
|
67525
|
+
//#endregion
|
|
67526
|
+
//#region ../../node_modules/@scure/bip39/node_modules/@noble/hashes/hmac.js
|
|
67527
|
+
/**
|
|
67528
|
+
* HMAC: RFC2104 message authentication code.
|
|
67529
|
+
* @module
|
|
67530
|
+
*/
|
|
67531
|
+
/** Internal class for HMAC. */
|
|
67532
|
+
var _HMAC = class {
|
|
67533
|
+
oHash;
|
|
67534
|
+
iHash;
|
|
67535
|
+
blockLen;
|
|
67536
|
+
outputLen;
|
|
67537
|
+
finished = false;
|
|
67538
|
+
destroyed = false;
|
|
67539
|
+
constructor(hash, key) {
|
|
67540
|
+
ahash(hash);
|
|
67541
|
+
abytes(key, void 0, "key");
|
|
67542
|
+
this.iHash = hash.create();
|
|
67543
|
+
if (typeof this.iHash.update !== "function") throw new Error("Expected instance of class which extends utils.Hash");
|
|
67544
|
+
this.blockLen = this.iHash.blockLen;
|
|
67545
|
+
this.outputLen = this.iHash.outputLen;
|
|
67546
|
+
const blockLen = this.blockLen;
|
|
67547
|
+
const pad = new Uint8Array(blockLen);
|
|
67548
|
+
pad.set(key.length > blockLen ? hash.create().update(key).digest() : key);
|
|
67549
|
+
for (let i = 0; i < pad.length; i++) pad[i] ^= 54;
|
|
67550
|
+
this.iHash.update(pad);
|
|
67551
|
+
this.oHash = hash.create();
|
|
67552
|
+
for (let i = 0; i < pad.length; i++) pad[i] ^= 106;
|
|
67553
|
+
this.oHash.update(pad);
|
|
67554
|
+
clean(pad);
|
|
67555
|
+
}
|
|
67556
|
+
update(buf) {
|
|
67557
|
+
aexists(this);
|
|
67558
|
+
this.iHash.update(buf);
|
|
67559
|
+
return this;
|
|
67560
|
+
}
|
|
67561
|
+
digestInto(out) {
|
|
67562
|
+
aexists(this);
|
|
67563
|
+
abytes(out, this.outputLen, "output");
|
|
67564
|
+
this.finished = true;
|
|
67565
|
+
this.iHash.digestInto(out);
|
|
67566
|
+
this.oHash.update(out);
|
|
67567
|
+
this.oHash.digestInto(out);
|
|
67568
|
+
this.destroy();
|
|
67569
|
+
}
|
|
67570
|
+
digest() {
|
|
67571
|
+
const out = new Uint8Array(this.oHash.outputLen);
|
|
67572
|
+
this.digestInto(out);
|
|
67573
|
+
return out;
|
|
67574
|
+
}
|
|
67575
|
+
_cloneInto(to) {
|
|
67576
|
+
to ||= Object.create(Object.getPrototypeOf(this), {});
|
|
67577
|
+
const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
|
|
67578
|
+
to = to;
|
|
67579
|
+
to.finished = finished;
|
|
67580
|
+
to.destroyed = destroyed;
|
|
67581
|
+
to.blockLen = blockLen;
|
|
67582
|
+
to.outputLen = outputLen;
|
|
67583
|
+
to.oHash = oHash._cloneInto(to.oHash);
|
|
67584
|
+
to.iHash = iHash._cloneInto(to.iHash);
|
|
67585
|
+
return to;
|
|
67586
|
+
}
|
|
67587
|
+
clone() {
|
|
67588
|
+
return this._cloneInto();
|
|
67589
|
+
}
|
|
67590
|
+
destroy() {
|
|
67591
|
+
this.destroyed = true;
|
|
67592
|
+
this.oHash.destroy();
|
|
67593
|
+
this.iHash.destroy();
|
|
67594
|
+
}
|
|
67595
|
+
};
|
|
67596
|
+
/**
|
|
67597
|
+
* HMAC: RFC2104 message authentication code.
|
|
67598
|
+
* @param hash - function that would be used e.g. sha256
|
|
67599
|
+
* @param key - message key
|
|
67600
|
+
* @param message - message data
|
|
67601
|
+
* @example
|
|
67602
|
+
* import { hmac } from '@noble/hashes/hmac';
|
|
67603
|
+
* import { sha256 } from '@noble/hashes/sha2';
|
|
67604
|
+
* const mac1 = hmac(sha256, 'key', 'message');
|
|
67605
|
+
*/
|
|
67606
|
+
const hmac = (hash, key, message) => new _HMAC(hash, key).update(message).digest();
|
|
67607
|
+
hmac.create = (hash, key) => new _HMAC(hash, key);
|
|
67608
|
+
|
|
67609
|
+
//#endregion
|
|
67610
|
+
//#region ../../node_modules/@scure/bip39/node_modules/@noble/hashes/pbkdf2.js
|
|
67611
|
+
/**
|
|
67612
|
+
* PBKDF (RFC 2898). Can be used to create a key from password and salt.
|
|
67613
|
+
* @module
|
|
67614
|
+
*/
|
|
67615
|
+
function pbkdf2Init(hash, _password, _salt, _opts) {
|
|
67616
|
+
ahash(hash);
|
|
67617
|
+
const { c, dkLen, asyncTick } = checkOpts({
|
|
67618
|
+
dkLen: 32,
|
|
67619
|
+
asyncTick: 10
|
|
67620
|
+
}, _opts);
|
|
67621
|
+
anumber(c, "c");
|
|
67622
|
+
anumber(dkLen, "dkLen");
|
|
67623
|
+
anumber(asyncTick, "asyncTick");
|
|
67624
|
+
if (c < 1) throw new Error("iterations (c) must be >= 1");
|
|
67625
|
+
const password = kdfInputToBytes(_password, "password");
|
|
67626
|
+
const salt = kdfInputToBytes(_salt, "salt");
|
|
67627
|
+
const DK = new Uint8Array(dkLen);
|
|
67628
|
+
const PRF = hmac.create(hash, password);
|
|
67629
|
+
return {
|
|
67630
|
+
c,
|
|
67631
|
+
dkLen,
|
|
67632
|
+
asyncTick,
|
|
67633
|
+
DK,
|
|
67634
|
+
PRF,
|
|
67635
|
+
PRFSalt: PRF._cloneInto().update(salt)
|
|
67636
|
+
};
|
|
67637
|
+
}
|
|
67638
|
+
function pbkdf2Output(PRF, PRFSalt, DK, prfW, u) {
|
|
67639
|
+
PRF.destroy();
|
|
67640
|
+
PRFSalt.destroy();
|
|
67641
|
+
if (prfW) prfW.destroy();
|
|
67642
|
+
clean(u);
|
|
67643
|
+
return DK;
|
|
67644
|
+
}
|
|
67645
|
+
/**
|
|
67646
|
+
* PBKDF2-HMAC: RFC 2898 key derivation function. Async version.
|
|
67647
|
+
* @example
|
|
67648
|
+
* await pbkdf2Async(sha256, 'password', 'salt', { dkLen: 32, c: 500_000 });
|
|
67649
|
+
*/
|
|
67650
|
+
async function pbkdf2Async(hash, password, salt, opts) {
|
|
67651
|
+
const { c, dkLen, asyncTick, DK, PRF, PRFSalt } = pbkdf2Init(hash, password, salt, opts);
|
|
67652
|
+
let prfW;
|
|
67653
|
+
const arr = new Uint8Array(4);
|
|
67654
|
+
const view = createView(arr);
|
|
67655
|
+
const u = new Uint8Array(PRF.outputLen);
|
|
67656
|
+
for (let ti = 1, pos = 0; pos < dkLen; ti++, pos += PRF.outputLen) {
|
|
67657
|
+
const Ti = DK.subarray(pos, pos + PRF.outputLen);
|
|
67658
|
+
view.setInt32(0, ti, false);
|
|
67659
|
+
(prfW = PRFSalt._cloneInto(prfW)).update(arr).digestInto(u);
|
|
67660
|
+
Ti.set(u.subarray(0, Ti.length));
|
|
67661
|
+
await asyncLoop(c - 1, asyncTick, () => {
|
|
67662
|
+
PRF._cloneInto(prfW).update(u).digestInto(u);
|
|
67663
|
+
for (let i = 0; i < Ti.length; i++) Ti[i] ^= u[i];
|
|
67664
|
+
});
|
|
67665
|
+
}
|
|
67666
|
+
return pbkdf2Output(PRF, PRFSalt, DK, prfW, u);
|
|
67667
|
+
}
|
|
67668
|
+
|
|
67669
|
+
//#endregion
|
|
67670
|
+
//#region ../../node_modules/@scure/bip39/node_modules/@noble/hashes/_md.js
|
|
67671
|
+
/**
|
|
67672
|
+
* Internal Merkle-Damgard hash utils.
|
|
67673
|
+
* @module
|
|
67674
|
+
*/
|
|
67675
|
+
/**
|
|
67676
|
+
* Merkle-Damgard hash construction base class.
|
|
67677
|
+
* Could be used to create MD5, RIPEMD, SHA1, SHA2.
|
|
67678
|
+
*/
|
|
67679
|
+
var HashMD = class {
|
|
67680
|
+
blockLen;
|
|
67681
|
+
outputLen;
|
|
67682
|
+
padOffset;
|
|
67683
|
+
isLE;
|
|
67684
|
+
buffer;
|
|
67685
|
+
view;
|
|
67686
|
+
finished = false;
|
|
67687
|
+
length = 0;
|
|
67688
|
+
pos = 0;
|
|
67689
|
+
destroyed = false;
|
|
67690
|
+
constructor(blockLen, outputLen, padOffset, isLE) {
|
|
67691
|
+
this.blockLen = blockLen;
|
|
67692
|
+
this.outputLen = outputLen;
|
|
67693
|
+
this.padOffset = padOffset;
|
|
67694
|
+
this.isLE = isLE;
|
|
67695
|
+
this.buffer = new Uint8Array(blockLen);
|
|
67696
|
+
this.view = createView(this.buffer);
|
|
67697
|
+
}
|
|
67698
|
+
update(data) {
|
|
67699
|
+
aexists(this);
|
|
67700
|
+
abytes(data);
|
|
67701
|
+
const { view, buffer, blockLen } = this;
|
|
67702
|
+
const len = data.length;
|
|
67703
|
+
for (let pos = 0; pos < len;) {
|
|
67704
|
+
const take = Math.min(blockLen - this.pos, len - pos);
|
|
67705
|
+
if (take === blockLen) {
|
|
67706
|
+
const dataView = createView(data);
|
|
67707
|
+
for (; blockLen <= len - pos; pos += blockLen) this.process(dataView, pos);
|
|
67708
|
+
continue;
|
|
67709
|
+
}
|
|
67710
|
+
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
67711
|
+
this.pos += take;
|
|
67712
|
+
pos += take;
|
|
67713
|
+
if (this.pos === blockLen) {
|
|
67714
|
+
this.process(view, 0);
|
|
67715
|
+
this.pos = 0;
|
|
67716
|
+
}
|
|
67717
|
+
}
|
|
67718
|
+
this.length += data.length;
|
|
67719
|
+
this.roundClean();
|
|
67720
|
+
return this;
|
|
67721
|
+
}
|
|
67722
|
+
digestInto(out) {
|
|
67723
|
+
aexists(this);
|
|
67724
|
+
aoutput(out, this);
|
|
67725
|
+
this.finished = true;
|
|
67726
|
+
const { buffer, view, blockLen, isLE } = this;
|
|
67727
|
+
let { pos } = this;
|
|
67728
|
+
buffer[pos++] = 128;
|
|
67729
|
+
clean(this.buffer.subarray(pos));
|
|
67730
|
+
if (this.padOffset > blockLen - pos) {
|
|
67731
|
+
this.process(view, 0);
|
|
67732
|
+
pos = 0;
|
|
67733
|
+
}
|
|
67734
|
+
for (let i = pos; i < blockLen; i++) buffer[i] = 0;
|
|
67735
|
+
view.setBigUint64(blockLen - 8, BigInt(this.length * 8), isLE);
|
|
67736
|
+
this.process(view, 0);
|
|
67737
|
+
const oview = createView(out);
|
|
67738
|
+
const len = this.outputLen;
|
|
67739
|
+
if (len % 4) throw new Error("_sha2: outputLen must be aligned to 32bit");
|
|
67740
|
+
const outLen = len / 4;
|
|
67741
|
+
const state = this.get();
|
|
67742
|
+
if (outLen > state.length) throw new Error("_sha2: outputLen bigger than state");
|
|
67743
|
+
for (let i = 0; i < outLen; i++) oview.setUint32(4 * i, state[i], isLE);
|
|
67744
|
+
}
|
|
67745
|
+
digest() {
|
|
67746
|
+
const { buffer, outputLen } = this;
|
|
67747
|
+
this.digestInto(buffer);
|
|
67748
|
+
const res = buffer.slice(0, outputLen);
|
|
67749
|
+
this.destroy();
|
|
67750
|
+
return res;
|
|
67751
|
+
}
|
|
67752
|
+
_cloneInto(to) {
|
|
67753
|
+
to ||= new this.constructor();
|
|
67754
|
+
to.set(...this.get());
|
|
67755
|
+
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
|
67756
|
+
to.destroyed = destroyed;
|
|
67757
|
+
to.finished = finished;
|
|
67758
|
+
to.length = length;
|
|
67759
|
+
to.pos = pos;
|
|
67760
|
+
if (length % blockLen) to.buffer.set(buffer);
|
|
67761
|
+
return to;
|
|
67762
|
+
}
|
|
67763
|
+
clone() {
|
|
67764
|
+
return this._cloneInto();
|
|
67765
|
+
}
|
|
67766
|
+
};
|
|
67767
|
+
/** Initial SHA512 state. Bits 0..64 of frac part of sqrt of primes 2..19 */
|
|
67768
|
+
const SHA512_IV = /* @__PURE__ */ Uint32Array.from([
|
|
67769
|
+
1779033703,
|
|
67770
|
+
4089235720,
|
|
67771
|
+
3144134277,
|
|
67772
|
+
2227873595,
|
|
67773
|
+
1013904242,
|
|
67774
|
+
4271175723,
|
|
67775
|
+
2773480762,
|
|
67776
|
+
1595750129,
|
|
67777
|
+
1359893119,
|
|
67778
|
+
2917565137,
|
|
67779
|
+
2600822924,
|
|
67780
|
+
725511199,
|
|
67781
|
+
528734635,
|
|
67782
|
+
4215389547,
|
|
67783
|
+
1541459225,
|
|
67784
|
+
327033209
|
|
67785
|
+
]);
|
|
67786
|
+
|
|
67787
|
+
//#endregion
|
|
67788
|
+
//#region ../../node_modules/@scure/bip39/node_modules/@noble/hashes/_u64.js
|
|
67789
|
+
/**
|
|
67790
|
+
* Internal helpers for u64. BigUint64Array is too slow as per 2025, so we implement it using Uint32Array.
|
|
67791
|
+
* @todo re-check https://issues.chromium.org/issues/42212588
|
|
67792
|
+
* @module
|
|
67793
|
+
*/
|
|
67794
|
+
const U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
|
|
67795
|
+
const _32n = /* @__PURE__ */ BigInt(32);
|
|
67796
|
+
function fromBig(n, le = false) {
|
|
67797
|
+
if (le) return {
|
|
67798
|
+
h: Number(n & U32_MASK64),
|
|
67799
|
+
l: Number(n >> _32n & U32_MASK64)
|
|
67800
|
+
};
|
|
67801
|
+
return {
|
|
67802
|
+
h: Number(n >> _32n & U32_MASK64) | 0,
|
|
67803
|
+
l: Number(n & U32_MASK64) | 0
|
|
67804
|
+
};
|
|
67805
|
+
}
|
|
67806
|
+
function split(lst, le = false) {
|
|
67807
|
+
const len = lst.length;
|
|
67808
|
+
let Ah = new Uint32Array(len);
|
|
67809
|
+
let Al = new Uint32Array(len);
|
|
67810
|
+
for (let i = 0; i < len; i++) {
|
|
67811
|
+
const { h, l } = fromBig(lst[i], le);
|
|
67812
|
+
[Ah[i], Al[i]] = [h, l];
|
|
67813
|
+
}
|
|
67814
|
+
return [Ah, Al];
|
|
67815
|
+
}
|
|
67816
|
+
const shrSH = (h, _l, s) => h >>> s;
|
|
67817
|
+
const shrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
67818
|
+
const rotrSH = (h, l, s) => h >>> s | l << 32 - s;
|
|
67819
|
+
const rotrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
67820
|
+
const rotrBH = (h, l, s) => h << 64 - s | l >>> s - 32;
|
|
67821
|
+
const rotrBL = (h, l, s) => h >>> s - 32 | l << 64 - s;
|
|
67822
|
+
function add(Ah, Al, Bh, Bl) {
|
|
67823
|
+
const l = (Al >>> 0) + (Bl >>> 0);
|
|
67824
|
+
return {
|
|
67825
|
+
h: Ah + Bh + (l / 2 ** 32 | 0) | 0,
|
|
67826
|
+
l: l | 0
|
|
67827
|
+
};
|
|
67828
|
+
}
|
|
67829
|
+
const add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
|
67830
|
+
const add3H = (low, Ah, Bh, Ch) => Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0;
|
|
67831
|
+
const add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
|
|
67832
|
+
const add4H = (low, Ah, Bh, Ch, Dh) => Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0;
|
|
67833
|
+
const add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
|
|
67834
|
+
const add5H = (low, Ah, Bh, Ch, Dh, Eh) => Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0;
|
|
67835
|
+
|
|
67836
|
+
//#endregion
|
|
67837
|
+
//#region ../../node_modules/@scure/bip39/node_modules/@noble/hashes/sha2.js
|
|
67838
|
+
/**
|
|
67839
|
+
* SHA2 hash function. A.k.a. sha256, sha384, sha512, sha512_224, sha512_256.
|
|
67840
|
+
* SHA256 is the fastest hash implementable in JS, even faster than Blake3.
|
|
67841
|
+
* Check out [RFC 4634](https://www.rfc-editor.org/rfc/rfc4634) and
|
|
67842
|
+
* [FIPS 180-4](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf).
|
|
67843
|
+
* @module
|
|
67844
|
+
*/
|
|
67845
|
+
const K512 = split([
|
|
67846
|
+
"0x428a2f98d728ae22",
|
|
67847
|
+
"0x7137449123ef65cd",
|
|
67848
|
+
"0xb5c0fbcfec4d3b2f",
|
|
67849
|
+
"0xe9b5dba58189dbbc",
|
|
67850
|
+
"0x3956c25bf348b538",
|
|
67851
|
+
"0x59f111f1b605d019",
|
|
67852
|
+
"0x923f82a4af194f9b",
|
|
67853
|
+
"0xab1c5ed5da6d8118",
|
|
67854
|
+
"0xd807aa98a3030242",
|
|
67855
|
+
"0x12835b0145706fbe",
|
|
67856
|
+
"0x243185be4ee4b28c",
|
|
67857
|
+
"0x550c7dc3d5ffb4e2",
|
|
67858
|
+
"0x72be5d74f27b896f",
|
|
67859
|
+
"0x80deb1fe3b1696b1",
|
|
67860
|
+
"0x9bdc06a725c71235",
|
|
67861
|
+
"0xc19bf174cf692694",
|
|
67862
|
+
"0xe49b69c19ef14ad2",
|
|
67863
|
+
"0xefbe4786384f25e3",
|
|
67864
|
+
"0x0fc19dc68b8cd5b5",
|
|
67865
|
+
"0x240ca1cc77ac9c65",
|
|
67866
|
+
"0x2de92c6f592b0275",
|
|
67867
|
+
"0x4a7484aa6ea6e483",
|
|
67868
|
+
"0x5cb0a9dcbd41fbd4",
|
|
67869
|
+
"0x76f988da831153b5",
|
|
67870
|
+
"0x983e5152ee66dfab",
|
|
67871
|
+
"0xa831c66d2db43210",
|
|
67872
|
+
"0xb00327c898fb213f",
|
|
67873
|
+
"0xbf597fc7beef0ee4",
|
|
67874
|
+
"0xc6e00bf33da88fc2",
|
|
67875
|
+
"0xd5a79147930aa725",
|
|
67876
|
+
"0x06ca6351e003826f",
|
|
67877
|
+
"0x142929670a0e6e70",
|
|
67878
|
+
"0x27b70a8546d22ffc",
|
|
67879
|
+
"0x2e1b21385c26c926",
|
|
67880
|
+
"0x4d2c6dfc5ac42aed",
|
|
67881
|
+
"0x53380d139d95b3df",
|
|
67882
|
+
"0x650a73548baf63de",
|
|
67883
|
+
"0x766a0abb3c77b2a8",
|
|
67884
|
+
"0x81c2c92e47edaee6",
|
|
67885
|
+
"0x92722c851482353b",
|
|
67886
|
+
"0xa2bfe8a14cf10364",
|
|
67887
|
+
"0xa81a664bbc423001",
|
|
67888
|
+
"0xc24b8b70d0f89791",
|
|
67889
|
+
"0xc76c51a30654be30",
|
|
67890
|
+
"0xd192e819d6ef5218",
|
|
67891
|
+
"0xd69906245565a910",
|
|
67892
|
+
"0xf40e35855771202a",
|
|
67893
|
+
"0x106aa07032bbd1b8",
|
|
67894
|
+
"0x19a4c116b8d2d0c8",
|
|
67895
|
+
"0x1e376c085141ab53",
|
|
67896
|
+
"0x2748774cdf8eeb99",
|
|
67897
|
+
"0x34b0bcb5e19b48a8",
|
|
67898
|
+
"0x391c0cb3c5c95a63",
|
|
67899
|
+
"0x4ed8aa4ae3418acb",
|
|
67900
|
+
"0x5b9cca4f7763e373",
|
|
67901
|
+
"0x682e6ff3d6b2b8a3",
|
|
67902
|
+
"0x748f82ee5defb2fc",
|
|
67903
|
+
"0x78a5636f43172f60",
|
|
67904
|
+
"0x84c87814a1f0ab72",
|
|
67905
|
+
"0x8cc702081a6439ec",
|
|
67906
|
+
"0x90befffa23631e28",
|
|
67907
|
+
"0xa4506cebde82bde9",
|
|
67908
|
+
"0xbef9a3f7b2c67915",
|
|
67909
|
+
"0xc67178f2e372532b",
|
|
67910
|
+
"0xca273eceea26619c",
|
|
67911
|
+
"0xd186b8c721c0c207",
|
|
67912
|
+
"0xeada7dd6cde0eb1e",
|
|
67913
|
+
"0xf57d4f7fee6ed178",
|
|
67914
|
+
"0x06f067aa72176fba",
|
|
67915
|
+
"0x0a637dc5a2c898a6",
|
|
67916
|
+
"0x113f9804bef90dae",
|
|
67917
|
+
"0x1b710b35131c471b",
|
|
67918
|
+
"0x28db77f523047d84",
|
|
67919
|
+
"0x32caab7b40c72493",
|
|
67920
|
+
"0x3c9ebe0a15c9bebc",
|
|
67921
|
+
"0x431d67c49c100d4c",
|
|
67922
|
+
"0x4cc5d4becb3e42b6",
|
|
67923
|
+
"0x597f299cfc657e2a",
|
|
67924
|
+
"0x5fcb6fab3ad6faec",
|
|
67925
|
+
"0x6c44198c4a475817"
|
|
67926
|
+
].map((n) => BigInt(n)));
|
|
67927
|
+
const SHA512_Kh = K512[0];
|
|
67928
|
+
const SHA512_Kl = K512[1];
|
|
67929
|
+
const SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
|
|
67930
|
+
const SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
|
|
67931
|
+
/** Internal 64-byte base SHA2 hash class. */
|
|
67932
|
+
var SHA2_64B = class extends HashMD {
|
|
67933
|
+
constructor(outputLen) {
|
|
67934
|
+
super(128, outputLen, 16, false);
|
|
67935
|
+
}
|
|
67936
|
+
get() {
|
|
67937
|
+
const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
67938
|
+
return [
|
|
67939
|
+
Ah,
|
|
67940
|
+
Al,
|
|
67941
|
+
Bh,
|
|
67942
|
+
Bl,
|
|
67943
|
+
Ch,
|
|
67944
|
+
Cl,
|
|
67945
|
+
Dh,
|
|
67946
|
+
Dl,
|
|
67947
|
+
Eh,
|
|
67948
|
+
El,
|
|
67949
|
+
Fh,
|
|
67950
|
+
Fl,
|
|
67951
|
+
Gh,
|
|
67952
|
+
Gl,
|
|
67953
|
+
Hh,
|
|
67954
|
+
Hl
|
|
67955
|
+
];
|
|
67956
|
+
}
|
|
67957
|
+
set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl) {
|
|
67958
|
+
this.Ah = Ah | 0;
|
|
67959
|
+
this.Al = Al | 0;
|
|
67960
|
+
this.Bh = Bh | 0;
|
|
67961
|
+
this.Bl = Bl | 0;
|
|
67962
|
+
this.Ch = Ch | 0;
|
|
67963
|
+
this.Cl = Cl | 0;
|
|
67964
|
+
this.Dh = Dh | 0;
|
|
67965
|
+
this.Dl = Dl | 0;
|
|
67966
|
+
this.Eh = Eh | 0;
|
|
67967
|
+
this.El = El | 0;
|
|
67968
|
+
this.Fh = Fh | 0;
|
|
67969
|
+
this.Fl = Fl | 0;
|
|
67970
|
+
this.Gh = Gh | 0;
|
|
67971
|
+
this.Gl = Gl | 0;
|
|
67972
|
+
this.Hh = Hh | 0;
|
|
67973
|
+
this.Hl = Hl | 0;
|
|
67974
|
+
}
|
|
67975
|
+
process(view, offset) {
|
|
67976
|
+
for (let i = 0; i < 16; i++, offset += 4) {
|
|
67977
|
+
SHA512_W_H[i] = view.getUint32(offset);
|
|
67978
|
+
SHA512_W_L[i] = view.getUint32(offset += 4);
|
|
67979
|
+
}
|
|
67980
|
+
for (let i = 16; i < 80; i++) {
|
|
67981
|
+
const W15h = SHA512_W_H[i - 15] | 0;
|
|
67982
|
+
const W15l = SHA512_W_L[i - 15] | 0;
|
|
67983
|
+
const s0h = rotrSH(W15h, W15l, 1) ^ rotrSH(W15h, W15l, 8) ^ shrSH(W15h, W15l, 7);
|
|
67984
|
+
const s0l = rotrSL(W15h, W15l, 1) ^ rotrSL(W15h, W15l, 8) ^ shrSL(W15h, W15l, 7);
|
|
67985
|
+
const W2h = SHA512_W_H[i - 2] | 0;
|
|
67986
|
+
const W2l = SHA512_W_L[i - 2] | 0;
|
|
67987
|
+
const s1h = rotrSH(W2h, W2l, 19) ^ rotrBH(W2h, W2l, 61) ^ shrSH(W2h, W2l, 6);
|
|
67988
|
+
const s1l = rotrSL(W2h, W2l, 19) ^ rotrBL(W2h, W2l, 61) ^ shrSL(W2h, W2l, 6);
|
|
67989
|
+
const SUMl = add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
|
|
67990
|
+
SHA512_W_H[i] = add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]) | 0;
|
|
67991
|
+
SHA512_W_L[i] = SUMl | 0;
|
|
67992
|
+
}
|
|
67993
|
+
let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
67994
|
+
for (let i = 0; i < 80; i++) {
|
|
67995
|
+
const sigma1h = rotrSH(Eh, El, 14) ^ rotrSH(Eh, El, 18) ^ rotrBH(Eh, El, 41);
|
|
67996
|
+
const sigma1l = rotrSL(Eh, El, 14) ^ rotrSL(Eh, El, 18) ^ rotrBL(Eh, El, 41);
|
|
67997
|
+
const CHIh = Eh & Fh ^ ~Eh & Gh;
|
|
67998
|
+
const CHIl = El & Fl ^ ~El & Gl;
|
|
67999
|
+
const T1ll = add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
|
|
68000
|
+
const T1h = add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
|
|
68001
|
+
const T1l = T1ll | 0;
|
|
68002
|
+
const sigma0h = rotrSH(Ah, Al, 28) ^ rotrBH(Ah, Al, 34) ^ rotrBH(Ah, Al, 39);
|
|
68003
|
+
const sigma0l = rotrSL(Ah, Al, 28) ^ rotrBL(Ah, Al, 34) ^ rotrBL(Ah, Al, 39);
|
|
68004
|
+
const MAJh = Ah & Bh ^ Ah & Ch ^ Bh & Ch;
|
|
68005
|
+
const MAJl = Al & Bl ^ Al & Cl ^ Bl & Cl;
|
|
68006
|
+
Hh = Gh | 0;
|
|
68007
|
+
Hl = Gl | 0;
|
|
68008
|
+
Gh = Fh | 0;
|
|
68009
|
+
Gl = Fl | 0;
|
|
68010
|
+
Fh = Eh | 0;
|
|
68011
|
+
Fl = El | 0;
|
|
68012
|
+
({h: Eh, l: El} = add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
|
|
68013
|
+
Dh = Ch | 0;
|
|
68014
|
+
Dl = Cl | 0;
|
|
68015
|
+
Ch = Bh | 0;
|
|
68016
|
+
Cl = Bl | 0;
|
|
68017
|
+
Bh = Ah | 0;
|
|
68018
|
+
Bl = Al | 0;
|
|
68019
|
+
const All = add3L(T1l, sigma0l, MAJl);
|
|
68020
|
+
Ah = add3H(All, T1h, sigma0h, MAJh);
|
|
68021
|
+
Al = All | 0;
|
|
68022
|
+
}
|
|
68023
|
+
({h: Ah, l: Al} = add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0));
|
|
68024
|
+
({h: Bh, l: Bl} = add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0));
|
|
68025
|
+
({h: Ch, l: Cl} = add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0));
|
|
68026
|
+
({h: Dh, l: Dl} = add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0));
|
|
68027
|
+
({h: Eh, l: El} = add(this.Eh | 0, this.El | 0, Eh | 0, El | 0));
|
|
68028
|
+
({h: Fh, l: Fl} = add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0));
|
|
68029
|
+
({h: Gh, l: Gl} = add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0));
|
|
68030
|
+
({h: Hh, l: Hl} = add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
|
|
68031
|
+
this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
|
|
68032
|
+
}
|
|
68033
|
+
roundClean() {
|
|
68034
|
+
clean(SHA512_W_H, SHA512_W_L);
|
|
68035
|
+
}
|
|
68036
|
+
destroy() {
|
|
68037
|
+
clean(this.buffer);
|
|
68038
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
68039
|
+
}
|
|
68040
|
+
};
|
|
68041
|
+
/** Internal SHA2-512 hash class. */
|
|
68042
|
+
var _SHA512 = class extends SHA2_64B {
|
|
68043
|
+
Ah = SHA512_IV[0] | 0;
|
|
68044
|
+
Al = SHA512_IV[1] | 0;
|
|
68045
|
+
Bh = SHA512_IV[2] | 0;
|
|
68046
|
+
Bl = SHA512_IV[3] | 0;
|
|
68047
|
+
Ch = SHA512_IV[4] | 0;
|
|
68048
|
+
Cl = SHA512_IV[5] | 0;
|
|
68049
|
+
Dh = SHA512_IV[6] | 0;
|
|
68050
|
+
Dl = SHA512_IV[7] | 0;
|
|
68051
|
+
Eh = SHA512_IV[8] | 0;
|
|
68052
|
+
El = SHA512_IV[9] | 0;
|
|
68053
|
+
Fh = SHA512_IV[10] | 0;
|
|
68054
|
+
Fl = SHA512_IV[11] | 0;
|
|
68055
|
+
Gh = SHA512_IV[12] | 0;
|
|
68056
|
+
Gl = SHA512_IV[13] | 0;
|
|
68057
|
+
Hh = SHA512_IV[14] | 0;
|
|
68058
|
+
Hl = SHA512_IV[15] | 0;
|
|
68059
|
+
constructor() {
|
|
68060
|
+
super(64);
|
|
68061
|
+
}
|
|
68062
|
+
};
|
|
68063
|
+
/** SHA2-512 hash function from RFC 4634. */
|
|
68064
|
+
const sha512 = /* @__PURE__ */ createHasher(() => new _SHA512(), /* @__PURE__ */ oidNist(3));
|
|
68065
|
+
|
|
68066
|
+
//#endregion
|
|
68067
|
+
//#region ../../node_modules/@scure/bip39/index.js
|
|
68068
|
+
/*! scure-bip39 - MIT License (c) 2022 Patricio Palladino, Paul Miller (paulmillr.com) */
|
|
68069
|
+
function nfkd(str) {
|
|
68070
|
+
if (typeof str !== "string") throw new TypeError("invalid mnemonic type: " + typeof str);
|
|
68071
|
+
return str.normalize("NFKD");
|
|
68072
|
+
}
|
|
68073
|
+
function normalize(str) {
|
|
68074
|
+
const norm = nfkd(str);
|
|
68075
|
+
const words = norm.split(" ");
|
|
68076
|
+
if (![
|
|
68077
|
+
12,
|
|
68078
|
+
15,
|
|
68079
|
+
18,
|
|
68080
|
+
21,
|
|
68081
|
+
24
|
|
68082
|
+
].includes(words.length)) throw new Error("Invalid mnemonic");
|
|
68083
|
+
return {
|
|
68084
|
+
nfkd: norm,
|
|
68085
|
+
words
|
|
68086
|
+
};
|
|
68087
|
+
}
|
|
68088
|
+
const psalt = (passphrase) => nfkd("mnemonic" + passphrase);
|
|
68089
|
+
/**
|
|
68090
|
+
* Irreversible: Uses KDF to derive 64 bytes of key data from mnemonic + optional password.
|
|
68091
|
+
* @param mnemonic 12-24 words
|
|
68092
|
+
* @param passphrase string that will additionally protect the key
|
|
68093
|
+
* @returns 64 bytes of key data
|
|
68094
|
+
* @example
|
|
68095
|
+
* const mnem = 'legal winner thank year wave sausage worth useful legal winner thank yellow';
|
|
68096
|
+
* await mnemonicToSeed(mnem, 'password');
|
|
68097
|
+
* // new Uint8Array([...64 bytes])
|
|
68098
|
+
*/
|
|
68099
|
+
function mnemonicToSeed(mnemonic, passphrase = "") {
|
|
68100
|
+
return pbkdf2Async(sha512, normalize(mnemonic).nfkd, psalt(passphrase), {
|
|
68101
|
+
c: 2048,
|
|
68102
|
+
dkLen: 64
|
|
68103
|
+
});
|
|
68104
|
+
}
|
|
68105
|
+
|
|
67363
68106
|
//#endregion
|
|
67364
68107
|
//#region ../walletkit/dist/esm/utils/mnemonic.js
|
|
67365
68108
|
/**
|
|
@@ -67369,6 +68112,42 @@ const messageHandlerRegistry = new MessageHandlerRegistry();
|
|
|
67369
68112
|
* LICENSE file in the root directory of this source tree.
|
|
67370
68113
|
*
|
|
67371
68114
|
*/
|
|
68115
|
+
async function bip39ToPrivateKey(mnemonic) {
|
|
68116
|
+
const seed = await mnemonicToSeed(mnemonic.join(" "));
|
|
68117
|
+
return (0, import_dist$1.keyPairFromSeed)((await (0, import_dist$1.deriveEd25519Path)(Buffer.from(seed), [
|
|
68118
|
+
44,
|
|
68119
|
+
607,
|
|
68120
|
+
0
|
|
68121
|
+
])).subarray(0, 32));
|
|
68122
|
+
}
|
|
68123
|
+
/**
|
|
68124
|
+
* Convert a mnemonic to a key pair
|
|
68125
|
+
* @param mnemonic - The mnemonic to convert, can be an array of strings or a string. 12 or 24 words
|
|
68126
|
+
* @param mnemonicType - The type of mnemonic to convert, can be 'ton' or 'bip39'
|
|
68127
|
+
* @returns The key pair
|
|
68128
|
+
*/
|
|
68129
|
+
async function MnemonicToKeyPair(mnemonic, mnemonicType = "ton") {
|
|
68130
|
+
const mnemonicArray = Array.isArray(mnemonic) ? mnemonic : mnemonic.split(" ");
|
|
68131
|
+
if (mnemonicArray.length !== 12 && mnemonicArray.length !== 24) throw new WalletKitError(ERROR_CODES.VALIDATION_ERROR, `Invalid mnemonic length: expected 12 or 24 words, got ${mnemonicArray.length}`);
|
|
68132
|
+
if (mnemonicType === "ton") {
|
|
68133
|
+
const key = await (0, import_dist$1.mnemonicToWalletKey)(mnemonicArray);
|
|
68134
|
+
return {
|
|
68135
|
+
publicKey: new Uint8Array(key.publicKey),
|
|
68136
|
+
secretKey: new Uint8Array(key.secretKey)
|
|
68137
|
+
};
|
|
68138
|
+
}
|
|
68139
|
+
if (mnemonicType === "bip39") {
|
|
68140
|
+
const key = await bip39ToPrivateKey(mnemonicArray);
|
|
68141
|
+
return {
|
|
68142
|
+
publicKey: new Uint8Array(key.publicKey),
|
|
68143
|
+
secretKey: new Uint8Array(key.secretKey)
|
|
68144
|
+
};
|
|
68145
|
+
}
|
|
68146
|
+
throw new WalletKitError(ERROR_CODES.VALIDATION_ERROR, `Invalid mnemonic type: expected "ton" or "bip39", got "${mnemonicType}"`, void 0, {
|
|
68147
|
+
receivedType: mnemonicType,
|
|
68148
|
+
supportedTypes: ["ton", "bip39"]
|
|
68149
|
+
});
|
|
68150
|
+
}
|
|
67372
68151
|
|
|
67373
68152
|
//#endregion
|
|
67374
68153
|
//#region ../walletkit/dist/esm/utils/sign.js
|
|
@@ -67379,7 +68158,20 @@ const messageHandlerRegistry = new MessageHandlerRegistry();
|
|
|
67379
68158
|
* LICENSE file in the root directory of this source tree.
|
|
67380
68159
|
*
|
|
67381
68160
|
*/
|
|
68161
|
+
function DefaultSignature(data, privateKey) {
|
|
68162
|
+
let fullKey = privateKey;
|
|
68163
|
+
if (fullKey.length === 32) fullKey = (0, import_dist$1.keyPairFromSeed)(Buffer.from(fullKey)).secretKey;
|
|
68164
|
+
return Uint8ArrayToHex((0, import_dist$1.sign)(Buffer.from(Uint8Array.from(data)), Buffer.from(fullKey)));
|
|
68165
|
+
}
|
|
68166
|
+
function createWalletSigner(privateKey) {
|
|
68167
|
+
return async (data) => {
|
|
68168
|
+
return DefaultSignature(Uint8Array.from(data), privateKey);
|
|
68169
|
+
};
|
|
68170
|
+
}
|
|
67382
68171
|
const fakeKeyPair = (0, import_dist$1.keyPairFromSeed)(Buffer.alloc(32, 0));
|
|
68172
|
+
function FakeSignature(data) {
|
|
68173
|
+
return Uint8ArrayToHex([...(0, import_dist$1.sign)(Buffer.from(Uint8Array.from(data)), Buffer.from(fakeKeyPair.secretKey))]);
|
|
68174
|
+
}
|
|
67383
68175
|
|
|
67384
68176
|
//#endregion
|
|
67385
68177
|
//#region ../walletkit/dist/esm/utils/Signer.js
|
|
@@ -67390,6 +68182,37 @@ const fakeKeyPair = (0, import_dist$1.keyPairFromSeed)(Buffer.alloc(32, 0));
|
|
|
67390
68182
|
* LICENSE file in the root directory of this source tree.
|
|
67391
68183
|
*
|
|
67392
68184
|
*/
|
|
68185
|
+
/**
|
|
68186
|
+
* Utility class for creating wallet signers from various sources
|
|
68187
|
+
*/
|
|
68188
|
+
var Signer = class {
|
|
68189
|
+
/**
|
|
68190
|
+
* Create a signer from a mnemonic phrase
|
|
68191
|
+
* @param mnemonic - Mnemonic phrase as string or array of words
|
|
68192
|
+
* @param options - Optional configuration for mnemonic type
|
|
68193
|
+
* @returns Signer function with publicKey property
|
|
68194
|
+
*/
|
|
68195
|
+
static async fromMnemonic(mnemonic, options) {
|
|
68196
|
+
const keyPair = await MnemonicToKeyPair(mnemonic, options?.type ?? "ton");
|
|
68197
|
+
return {
|
|
68198
|
+
sign: createWalletSigner(keyPair.secretKey),
|
|
68199
|
+
publicKey: Uint8ArrayToHex(keyPair.publicKey)
|
|
68200
|
+
};
|
|
68201
|
+
}
|
|
68202
|
+
/**
|
|
68203
|
+
* Create a signer from a private key
|
|
68204
|
+
* @param privateKey - Private key as hex string or Uint8Array
|
|
68205
|
+
* @returns Signer function with publicKey property
|
|
68206
|
+
*/
|
|
68207
|
+
static async fromPrivateKey(privateKey) {
|
|
68208
|
+
const privateKeyBytes = typeof privateKey === "string" ? Uint8Array.from(Buffer.from(privateKey.replace("0x", ""), "hex")) : privateKey;
|
|
68209
|
+
const keyPair = (0, import_dist$1.keyPairFromSeed)(Buffer.from(privateKeyBytes));
|
|
68210
|
+
return {
|
|
68211
|
+
sign: createWalletSigner(keyPair.secretKey),
|
|
68212
|
+
publicKey: Uint8ArrayToHex(keyPair.publicKey)
|
|
68213
|
+
};
|
|
68214
|
+
}
|
|
68215
|
+
};
|
|
67393
68216
|
|
|
67394
68217
|
//#endregion
|
|
67395
68218
|
//#region ../walletkit/dist/esm/utils/cell.js
|
|
@@ -67446,6 +68269,12 @@ function getEventsSubsystem() {
|
|
|
67446
68269
|
* LICENSE file in the root directory of this source tree.
|
|
67447
68270
|
*
|
|
67448
68271
|
*/
|
|
68272
|
+
/**
|
|
68273
|
+
* Creates a wallet ID from network and address
|
|
68274
|
+
*/
|
|
68275
|
+
function createWalletId(network, address) {
|
|
68276
|
+
return (0, import_dist$1.sha256_sync)(`${network.chainId}:${address}`).toString("base64");
|
|
68277
|
+
}
|
|
67449
68278
|
|
|
67450
68279
|
//#endregion
|
|
67451
68280
|
//#region ../walletkit/dist/esm/core/wallet/extensions/jetton.js
|
|
@@ -70172,7 +71001,7 @@ async function dnsLookup(client, domain, category, resolver) {
|
|
|
70172
71001
|
type: "int",
|
|
70173
71002
|
value: toTonDnsCategory(category)
|
|
70174
71003
|
}];
|
|
70175
|
-
const { stack, exitCode } = await client.runGetMethod(asAddressFriendly(resolver), "dnsresolve", SerializeStack(param));
|
|
71004
|
+
const { stack, exitCode } = await CallForSuccess(() => client.runGetMethod(asAddressFriendly(resolver), "dnsresolve", SerializeStack(param)));
|
|
70176
71005
|
if (stack?.length !== 2) return null;
|
|
70177
71006
|
const parsedStack = ParseStack(stack);
|
|
70178
71007
|
if (exitCode !== 0) return null;
|
|
@@ -71170,6 +71999,90 @@ const WalletV5R1CodeCell = import_dist$2.Cell.fromBoc(Buffer.from(WalletV5R1Code
|
|
|
71170
71999
|
* LICENSE file in the root directory of this source tree.
|
|
71171
72000
|
*
|
|
71172
72001
|
*/
|
|
72002
|
+
var ActionSendMsg = class ActionSendMsg {
|
|
72003
|
+
mode;
|
|
72004
|
+
outMsg;
|
|
72005
|
+
static tag = 247711853;
|
|
72006
|
+
tag = ActionSendMsg.tag;
|
|
72007
|
+
constructor(mode, outMsg) {
|
|
72008
|
+
this.mode = mode;
|
|
72009
|
+
this.outMsg = outMsg;
|
|
72010
|
+
}
|
|
72011
|
+
serialize() {
|
|
72012
|
+
return (0, import_dist$2.beginCell)().storeUint(this.tag, 32).storeUint(this.mode | import_dist$2.SendMode.IGNORE_ERRORS, 8).storeRef((0, import_dist$2.beginCell)().store((0, import_dist$2.storeMessageRelaxed)(this.outMsg)).endCell()).endCell();
|
|
72013
|
+
}
|
|
72014
|
+
};
|
|
72015
|
+
var ActionAddExtension = class ActionAddExtension {
|
|
72016
|
+
address;
|
|
72017
|
+
static tag = 2;
|
|
72018
|
+
tag = ActionAddExtension.tag;
|
|
72019
|
+
constructor(address) {
|
|
72020
|
+
this.address = address;
|
|
72021
|
+
}
|
|
72022
|
+
serialize() {
|
|
72023
|
+
return (0, import_dist$2.beginCell)().storeUint(this.tag, 8).storeAddress(this.address).endCell();
|
|
72024
|
+
}
|
|
72025
|
+
};
|
|
72026
|
+
var ActionRemoveExtension = class ActionRemoveExtension {
|
|
72027
|
+
address;
|
|
72028
|
+
static tag = 3;
|
|
72029
|
+
tag = ActionRemoveExtension.tag;
|
|
72030
|
+
constructor(address) {
|
|
72031
|
+
this.address = address;
|
|
72032
|
+
}
|
|
72033
|
+
serialize() {
|
|
72034
|
+
return (0, import_dist$2.beginCell)().storeUint(this.tag, 8).storeAddress(this.address).endCell();
|
|
72035
|
+
}
|
|
72036
|
+
};
|
|
72037
|
+
var ActionSetSignatureAuthAllowed = class ActionSetSignatureAuthAllowed {
|
|
72038
|
+
allowed;
|
|
72039
|
+
static tag = 4;
|
|
72040
|
+
tag = ActionSetSignatureAuthAllowed.tag;
|
|
72041
|
+
constructor(allowed) {
|
|
72042
|
+
this.allowed = allowed;
|
|
72043
|
+
}
|
|
72044
|
+
serialize() {
|
|
72045
|
+
return (0, import_dist$2.beginCell)().storeUint(this.tag, 8).storeUint(this.allowed ? 1 : 0, 1).endCell();
|
|
72046
|
+
}
|
|
72047
|
+
};
|
|
72048
|
+
function isExtendedAction(action) {
|
|
72049
|
+
return action.tag === ActionAddExtension.tag || action.tag === ActionRemoveExtension.tag || action.tag === ActionSetSignatureAuthAllowed.tag;
|
|
72050
|
+
}
|
|
72051
|
+
function packActionsListOut(actions) {
|
|
72052
|
+
if (actions.length === 0) return (0, import_dist$2.beginCell)().endCell();
|
|
72053
|
+
const [action, ...rest] = actions;
|
|
72054
|
+
if (isExtendedAction(action)) throw new Error("Actions bust be in an order: all extended actions, all out actions");
|
|
72055
|
+
return (0, import_dist$2.beginCell)().storeRef(packActionsListOut(rest)).storeSlice(action.serialize().beginParse()).endCell();
|
|
72056
|
+
}
|
|
72057
|
+
function packExtendedActions(extendedActions) {
|
|
72058
|
+
const first = extendedActions[0];
|
|
72059
|
+
const rest = extendedActions.slice(1);
|
|
72060
|
+
let builder = (0, import_dist$2.beginCell)().storeSlice(first.serialize().beginParse());
|
|
72061
|
+
if (rest.length > 0) builder = builder.storeRef(packExtendedActions(extendedActions.slice(1)));
|
|
72062
|
+
return builder.endCell();
|
|
72063
|
+
}
|
|
72064
|
+
function packActionsListExtended(actions) {
|
|
72065
|
+
const extendedActions = [];
|
|
72066
|
+
const outActions = [];
|
|
72067
|
+
actions.forEach((action) => {
|
|
72068
|
+
if (isExtendedAction(action)) extendedActions.push(action);
|
|
72069
|
+
else outActions.push(action);
|
|
72070
|
+
});
|
|
72071
|
+
let builder = (0, import_dist$2.beginCell)();
|
|
72072
|
+
if (outActions.length === 0) builder = builder.storeUint(0, 1);
|
|
72073
|
+
else builder = builder.storeMaybeRef(packActionsListOut(outActions.slice().reverse()));
|
|
72074
|
+
if (extendedActions.length === 0) builder = builder.storeUint(0, 1);
|
|
72075
|
+
else {
|
|
72076
|
+
const first = extendedActions[0];
|
|
72077
|
+
const rest = extendedActions.slice(1);
|
|
72078
|
+
builder = builder.storeUint(1, 1).storeSlice(first.serialize().beginParse());
|
|
72079
|
+
if (rest.length > 0) builder = builder.storeRef(packExtendedActions(rest));
|
|
72080
|
+
}
|
|
72081
|
+
return builder.endCell();
|
|
72082
|
+
}
|
|
72083
|
+
function packActionsList(actions) {
|
|
72084
|
+
return packActionsListExtended(actions);
|
|
72085
|
+
}
|
|
71173
72086
|
|
|
71174
72087
|
//#endregion
|
|
71175
72088
|
//#region ../walletkit/dist/esm/contracts/w5/WalletV5R1Adapter.js
|
|
@@ -71181,6 +72094,201 @@ const WalletV5R1CodeCell = import_dist$2.Cell.fromBoc(Buffer.from(WalletV5R1Code
|
|
|
71181
72094
|
*
|
|
71182
72095
|
*/
|
|
71183
72096
|
const log$2 = globalLogger.createChild("WalletV5R1Adapter");
|
|
72097
|
+
const defaultWalletIdV5R1 = 2147483409;
|
|
72098
|
+
/**
|
|
72099
|
+
* WalletV5R1 adapter that implements WalletInterface for WalletV5 contracts
|
|
72100
|
+
*/
|
|
72101
|
+
var WalletV5R1Adapter = class WalletV5R1Adapter {
|
|
72102
|
+
signer;
|
|
72103
|
+
config;
|
|
72104
|
+
walletContract;
|
|
72105
|
+
client;
|
|
72106
|
+
publicKey;
|
|
72107
|
+
version = "v5r1";
|
|
72108
|
+
/**
|
|
72109
|
+
* Static factory method to create a WalletV5R1Adapter
|
|
72110
|
+
* @param signer - Signer function with publicKey property (from Signer utility)
|
|
72111
|
+
* @param options - Configuration options for the wallet
|
|
72112
|
+
*/
|
|
72113
|
+
static async create(signer, options) {
|
|
72114
|
+
return new WalletV5R1Adapter({
|
|
72115
|
+
signer,
|
|
72116
|
+
publicKey: signer.publicKey,
|
|
72117
|
+
tonClient: options.client,
|
|
72118
|
+
network: options.network,
|
|
72119
|
+
walletId: options.walletId,
|
|
72120
|
+
workchain: options.workchain
|
|
72121
|
+
});
|
|
72122
|
+
}
|
|
72123
|
+
constructor(config) {
|
|
72124
|
+
this.config = config;
|
|
72125
|
+
this.client = config.tonClient;
|
|
72126
|
+
this.signer = config.signer;
|
|
72127
|
+
this.publicKey = this.config.publicKey;
|
|
72128
|
+
this.walletContract = WalletV5.createFromConfig({
|
|
72129
|
+
publicKey: HexToBigInt(this.publicKey),
|
|
72130
|
+
seqno: 0,
|
|
72131
|
+
signatureAllowed: true,
|
|
72132
|
+
walletId: typeof config.walletId === "bigint" ? Number(config.walletId) : config.walletId ?? defaultWalletIdV5R1,
|
|
72133
|
+
extensions: import_dist$2.Dictionary.empty()
|
|
72134
|
+
}, {
|
|
72135
|
+
code: WalletV5R1CodeCell,
|
|
72136
|
+
workchain: config.workchain ?? 0,
|
|
72137
|
+
client: this.client
|
|
72138
|
+
});
|
|
72139
|
+
}
|
|
72140
|
+
getPublicKey() {
|
|
72141
|
+
return this.publicKey;
|
|
72142
|
+
}
|
|
72143
|
+
getClient() {
|
|
72144
|
+
return this.client;
|
|
72145
|
+
}
|
|
72146
|
+
/**
|
|
72147
|
+
* Sign raw bytes with wallet's private key
|
|
72148
|
+
*/
|
|
72149
|
+
async sign(bytes) {
|
|
72150
|
+
return this.signer.sign(bytes);
|
|
72151
|
+
}
|
|
72152
|
+
getNetwork() {
|
|
72153
|
+
return this.config.network;
|
|
72154
|
+
}
|
|
72155
|
+
/**
|
|
72156
|
+
* Get wallet's TON address
|
|
72157
|
+
*/
|
|
72158
|
+
getAddress(options) {
|
|
72159
|
+
return formatWalletAddress(this.walletContract.address, options?.testnet);
|
|
72160
|
+
}
|
|
72161
|
+
getWalletId() {
|
|
72162
|
+
return createWalletId(this.getNetwork(), this.getAddress());
|
|
72163
|
+
}
|
|
72164
|
+
async getSignedSendTransaction(input, options) {
|
|
72165
|
+
const actions = packActionsList(input.messages.map((m) => {
|
|
72166
|
+
let bounce = true;
|
|
72167
|
+
if (import_dist$2.Address.parseFriendly(m.address).isBounceable === false) bounce = false;
|
|
72168
|
+
const msg = (0, import_dist$2.internal)({
|
|
72169
|
+
to: m.address,
|
|
72170
|
+
value: BigInt(m.amount),
|
|
72171
|
+
bounce,
|
|
72172
|
+
extracurrency: m.extraCurrency ? Object.fromEntries(Object.entries(m.extraCurrency).map(([k, v]) => [Number(k), BigInt(v)])) : void 0
|
|
72173
|
+
});
|
|
72174
|
+
if (m.payload) try {
|
|
72175
|
+
msg.body = import_dist$2.Cell.fromBase64(m.payload);
|
|
72176
|
+
} catch (error) {
|
|
72177
|
+
log$2.warn("Failed to load payload", { error });
|
|
72178
|
+
throw WalletKitError.fromError(ERROR_CODES.CONTRACT_VALIDATION_FAILED, "Failed to parse transaction payload", error);
|
|
72179
|
+
}
|
|
72180
|
+
if (m.stateInit) try {
|
|
72181
|
+
msg.init = (0, import_dist$2.loadStateInit)(import_dist$2.Cell.fromBase64(m.stateInit).asSlice());
|
|
72182
|
+
} catch (error) {
|
|
72183
|
+
log$2.warn("Failed to load state init", { error });
|
|
72184
|
+
throw WalletKitError.fromError(ERROR_CODES.CONTRACT_VALIDATION_FAILED, "Failed to parse state init", error);
|
|
72185
|
+
}
|
|
72186
|
+
return new ActionSendMsg(import_dist$2.SendMode.PAY_GAS_SEPARATELY + import_dist$2.SendMode.IGNORE_ERRORS, msg);
|
|
72187
|
+
}));
|
|
72188
|
+
const createBodyOptions = {
|
|
72189
|
+
...options,
|
|
72190
|
+
validUntil: void 0
|
|
72191
|
+
};
|
|
72192
|
+
if (input.validUntil) {
|
|
72193
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
72194
|
+
const maxValidUntil = now + 600;
|
|
72195
|
+
if (input.validUntil < now) throw new WalletKitError(ERROR_CODES.VALIDATION_ERROR, "Transaction valid_until timestamp is in the past", void 0, {
|
|
72196
|
+
validUntil: input.validUntil,
|
|
72197
|
+
currentTime: now
|
|
72198
|
+
});
|
|
72199
|
+
else if (input.validUntil > maxValidUntil) createBodyOptions.validUntil = maxValidUntil;
|
|
72200
|
+
else createBodyOptions.validUntil = input.validUntil;
|
|
72201
|
+
}
|
|
72202
|
+
let seqno = 0;
|
|
72203
|
+
try {
|
|
72204
|
+
seqno = await CallForSuccess(async () => this.getSeqno(), 5, 1e3);
|
|
72205
|
+
} catch (_) {}
|
|
72206
|
+
const walletId = (await this.walletContract.walletId).serialized;
|
|
72207
|
+
if (!walletId) throw new Error("Failed to get seqno or walletId");
|
|
72208
|
+
const transfer = await this.createBodyV5(seqno, walletId, actions, createBodyOptions);
|
|
72209
|
+
const ext = (0, import_dist$2.external)({
|
|
72210
|
+
to: this.walletContract.address,
|
|
72211
|
+
init: this.walletContract.init,
|
|
72212
|
+
body: transfer
|
|
72213
|
+
});
|
|
72214
|
+
return (0, import_dist$2.beginCell)().store((0, import_dist$2.storeMessage)(ext)).endCell().toBoc().toString("base64");
|
|
72215
|
+
}
|
|
72216
|
+
/**
|
|
72217
|
+
* Get state init for wallet deployment
|
|
72218
|
+
*/
|
|
72219
|
+
async getStateInit() {
|
|
72220
|
+
if (!this.walletContract.init) throw new Error("Wallet contract not properly initialized");
|
|
72221
|
+
return (0, import_dist$2.beginCell)().store((0, import_dist$2.storeStateInit)(this.walletContract.init)).endCell().toBoc().toString("base64");
|
|
72222
|
+
}
|
|
72223
|
+
/**
|
|
72224
|
+
* Get the underlying WalletV5 contract
|
|
72225
|
+
*/
|
|
72226
|
+
getContract() {
|
|
72227
|
+
return this.walletContract;
|
|
72228
|
+
}
|
|
72229
|
+
/**
|
|
72230
|
+
* Get current sequence number
|
|
72231
|
+
*/
|
|
72232
|
+
async getSeqno() {
|
|
72233
|
+
try {
|
|
72234
|
+
return await this.walletContract.seqno;
|
|
72235
|
+
} catch (error) {
|
|
72236
|
+
log$2.warn("Failed to get seqno", { error });
|
|
72237
|
+
throw error;
|
|
72238
|
+
}
|
|
72239
|
+
}
|
|
72240
|
+
/**
|
|
72241
|
+
* Get wallet ID
|
|
72242
|
+
*/
|
|
72243
|
+
async getWalletV5R1Id() {
|
|
72244
|
+
try {
|
|
72245
|
+
return this.walletContract.walletId;
|
|
72246
|
+
} catch (error) {
|
|
72247
|
+
log$2.warn("Failed to get wallet ID", { error });
|
|
72248
|
+
const walletId = this.config.walletId;
|
|
72249
|
+
return new WalletV5R1Id({ subwalletNumber: typeof walletId === "bigint" ? Number(walletId) : walletId || 0 });
|
|
72250
|
+
}
|
|
72251
|
+
}
|
|
72252
|
+
/**
|
|
72253
|
+
* Check if wallet is deployed on the network
|
|
72254
|
+
*/
|
|
72255
|
+
async isDeployed() {
|
|
72256
|
+
try {
|
|
72257
|
+
return (await this.client.getAccountState(asAddressFriendly(this.walletContract.address))).status === "active";
|
|
72258
|
+
} catch (error) {
|
|
72259
|
+
log$2.warn("Failed to check deployment status", { error });
|
|
72260
|
+
return false;
|
|
72261
|
+
}
|
|
72262
|
+
}
|
|
72263
|
+
async createBodyV5(seqno, walletId, actionsList, options) {
|
|
72264
|
+
const Opcodes = { auth_signed: 1936287598 };
|
|
72265
|
+
const expireAt = options.validUntil ?? Math.floor(Date.now() / 1e3) + 300;
|
|
72266
|
+
const payload = (0, import_dist$2.beginCell)().storeUint(Opcodes.auth_signed, 32).storeUint(walletId, 32).storeUint(expireAt, 32).storeUint(seqno, 32).storeSlice(actionsList.beginParse()).endCell();
|
|
72267
|
+
const signingData = payload.hash();
|
|
72268
|
+
const signature = options.fakeSignature ? FakeSignature(signingData) : await this.sign(signingData);
|
|
72269
|
+
return (0, import_dist$2.beginCell)().storeSlice(payload.beginParse()).storeBuffer(Buffer.from(HexToUint8Array(signature))).endCell();
|
|
72270
|
+
}
|
|
72271
|
+
async getSignedSignData(input) {
|
|
72272
|
+
return await this.sign(HexToUint8Array(input.hash));
|
|
72273
|
+
}
|
|
72274
|
+
async getSignedTonProof(input) {
|
|
72275
|
+
const message = await CreateTonProofMessageBytes(input);
|
|
72276
|
+
return await this.sign(message);
|
|
72277
|
+
}
|
|
72278
|
+
getSupportedFeatures() {
|
|
72279
|
+
return [{
|
|
72280
|
+
name: "SendTransaction",
|
|
72281
|
+
maxMessages: 255
|
|
72282
|
+
}, {
|
|
72283
|
+
name: "SignData",
|
|
72284
|
+
types: [
|
|
72285
|
+
"binary",
|
|
72286
|
+
"cell",
|
|
72287
|
+
"text"
|
|
72288
|
+
]
|
|
72289
|
+
}];
|
|
72290
|
+
}
|
|
72291
|
+
};
|
|
71184
72292
|
|
|
71185
72293
|
//#endregion
|
|
71186
72294
|
//#region ../walletkit/dist/esm/contracts/w5/WalletV5R1.js
|
|
@@ -71191,6 +72299,129 @@ const log$2 = globalLogger.createChild("WalletV5R1Adapter");
|
|
|
71191
72299
|
* LICENSE file in the root directory of this source tree.
|
|
71192
72300
|
*
|
|
71193
72301
|
*/
|
|
72302
|
+
function walletV5ConfigToCell(config) {
|
|
72303
|
+
return (0, import_dist$2.beginCell)().storeBit(config.signatureAllowed).storeUint(config.seqno, 32).storeUint(config.walletId, 32).storeUint(config.publicKey, 256).storeDict(config.extensions, import_dist$2.Dictionary.Keys.BigUint(256), import_dist$2.Dictionary.Values.BigInt(1)).endCell();
|
|
72304
|
+
}
|
|
72305
|
+
const Opcodes = {
|
|
72306
|
+
action_send_msg: 247711853,
|
|
72307
|
+
action_set_code: 2907562126,
|
|
72308
|
+
action_extended_set_data: 536406539,
|
|
72309
|
+
action_extended_add_extension: 2,
|
|
72310
|
+
action_extended_remove_extension: 3,
|
|
72311
|
+
action_extended_set_signature_auth_allowed: 4,
|
|
72312
|
+
auth_extension: 1702392942,
|
|
72313
|
+
auth_signed: 1936287598,
|
|
72314
|
+
auth_signed_internal: 1936289396
|
|
72315
|
+
};
|
|
72316
|
+
var WalletV5R1Id = class WalletV5R1Id {
|
|
72317
|
+
static deserialize(walletId) {
|
|
72318
|
+
return new WalletV5R1Id({ subwalletNumber: walletId });
|
|
72319
|
+
}
|
|
72320
|
+
subwalletNumber;
|
|
72321
|
+
serialized;
|
|
72322
|
+
constructor(args) {
|
|
72323
|
+
this.subwalletNumber = args?.subwalletNumber ?? 0;
|
|
72324
|
+
this.serialized = BigInt(this.subwalletNumber);
|
|
72325
|
+
}
|
|
72326
|
+
};
|
|
72327
|
+
var WalletV5 = class WalletV5 {
|
|
72328
|
+
client;
|
|
72329
|
+
address;
|
|
72330
|
+
init;
|
|
72331
|
+
subwalletId;
|
|
72332
|
+
constructor(client, address, init) {
|
|
72333
|
+
this.client = client;
|
|
72334
|
+
this.address = address;
|
|
72335
|
+
this.init = init;
|
|
72336
|
+
}
|
|
72337
|
+
static createFromAddress(client, address) {
|
|
72338
|
+
return new WalletV5(client, address);
|
|
72339
|
+
}
|
|
72340
|
+
static createFromConfig(config, options) {
|
|
72341
|
+
const data = walletV5ConfigToCell(config);
|
|
72342
|
+
const init = {
|
|
72343
|
+
code: options.code,
|
|
72344
|
+
data
|
|
72345
|
+
};
|
|
72346
|
+
const wallet = new WalletV5(options.client, (0, import_dist$2.contractAddress)(options.workchain, init), init);
|
|
72347
|
+
wallet.subwalletId = config.walletId;
|
|
72348
|
+
return wallet;
|
|
72349
|
+
}
|
|
72350
|
+
async sendDeploy(provider, via, value) {
|
|
72351
|
+
await provider.internal(via, {
|
|
72352
|
+
value,
|
|
72353
|
+
sendMode: import_dist$2.SendMode.PAY_GAS_SEPARATELY,
|
|
72354
|
+
body: (0, import_dist$2.beginCell)().endCell()
|
|
72355
|
+
});
|
|
72356
|
+
}
|
|
72357
|
+
async sendInternalSignedMessage(provider, via, opts) {
|
|
72358
|
+
await provider.internal(via, {
|
|
72359
|
+
value: opts.value,
|
|
72360
|
+
sendMode: import_dist$2.SendMode.PAY_GAS_SEPARATELY,
|
|
72361
|
+
body: (0, import_dist$2.beginCell)().storeSlice(opts.body.beginParse()).endCell()
|
|
72362
|
+
});
|
|
72363
|
+
}
|
|
72364
|
+
async sendInternalMessageFromExtension(provider, via, opts) {
|
|
72365
|
+
await provider.internal(via, {
|
|
72366
|
+
value: opts.value,
|
|
72367
|
+
sendMode: import_dist$2.SendMode.PAY_GAS_SEPARATELY,
|
|
72368
|
+
body: (0, import_dist$2.beginCell)().storeUint(Opcodes.auth_extension, 32).storeUint(0, 64).storeSlice(opts.body.beginParse()).endCell()
|
|
72369
|
+
});
|
|
72370
|
+
}
|
|
72371
|
+
async sendInternal(provider, via, opts) {
|
|
72372
|
+
await provider.internal(via, opts);
|
|
72373
|
+
}
|
|
72374
|
+
async sendExternalSignedMessage(provider, body) {
|
|
72375
|
+
await provider.external(body);
|
|
72376
|
+
}
|
|
72377
|
+
async sendExternal(provider, body) {
|
|
72378
|
+
await provider.external(body);
|
|
72379
|
+
}
|
|
72380
|
+
get publicKey() {
|
|
72381
|
+
return this.client.runGetMethod(asAddressFriendly(this.address), "get_public_key").then((data) => {
|
|
72382
|
+
if (data.exitCode === 0) {
|
|
72383
|
+
const parsedStack = ParseStack(data.stack);
|
|
72384
|
+
if (parsedStack[0]?.type === "int") return parsedStack[0].value;
|
|
72385
|
+
else throw new Error("Stack is not an int");
|
|
72386
|
+
} else if (this.init) return this.init.data.asSlice().skip(65).loadUintBig(256);
|
|
72387
|
+
else return 0n;
|
|
72388
|
+
});
|
|
72389
|
+
}
|
|
72390
|
+
get status() {
|
|
72391
|
+
return this.client.getAccountState(asAddressFriendly(this.address)).then((state) => state.status);
|
|
72392
|
+
}
|
|
72393
|
+
get seqno() {
|
|
72394
|
+
return this.client.runGetMethod(asAddressFriendly(this.address), "seqno").then((data) => {
|
|
72395
|
+
if (data.exitCode === 0) {
|
|
72396
|
+
const parsedStack = ParseStack(data.stack);
|
|
72397
|
+
if (parsedStack[0]?.type === "int") return Number(parsedStack[0].value);
|
|
72398
|
+
else throw new Error("Stack is not an int");
|
|
72399
|
+
} else return 0;
|
|
72400
|
+
});
|
|
72401
|
+
}
|
|
72402
|
+
get isSignatureAuthAllowed() {
|
|
72403
|
+
return this.client.runGetMethod(asAddressFriendly(this.address), "is_signature_allowed").then((data) => {
|
|
72404
|
+
if (data.exitCode === 0) {
|
|
72405
|
+
const parsedStack = ParseStack(data.stack);
|
|
72406
|
+
if (parsedStack[0]?.type === "int") return Boolean(parsedStack[0].value);
|
|
72407
|
+
else throw new Error("Stack is not an int");
|
|
72408
|
+
} else return false;
|
|
72409
|
+
});
|
|
72410
|
+
}
|
|
72411
|
+
get walletId() {
|
|
72412
|
+
if (this.subwalletId !== void 0) return new Promise((resolve) => {
|
|
72413
|
+
resolve(WalletV5R1Id.deserialize(this.subwalletId));
|
|
72414
|
+
});
|
|
72415
|
+
else return this.client.runGetMethod(asAddressFriendly(this.address), "get_subwallet_id").then((data) => {
|
|
72416
|
+
if (data.exitCode === 0) {
|
|
72417
|
+
const parsedStack = ParseStack(data.stack);
|
|
72418
|
+
if (parsedStack[0]?.type === "int") this.subwalletId = Number(parsedStack[0].value);
|
|
72419
|
+
else throw new Error("Stack is not an int");
|
|
72420
|
+
return WalletV5R1Id.deserialize(this.subwalletId);
|
|
72421
|
+
} else return WalletV5R1Id.deserialize(defaultWalletIdV5R1);
|
|
72422
|
+
});
|
|
72423
|
+
}
|
|
72424
|
+
};
|
|
71194
72425
|
|
|
71195
72426
|
//#endregion
|
|
71196
72427
|
//#region ../walletkit/dist/esm/contracts/v4r2/WalletV4R2.js
|
|
@@ -86943,6 +88174,18 @@ var McpWalletService = class McpWalletService {
|
|
|
86943
88174
|
return this.config.contacts.resolve("default", name);
|
|
86944
88175
|
}
|
|
86945
88176
|
/**
|
|
88177
|
+
* Resolve a TON DNS domain (e.g., "wallet.ton") to a wallet address
|
|
88178
|
+
*/
|
|
88179
|
+
async resolveDns(domain) {
|
|
88180
|
+
return this.wallet.getClient().resolveDnsWallet(domain);
|
|
88181
|
+
}
|
|
88182
|
+
/**
|
|
88183
|
+
* Reverse resolve a wallet address to a TON DNS domain
|
|
88184
|
+
*/
|
|
88185
|
+
async backResolveDns(address) {
|
|
88186
|
+
return this.wallet.getClient().backResolveDnsWallet(address);
|
|
88187
|
+
}
|
|
88188
|
+
/**
|
|
86946
88189
|
* Close and cleanup
|
|
86947
88190
|
*/
|
|
86948
88191
|
async close() {
|
|
@@ -87544,6 +88787,97 @@ function createMcpNftTools(service) {
|
|
|
87544
88787
|
};
|
|
87545
88788
|
}
|
|
87546
88789
|
|
|
88790
|
+
//#endregion
|
|
88791
|
+
//#region src/tools/dns-tools.ts
|
|
88792
|
+
/**
|
|
88793
|
+
* Copyright (c) TonTech.
|
|
88794
|
+
*
|
|
88795
|
+
* This source code is licensed under the MIT license found in the
|
|
88796
|
+
* LICENSE file in the root directory of this source tree.
|
|
88797
|
+
*
|
|
88798
|
+
*/
|
|
88799
|
+
const resolveDnsSchema = z.object({ domain: z.string().min(1).describe("TON DNS domain to resolve (e.g., \"foundation.ton\")") });
|
|
88800
|
+
const backResolveDnsSchema = z.object({ address: z.string().min(1).describe("TON wallet address to reverse resolve") });
|
|
88801
|
+
function createMcpDnsTools(service) {
|
|
88802
|
+
return {
|
|
88803
|
+
resolve_dns: {
|
|
88804
|
+
description: "Resolve a TON DNS domain (e.g., \"foundation.ton\") to a wallet address. Use this when the user provides a .ton domain instead of a raw address.",
|
|
88805
|
+
inputSchema: resolveDnsSchema,
|
|
88806
|
+
handler: async (args) => {
|
|
88807
|
+
try {
|
|
88808
|
+
const address = await service.resolveDns(args.domain);
|
|
88809
|
+
if (!address) return {
|
|
88810
|
+
content: [{
|
|
88811
|
+
type: "text",
|
|
88812
|
+
text: JSON.stringify({
|
|
88813
|
+
success: false,
|
|
88814
|
+
error: `Could not resolve domain "${args.domain}". The domain may not exist or may not have a wallet record.`
|
|
88815
|
+
})
|
|
88816
|
+
}],
|
|
88817
|
+
isError: true
|
|
88818
|
+
};
|
|
88819
|
+
return { content: [{
|
|
88820
|
+
type: "text",
|
|
88821
|
+
text: JSON.stringify({
|
|
88822
|
+
success: true,
|
|
88823
|
+
domain: args.domain,
|
|
88824
|
+
address
|
|
88825
|
+
}, null, 2)
|
|
88826
|
+
}] };
|
|
88827
|
+
} catch (error) {
|
|
88828
|
+
return {
|
|
88829
|
+
content: [{
|
|
88830
|
+
type: "text",
|
|
88831
|
+
text: JSON.stringify({
|
|
88832
|
+
success: false,
|
|
88833
|
+
error: `Failed to resolve domain: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
88834
|
+
})
|
|
88835
|
+
}],
|
|
88836
|
+
isError: true
|
|
88837
|
+
};
|
|
88838
|
+
}
|
|
88839
|
+
}
|
|
88840
|
+
},
|
|
88841
|
+
back_resolve_dns: {
|
|
88842
|
+
description: "Reverse resolve a TON wallet address to its DNS domain (e.g., find the .ton domain associated with an address).",
|
|
88843
|
+
inputSchema: backResolveDnsSchema,
|
|
88844
|
+
handler: async (args) => {
|
|
88845
|
+
try {
|
|
88846
|
+
const domain = await service.backResolveDns(args.address);
|
|
88847
|
+
if (!domain) return { content: [{
|
|
88848
|
+
type: "text",
|
|
88849
|
+
text: JSON.stringify({
|
|
88850
|
+
success: true,
|
|
88851
|
+
address: args.address,
|
|
88852
|
+
domain: null,
|
|
88853
|
+
message: "No DNS domain found for this address."
|
|
88854
|
+
})
|
|
88855
|
+
}] };
|
|
88856
|
+
return { content: [{
|
|
88857
|
+
type: "text",
|
|
88858
|
+
text: JSON.stringify({
|
|
88859
|
+
success: true,
|
|
88860
|
+
address: args.address,
|
|
88861
|
+
domain
|
|
88862
|
+
}, null, 2)
|
|
88863
|
+
}] };
|
|
88864
|
+
} catch (error) {
|
|
88865
|
+
return {
|
|
88866
|
+
content: [{
|
|
88867
|
+
type: "text",
|
|
88868
|
+
text: JSON.stringify({
|
|
88869
|
+
success: false,
|
|
88870
|
+
error: `Failed to reverse resolve address: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
88871
|
+
})
|
|
88872
|
+
}],
|
|
88873
|
+
isError: true
|
|
88874
|
+
};
|
|
88875
|
+
}
|
|
88876
|
+
}
|
|
88877
|
+
}
|
|
88878
|
+
};
|
|
88879
|
+
}
|
|
88880
|
+
|
|
87547
88881
|
//#endregion
|
|
87548
88882
|
//#region src/factory.ts
|
|
87549
88883
|
/**
|
|
@@ -87597,6 +88931,7 @@ async function createTonWalletMCP(config) {
|
|
|
87597
88931
|
const swapTools = createMcpSwapTools(walletService);
|
|
87598
88932
|
const knownJettonsTools = createMcpKnownJettonsTools();
|
|
87599
88933
|
const nftTools = createMcpNftTools(walletService);
|
|
88934
|
+
const dnsTools = createMcpDnsTools(walletService);
|
|
87600
88935
|
const registerTool = (name, tool) => {
|
|
87601
88936
|
server.registerTool(name, {
|
|
87602
88937
|
description: tool.description,
|
|
@@ -87615,6 +88950,8 @@ async function createTonWalletMCP(config) {
|
|
|
87615
88950
|
registerTool("get_nfts", nftTools.get_nfts);
|
|
87616
88951
|
registerTool("get_nft", nftTools.get_nft);
|
|
87617
88952
|
registerTool("send_nft", nftTools.send_nft);
|
|
88953
|
+
registerTool("resolve_dns", dnsTools.resolve_dns);
|
|
88954
|
+
registerTool("back_resolve_dns", dnsTools.back_resolve_dns);
|
|
87618
88955
|
return server;
|
|
87619
88956
|
}
|
|
87620
88957
|
/**
|
|
@@ -87626,4 +88963,169 @@ function createShutdownHandler(walletService) {
|
|
|
87626
88963
|
}
|
|
87627
88964
|
|
|
87628
88965
|
//#endregion
|
|
87629
|
-
|
|
88966
|
+
//#region src/serverless.ts
|
|
88967
|
+
/**
|
|
88968
|
+
* Copyright (c) TonTech.
|
|
88969
|
+
*
|
|
88970
|
+
* This source code is licensed under the MIT license found in the
|
|
88971
|
+
* LICENSE file in the root directory of this source tree.
|
|
88972
|
+
*
|
|
88973
|
+
*/
|
|
88974
|
+
/**
|
|
88975
|
+
* TON MCP Serverless Handler
|
|
88976
|
+
*
|
|
88977
|
+
* Provides a simple serverless-compatible function for MCP operations.
|
|
88978
|
+
* Credentials are extracted from request headers.
|
|
88979
|
+
*
|
|
88980
|
+
* Headers:
|
|
88981
|
+
* - MNEMONIC: 24-word mnemonic phrase
|
|
88982
|
+
* - PRIVATE_KEY: Hex-encoded private key (takes priority over MNEMONIC)
|
|
88983
|
+
* - NETWORK: Network to use (mainnet or testnet, default: mainnet)
|
|
88984
|
+
* - TONCENTER_KEY: Optional TonCenter API key
|
|
88985
|
+
*/
|
|
88986
|
+
function getHeader(headers, name) {
|
|
88987
|
+
const value = headers[name] || headers[name.toLowerCase()] || headers[name.toUpperCase()];
|
|
88988
|
+
if (Array.isArray(value)) return value[0];
|
|
88989
|
+
return value;
|
|
88990
|
+
}
|
|
88991
|
+
function parseCredentials(headers) {
|
|
88992
|
+
const privateKeyHex = getHeader(headers, "PRIVATE_KEY") || getHeader(headers, "private-key");
|
|
88993
|
+
const mnemonicStr = getHeader(headers, "MNEMONIC") || getHeader(headers, "mnemonic");
|
|
88994
|
+
const networkStr = getHeader(headers, "NETWORK") || getHeader(headers, "network");
|
|
88995
|
+
const toncenterKey = getHeader(headers, "TONCENTER_KEY") || getHeader(headers, "toncenter-key");
|
|
88996
|
+
let privateKey;
|
|
88997
|
+
let mnemonic;
|
|
88998
|
+
if (privateKeyHex) {
|
|
88999
|
+
privateKey = Buffer.from(privateKeyHex, "hex");
|
|
89000
|
+
if (privateKey.length !== 32 && privateKey.length !== 64) return null;
|
|
89001
|
+
if (privateKey.length === 64) privateKey = privateKey.subarray(0, 32);
|
|
89002
|
+
} else if (mnemonicStr) {
|
|
89003
|
+
mnemonic = mnemonicStr.trim().split(/\s+/);
|
|
89004
|
+
if (mnemonic.length !== 24) return null;
|
|
89005
|
+
} else return null;
|
|
89006
|
+
return {
|
|
89007
|
+
mnemonic,
|
|
89008
|
+
privateKey,
|
|
89009
|
+
network: networkStr === "testnet" ? "testnet" : "mainnet",
|
|
89010
|
+
toncenterKey
|
|
89011
|
+
};
|
|
89012
|
+
}
|
|
89013
|
+
async function createWalletFromCredentials(credentials) {
|
|
89014
|
+
const network = credentials.network === "mainnet" ? Network.mainnet() : Network.testnet();
|
|
89015
|
+
const apiConfig = {};
|
|
89016
|
+
if (credentials.toncenterKey) {
|
|
89017
|
+
apiConfig.url = credentials.network === "mainnet" ? "https://toncenter.com" : "https://testnet.toncenter.com";
|
|
89018
|
+
apiConfig.key = credentials.toncenterKey;
|
|
89019
|
+
}
|
|
89020
|
+
const kit = new TonWalletKit({
|
|
89021
|
+
networks: { [network.chainId]: { apiClient: apiConfig } },
|
|
89022
|
+
storage: new MemoryStorageAdapter()
|
|
89023
|
+
});
|
|
89024
|
+
await kit.waitForReady();
|
|
89025
|
+
let signer;
|
|
89026
|
+
if (credentials.privateKey) signer = await Signer.fromPrivateKey(credentials.privateKey);
|
|
89027
|
+
else if (credentials.mnemonic) signer = await Signer.fromMnemonic(credentials.mnemonic, { type: "ton" });
|
|
89028
|
+
else throw new Error("No valid credentials");
|
|
89029
|
+
const walletAdapter = await WalletV5R1Adapter.create(signer, {
|
|
89030
|
+
client: kit.getApiClient(network),
|
|
89031
|
+
network
|
|
89032
|
+
});
|
|
89033
|
+
let wallet = await kit.addWallet(walletAdapter);
|
|
89034
|
+
if (!wallet) wallet = kit.getWallet(walletAdapter.getWalletId());
|
|
89035
|
+
if (!wallet) throw new Error("Failed to create wallet");
|
|
89036
|
+
return {
|
|
89037
|
+
wallet,
|
|
89038
|
+
kit
|
|
89039
|
+
};
|
|
89040
|
+
}
|
|
89041
|
+
/**
|
|
89042
|
+
* Create a serverless handler for MCP requests
|
|
89043
|
+
*
|
|
89044
|
+
* @example
|
|
89045
|
+
* ```typescript
|
|
89046
|
+
* // AWS Lambda
|
|
89047
|
+
* import { createServerlessHandler } from '@ton/mcp/serverless';
|
|
89048
|
+
* export const handler = createServerlessHandler();
|
|
89049
|
+
*
|
|
89050
|
+
* // Vercel
|
|
89051
|
+
* import { createServerlessHandler } from '@ton/mcp/serverless';
|
|
89052
|
+
* export default createServerlessHandler();
|
|
89053
|
+
* ```
|
|
89054
|
+
*/
|
|
89055
|
+
function createServerlessHandler() {
|
|
89056
|
+
return async (req) => {
|
|
89057
|
+
const credentials = parseCredentials(req.headers);
|
|
89058
|
+
if (!credentials) return {
|
|
89059
|
+
statusCode: 403,
|
|
89060
|
+
headers: { "Content-Type": "application/json" },
|
|
89061
|
+
body: JSON.stringify({
|
|
89062
|
+
error: "Forbidden",
|
|
89063
|
+
message: "Missing or invalid credentials. Provide PRIVATE_KEY or MNEMONIC header."
|
|
89064
|
+
})
|
|
89065
|
+
};
|
|
89066
|
+
let kit = null;
|
|
89067
|
+
try {
|
|
89068
|
+
const { wallet, kit: walletKit } = await createWalletFromCredentials(credentials);
|
|
89069
|
+
kit = walletKit;
|
|
89070
|
+
const server = await createTonWalletMCP({
|
|
89071
|
+
wallet,
|
|
89072
|
+
networks: {
|
|
89073
|
+
mainnet: credentials.toncenterKey && credentials.network === "mainnet" ? { apiKey: credentials.toncenterKey } : void 0,
|
|
89074
|
+
testnet: credentials.toncenterKey && credentials.network === "testnet" ? { apiKey: credentials.toncenterKey } : void 0
|
|
89075
|
+
}
|
|
89076
|
+
});
|
|
89077
|
+
const transport = new StreamableHTTPServerTransport({ sessionIdGenerator: () => crypto.randomUUID() });
|
|
89078
|
+
await server.connect(transport);
|
|
89079
|
+
const responseChunks = [];
|
|
89080
|
+
const mockRes = {
|
|
89081
|
+
writeHead: () => mockRes,
|
|
89082
|
+
write: (chunk) => {
|
|
89083
|
+
responseChunks.push(Buffer.from(chunk));
|
|
89084
|
+
return true;
|
|
89085
|
+
},
|
|
89086
|
+
end: (chunk) => {
|
|
89087
|
+
if (chunk) responseChunks.push(Buffer.from(chunk));
|
|
89088
|
+
},
|
|
89089
|
+
setHeader: () => mockRes,
|
|
89090
|
+
getHeader: () => void 0,
|
|
89091
|
+
on: () => mockRes
|
|
89092
|
+
};
|
|
89093
|
+
const mockReq = {
|
|
89094
|
+
method: req.method || "POST",
|
|
89095
|
+
url: req.url || "/mcp",
|
|
89096
|
+
headers: req.headers,
|
|
89097
|
+
on: (event, handler) => {
|
|
89098
|
+
if (event === "data" && req.body) handler(typeof req.body === "string" ? req.body : JSON.stringify(req.body));
|
|
89099
|
+
if (event === "end") handler();
|
|
89100
|
+
return mockReq;
|
|
89101
|
+
}
|
|
89102
|
+
};
|
|
89103
|
+
await transport.handleRequest(mockReq, mockRes);
|
|
89104
|
+
const responseBody = Buffer.concat(responseChunks).toString("utf-8");
|
|
89105
|
+
await transport.close();
|
|
89106
|
+
await kit.close();
|
|
89107
|
+
return {
|
|
89108
|
+
statusCode: 200,
|
|
89109
|
+
headers: { "Content-Type": "application/json" },
|
|
89110
|
+
body: responseBody
|
|
89111
|
+
};
|
|
89112
|
+
} catch (error) {
|
|
89113
|
+
if (kit) await kit.close();
|
|
89114
|
+
return {
|
|
89115
|
+
statusCode: 500,
|
|
89116
|
+
headers: { "Content-Type": "application/json" },
|
|
89117
|
+
body: JSON.stringify({
|
|
89118
|
+
error: "Internal Server Error",
|
|
89119
|
+
message: error instanceof Error ? error.message : "Unknown error"
|
|
89120
|
+
})
|
|
89121
|
+
};
|
|
89122
|
+
}
|
|
89123
|
+
};
|
|
89124
|
+
}
|
|
89125
|
+
/**
|
|
89126
|
+
* Default serverless handler
|
|
89127
|
+
*/
|
|
89128
|
+
const handler = createServerlessHandler();
|
|
89129
|
+
|
|
89130
|
+
//#endregion
|
|
89131
|
+
export { McpWalletService, createServerlessHandler, createShutdownHandler, createTonWalletMCP, handler };
|