wan-ton-sdk 0.0.6

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.
Files changed (119) hide show
  1. package/Bridge.compile.ts +8 -0
  2. package/Bridge.ts +645 -0
  3. package/Bridge.ts.org +647 -0
  4. package/Fake.compile.ts +8 -0
  5. package/GroupApprove.compile.ts +6 -0
  6. package/GroupApprove.ts +274 -0
  7. package/JettonMinter.compile.ts +5 -0
  8. package/JettonMinter.ts +135 -0
  9. package/JettonWallet.compile.ts +5 -0
  10. package/JettonWallet.ts +127 -0
  11. package/README.md +24 -0
  12. package/Signature.compile.ts +6 -0
  13. package/Signature.ts +105 -0
  14. package/address.compile.ts +8 -0
  15. package/client/1.json +1730 -0
  16. package/client/client.ts +73 -0
  17. package/client/log/.ba8901062aebb86bf525b42c65ff84f9485419e4-audit.json +15 -0
  18. package/client/log/wan-ton-sdk.out.2024-12-19 +0 -0
  19. package/code/decode.spec.ts +81 -0
  20. package/code/encode-decode.ts +622 -0
  21. package/code/log/.ba8901062aebb86bf525b42c65ff84f9485419e4-audit.json +20 -0
  22. package/code/log/wan-ton-sdk.out.2025-01-15 +0 -0
  23. package/code/log/wan-ton-sdk.out.2025-01-20 +0 -0
  24. package/code/userLock.ts +291 -0
  25. package/common.ts +62 -0
  26. package/config/config-ex.ts +25 -0
  27. package/config/config.ts +1 -0
  28. package/const/const-value.ts +33 -0
  29. package/contractAccess/bridgeAccess.ts +60 -0
  30. package/contractAccess/groupApproveAccess.ts +59 -0
  31. package/data/EQCxE6mUtQJKFnGfaROTKOt1lZbDiiX1kCixRv7Nw2Id_sDs.json +7348 -0
  32. package/db/Db.spec.ts +193 -0
  33. package/db/Db.ts +736 -0
  34. package/db/DbAccess.spec.ts +23 -0
  35. package/db/DbAccess.ts +174 -0
  36. package/db/common.ts +94 -0
  37. package/db/log/.ba8901062aebb86bf525b42c65ff84f9485419e4-audit.json +15 -0
  38. package/db/log/wan-ton-sdk.out.2025-04-21 +0 -0
  39. package/event/getEvents.ts +441 -0
  40. package/event/log/.ba8901062aebb86bf525b42c65ff84f9485419e4-audit.json +15 -0
  41. package/event/log/wan-ton-sdk.out.2024-12-19 +0 -0
  42. package/example/0:1.txt +1 -0
  43. package/example/AddToken-ex.ts +148 -0
  44. package/example/README.md +21 -0
  45. package/example/addSmg-ex.ts +78 -0
  46. package/example/addTokenPair-ex.ts +73 -0
  47. package/example/compileContract-ex.ts +10 -0
  48. package/example/converTranToTonTran.ts +43 -0
  49. package/example/createDb-ex.ts +8 -0
  50. package/example/dbFeedTrans-ex.ts +20 -0
  51. package/example/delTokenPair-ex.ts +63 -0
  52. package/example/deploy-ex.ts +68 -0
  53. package/example/getAllEvents-ex.ts +61 -0
  54. package/example/getAllTrans.ts +32 -0
  55. package/example/getContractState-ex.ts +33 -0
  56. package/example/getEventByHash-ex.ts +39 -0
  57. package/example/getEvents-ex.ts +55 -0
  58. package/example/getJettonInfo-ex.ts +44 -0
  59. package/example/getTokenPair-ex.ts +47 -0
  60. package/example/getTransByRange-ex.ts +81 -0
  61. package/example/getTransaction-ex.ts +62 -0
  62. package/example/isTranSuccess-ex.ts +33 -0
  63. package/example/locateTx-ex.ts +44 -0
  64. package/example/log/.ba8901062aebb86bf525b42c65ff84f9485419e4-audit.json +55 -0
  65. package/example/log/wan-ton-sdk.out.2025-04-21 +447 -0
  66. package/example/log/wan-ton-sdk.out.2025-04-22 +1918 -0
  67. package/example/log/wan-ton-sdk.out.2025-04-23 +4216 -0
  68. package/example/log/wan-ton-sdk.out.2025-04-24.gz +0 -0
  69. package/example/log/wan-ton-sdk.out.2025-04-25 +46814 -0
  70. package/example/log/wan-ton-sdk.out.2025-04-27.gz +0 -0
  71. package/example/log/wan-ton-sdk.out.2025-04-28 +4966 -0
  72. package/example/log/wan-ton-sdk.out.2025-04-29 +1328 -0
  73. package/example/log/wan-ton-sdk.out.2025-04-30 +9 -0
  74. package/example/mintToken-ex.ts +82 -0
  75. package/example/setFee-ex.ts +82 -0
  76. package/example/setFeeProxy.ts +70 -0
  77. package/example/smgRelease-ex.ts +173 -0
  78. package/example/upgradeBridgeSc-ex.ts +49 -0
  79. package/example/userLock-ex.ts +78 -0
  80. package/fee/fee.ts +23 -0
  81. package/index.ts +67 -0
  82. package/jest.config.ts +9 -0
  83. package/log/.ba8901062aebb86bf525b42c65ff84f9485419e4-audit.json +20 -0
  84. package/log/wan-ton-sdk.out.2025-04-21 +0 -0
  85. package/log/wan-ton-sdk.out.2025-04-30 +10 -0
  86. package/opcodes.ts +58 -0
  87. package/package.json +34 -0
  88. package/publish.sh +78 -0
  89. package/sign/buildHash.ts +47 -0
  90. package/sign/log/.ba8901062aebb86bf525b42c65ff84f9485419e4-audit.json +15 -0
  91. package/sign/log/wan-ton-sdk.out.2025-04-14 +3 -0
  92. package/sign/rawTrans.spec.ts +117 -0
  93. package/sign/rawTrans.ts +122 -0
  94. package/sign/tools-secp256k1.js +210 -0
  95. package/testData/JettonMinter.compile.func.ts +8 -0
  96. package/testData/JettonWallet.compile.func.ts +8 -0
  97. package/testData/addressList.json +6 -0
  98. package/testData/bridge.compile.func.ts +8 -0
  99. package/testData/bridge.compiled.json +1 -0
  100. package/testData/contractAddress.json +7 -0
  101. package/testData/jettonTokenInfo.json +14 -0
  102. package/testData/prvlist.json +6 -0
  103. package/testData/smg.json +5 -0
  104. package/testData/tokenInfo.json +23 -0
  105. package/transResult/transResult.ts +621 -0
  106. package/tsconfig.json +19 -0
  107. package/utils/.compiled.json +1 -0
  108. package/utils/compileContract.ts +60 -0
  109. package/utils/log/.ba8901062aebb86bf525b42c65ff84f9485419e4-audit.json +15 -0
  110. package/utils/log/wan-ton-sdk.out.2025-01-08 +0 -0
  111. package/utils/logger.spec.ts +14 -0
  112. package/utils/logger.ts +96 -0
  113. package/utils/utils.spec.ts +21 -0
  114. package/utils/utils.ts +295 -0
  115. package/wallet/balance.ts +29 -0
  116. package/wallet/jetton.spec.ts +27 -0
  117. package/wallet/jetton.ts +159 -0
  118. package/wallet/walletContract.spec.ts +111 -0
  119. package/wallet/walletContract.ts +105 -0
package/opcodes.ts ADDED
@@ -0,0 +1,58 @@
1
+ export const OP_SIG = 0x10000000;
2
+ export const OP_SIG_Verify = 0x10000001;
3
+ export const OP_SIG_VerifyEcdsa = 0x10000002;
4
+ export const OP_COMMON = 0x20000000;
5
+ export const OP_COMMON_TransferOwner = 0x20000003;
6
+ export const OP_COMMON_SetHalt = 0x20000004;
7
+ export const OP_COMMON_Finished = 0x2000000F;
8
+ export const OP_FEE = 0x30000000;
9
+ export const OP_FEE_SetChainFee = 0x30000001;
10
+ export const OP_FEE_SetChainFees = 0x30000002;
11
+ export const OP_FEE_SetSmgFeeProxy = 0x30000003;
12
+ export const OP_FEE_SetFeeOperator = 0x30000004;
13
+ export const OP_FEE_SetTokenPairFee = 0x30000005;
14
+ export const OP_FEE_SetTokenPairFees = 0x30000006;
15
+ export const OP_FEE_AddCrossAdmin = 0x30000007;
16
+ export const OP_FEE_DelCrossAdmin = 0x30000008;
17
+ export const OP_CROSS = 0x40000000;
18
+ export const OP_CROSS_UserLock = 0x40000001;
19
+ export const OP_CROSS_SmgMint = 0x40000002;
20
+ export const OP_CROSS_UserBurn = 0x40000003;
21
+ export const OP_CROSS_UserRelease = 0x40000004;
22
+ export const OP_CROSS_SmgRelease = 0x40000005;
23
+ export const OP_CROSS_TransOwner = 0x40000006;
24
+ export const OP_TOKENPAIR = 0x50000000;
25
+ export const OP_TOKENPAIR_Remove = 0x50000002;
26
+ export const OP_TOKENPAIR_Upsert = 0x50000003;
27
+ export const OP_ORACLE = 0x60000000;
28
+ export const OP_ORACLE_TransferOracleAdmin = 0x60000001;
29
+ export const OP_ORACLE_SetSMG = 0x60000002;
30
+ export const OP_ORACLE_CommitSMG = 0x60000003;
31
+ export const OP_ORACLE_DeleteSMG = 0x60000004;
32
+ export const OP_EXTEND = 0x80000000;
33
+ export const OP_EXTEND_UpdateAddInt = 0x80000007;
34
+ export const OP_UPGRADE = 0x90000000;
35
+ export const OP_UPGRADE_Code = 0x90000001;
36
+ export const OP_GROUPAPPROVE_Proposol = 0xa0000001;
37
+ export const OP_GROUPAPPROVE_Execute = 0xa0000002;
38
+ export const OP_GROUPAPPROVE_Executed_CB = 0xa0000003;
39
+ export const OP_GROUPAPPROVE_TranferFoundation = 0xa0000004;
40
+ export const OP_GROUPAPPROVE_Execute_WithGPK = 0xa0000005;
41
+ export const OP_GROUPAPPROVE_Proposol_SetSmgFeeProxy = 0xa0000010;
42
+ export const OP_GROUPAPPROVE_Proposol_AddCrossAdmin = 0xa0000011;
43
+ export const OP_GROUPAPPROVE_Proposol_DelCrossAdmin = 0xa0000012;
44
+ export const OP_GROUPAPPROVE_Proposol_TransferOwner = 0xa0000013;
45
+ export const OP_GROUPAPPROVE_Proposol_SetHalt = 0xa0000014;
46
+ export const OP_GROUPAPPROVE_Proposol_TOKENPAIR_Upsert = 0xa0000015;
47
+ export const OP_GROUPAPPROVE_Proposol_TOKENPAIR_Remove = 0xa0000016;
48
+ export const OP_GROUPAPPROVE_Proposol_TranferFoundation = 0xa0000017;
49
+ export const OP_GROUPAPPROVE_Proposol_TransferOracleAdmin = 0xa0000018;
50
+ export const OP_GROUPAPPROVE_Proposol_TransferOperator = 0xa000001;
51
+ export const OP_GROUPAPPROVE_Proposol_UpgradeSc = 0xa000001a;
52
+ export const OP_FETCH_GPK = 0xb0000001;
53
+
54
+
55
+
56
+ export const OP_TRANSFER_NOTIFICATION = 0x7362d09c;
57
+ export const OP_TRANSFER = 0xf8a7ea5;
58
+ export const OP_EXCESSES = 0xd53276db;
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "wan-ton-sdk",
3
+ "version": "0.0.6",
4
+ "scripts": {
5
+ "build": "rm -rf ../wan-ton-sdk/dist && tsc",
6
+ "test": "jest --verbose",
7
+ "main": "../wan-ton-sdk/dist/index.js"
8
+ },
9
+ "devDependencies": {
10
+ "@types/jest": "^29.5.14",
11
+ "@types/node": "^22.14.1",
12
+ "jest": "29.7.0",
13
+ "prettier": "^3.3.2",
14
+ "ts-jest": "^29.2.0",
15
+ "ts-node": "^10.9.2",
16
+ "typescript": "^5.5.3"
17
+ },
18
+ "dependencies": {
19
+ "@orbs-network/ton-access": "^2.3.3",
20
+ "@ton-community/func-js": "0.7.0",
21
+ "@ton/blueprint": "0.22.0",
22
+ "@ton/core": "0.56.3",
23
+ "@ton/crypto": "3.3.0",
24
+ "@ton/sandbox": "0.20.0",
25
+ "@ton/ton": "13.11.2",
26
+ "async-mutex": "^0.5.0",
27
+ "bigi": "^1.4.2",
28
+ "json-bigint": "^1.0.0",
29
+ "lowdb": "^1.0.0",
30
+ "moment": "^2.30.1",
31
+ "winston": "^3.17.0",
32
+ "winston-daily-rotate-file": "^5.0.0"
33
+ }
34
+ }
package/publish.sh ADDED
@@ -0,0 +1,78 @@
1
+ #!/bin/bash
2
+
3
+ # ------------------- 配置 -------------------
4
+ PROJECT_ROOT=$(pwd) # 项目根目录,默认为脚本执行目录
5
+ OUTPUT_DIR="dist" # TypeScript 编译输出目录
6
+ NPM_COMMAND="npm" # npm 命令,如果你的环境不同可以修改
7
+ BUILD_COMMAND="npm run build" # 执行 TypeScript 编译的命令
8
+ TEST_COMMAND="npm run test" # 执行测试的命令 (可选)
9
+ DRY_RUN=false # 是否进行 npm publish 的 dry-run (模拟发布)
10
+ # --------------------------------------------
11
+
12
+ echo "开始自动编译和发布 TypeScript 项目..."
13
+ echo "项目根目录: $PROJECT_ROOT"
14
+ echo "输出目录: $OUTPUT_DIR"
15
+
16
+ # 切换到项目根目录
17
+ cd "$PROJECT_ROOT" || {
18
+ echo "错误: 无法切换到项目根目录。"
19
+ exit 1
20
+ }
21
+
22
+ echo "当前工作目录: $(pwd)"
23
+
24
+ # 检查 package.json 和 tsconfig.json 是否存在
25
+ if [ ! -f "package.json" ]; then
26
+ echo "错误: package.json 文件不存在。"
27
+ exit 1
28
+ fi
29
+
30
+ if [ ! -f "tsconfig.json" ]; then
31
+ echo "错误: tsconfig.json 文件不存在。"
32
+ exit 1
33
+ fi
34
+
35
+ echo "package.json 和 tsconfig.json 文件存在。"
36
+
37
+ # 执行构建命令
38
+ echo "执行构建命令: $BUILD_COMMAND"
39
+ if ! $BUILD_COMMAND; then
40
+ echo "错误: TypeScript 编译失败。"
41
+ exit 1
42
+ fi
43
+
44
+ echo "TypeScript 编译成功,文件输出到 $OUTPUT_DIR 目录。"
45
+
46
+ # 执行测试命令 (可选)
47
+ #if [ -n "$TEST_COMMAND" ]; then
48
+ # echo "执行测试命令: $TEST_COMMAND"
49
+ # if ! $TEST_COMMAND; then
50
+ # echo "警告: 测试失败,但继续发布。"
51
+ # # 如果你希望测试失败就停止发布,取消下一行的注释
52
+ # # exit 1
53
+ # fi
54
+ # echo "测试通过。"
55
+ #fi
56
+
57
+ # 发布 npm 包
58
+ echo "准备发布 npm 包..."
59
+
60
+ if $DRY_RUN; then
61
+ echo "执行 npm publish --dry-run (模拟发布)..."
62
+ if ! $NPM_COMMAND publish --dry-run; then
63
+ echo "错误: npm publish --dry-run 失败,请检查你的 npm 配置。"
64
+ exit 1
65
+ fi
66
+ echo "npm publish --dry-run 成功。"
67
+ else
68
+ echo "执行 npm publish..."
69
+ if ! $NPM_COMMAND publish; then
70
+ echo "错误: npm publish 失败,请检查你的 npm 配置和包版本。"
71
+ exit 1
72
+ fi
73
+ echo "npm 包发布成功!"
74
+ fi
75
+
76
+ echo "脚本执行完毕。"
77
+
78
+ exit 0
@@ -0,0 +1,47 @@
1
+ import {
2
+ Address,
3
+ beginCell,
4
+ } from '@ton/core';
5
+
6
+ import {logger} from '../utils/logger'
7
+ const formatUtil = require('util');
8
+
9
+ export function computeHash(currentChainId: bigint, uniqueId: bigint, tokenPairId: bigint, value: bigint, fee: bigint, tokenAccount: Address | string, userAccount: Address) {
10
+
11
+ let tokenAccountBuf: Buffer
12
+
13
+ if (Address.isAddress(tokenAccount)) {
14
+ tokenAccountBuf = tokenAccount.hash;
15
+ } else {
16
+ tokenAccountBuf = Buffer.from(tokenAccount.substring(0, 2).toLowerCase() == '0x' ? tokenAccount.substring(2) : tokenAccount, 'hex');
17
+ }
18
+
19
+ let userAccountBuf = userAccount.hash;
20
+
21
+ let msg = beginCell()
22
+ .storeUint(currentChainId, 64)
23
+ .storeUint(uniqueId, 256)
24
+ .storeUint(tokenPairId, 32)
25
+ .storeUint(value, 256)
26
+ .storeUint(fee, 256)
27
+ .storeRef(
28
+ beginCell()
29
+ .storeAddress(userAccount)
30
+ .storeBuffer(tokenAccountBuf)
31
+ .endCell()
32
+ );
33
+ logger.info(formatUtil.format("jacob tokenAccount...", tokenAccount));
34
+ logger.info(formatUtil.format("jacob tokenAccountBuf......", tokenAccountBuf.toString('hex'), tokenAccountBuf.length));
35
+ logger.info(formatUtil.format("(bigInt)jacob tokenAccountBuf......", BigInt("0x" + tokenAccountBuf.toString('hex'))));
36
+
37
+ logger.info(formatUtil.format("jacob userAccount...", userAccount));
38
+ logger.info(formatUtil.format("jacob userAccountBuf......", userAccountBuf.toString('hex'), userAccountBuf.length));
39
+ logger.info(formatUtil.format("(bigInt)jacob userAccountBuf......", BigInt("0x" + userAccountBuf.toString('hex'))));
40
+ logger.info(formatUtil.format("user_account_cell(cell)", msg.endCell()));
41
+ let hashBuf = msg.endCell().hash();
42
+ return {
43
+ hashHex: hashBuf.toString('hex'),
44
+ hashBig: BigInt(`0x${hashBuf.toString('hex')}`),
45
+ hashBuf: hashBuf
46
+ };
47
+ }
@@ -0,0 +1,15 @@
1
+ {
2
+ "keep": {
3
+ "days": true,
4
+ "amount": 10
5
+ },
6
+ "auditLog": "log/.ba8901062aebb86bf525b42c65ff84f9485419e4-audit.json",
7
+ "files": [
8
+ {
9
+ "date": 1744604588044,
10
+ "name": "log/wan-ton-sdk.out.2025-04-14",
11
+ "hash": "94b71d3dfd41927c260dacc58b030c5acee210ef6952a6e7e491fdf4dcea1c6f"
12
+ }
13
+ ],
14
+ "hashType": "sha256"
15
+ }
@@ -0,0 +1,3 @@
1
+ 2025-04-14T04:23:08.458Z [info]: getClient config {"network":"testnet","tonClientTimeout":60000000}
2
+ 2025-04-14T04:23:10.581Z [info]: http endpoint is => https://ton.access.orbs.network/55033c0ff5Bd3F8B62C092Ab4D238bEE463E5503/1/testnet/toncenter-api-v2/jsonRPC
3
+ 2025-04-14T04:23:10.596Z [info]: fromBuffer,toBuffer: 833589fcd6edb6e08f4c7c32d4f71b54bda02913 0000000000000000000000000000000000000000000000000000000000000000
@@ -0,0 +1,117 @@
1
+ import {TON_FEE} from "../fee/fee";
2
+
3
+ import {configTestnet,configMainnet} from "../config/config-ex";
4
+
5
+ import {Cell, toNano} from "@ton/core";
6
+ import {
7
+ buildSignData,
8
+ signData,
9
+ buildRawTransaction,
10
+ sendRawTransaction,
11
+ buildInternalMessage
12
+ } from "./rawTrans";
13
+ import {OP_FEE_SetTokenPairFee, OP_TOKENPAIR_Upsert} from "../opcodes"
14
+ import {BIP44_CHAINID, TON_COIN_ACCOUT, TON_COIN_ACCOUNT_STR} from "../const/const-value";
15
+ import {codeTable} from "../code/encode-decode";
16
+ import {getClient, TonClientConfig} from "../client/client";
17
+ import {getSenderByPrvKey, getWalletByPrvKey, openWalletByPrvKey} from "../wallet/walletContract";
18
+ import {sign} from "@ton/crypto";
19
+ import {SendMode} from "@ton/core";
20
+ import {getQueryID, sleep} from "../utils/utils";
21
+ import {getTranResultByMsgHash} from "../transResult/transResult";
22
+
23
+ let jettonAdmin = TON_COIN_ACCOUNT_STR;
24
+
25
+ let tokenInfo = {
26
+ tokenOrg:{tokenPairId:0x01,srcChainId:0x1234,dstChainId:BIP44_CHAINID,srcTokenAcc:"0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",dstTokenAcc:'',},
27
+ tokenWrapped:{tokenPairId:0x02,srcChainId:0x1234,dstChainId:BIP44_CHAINID,srcTokenAcc:"0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",dstTokenAcc:'',},
28
+ coin:{tokenPairId:0x03,srcChainId:0x1234,dstChainId:BIP44_CHAINID,srcTokenAcc:"0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",dstTokenAcc:''},
29
+ }
30
+ let queryID ;
31
+ let tokenPairId3 = tokenInfo.coin.tokenPairId;
32
+ let srcChainId3 = tokenInfo.coin.srcChainId;
33
+ let dstChainId3 = tokenInfo.coin.dstChainId;
34
+ let srcTokenAcc3 = tokenInfo.coin.srcTokenAcc;
35
+ let dstTokenAcc3 = tokenInfo.coin.dstTokenAcc;
36
+ tokenInfo.coin.dstTokenAcc = TON_COIN_ACCOUT;
37
+
38
+ const scAddresses = require('../testData/contractAddress.json');
39
+ let prvList = require('../testData/prvlist.json')
40
+
41
+ let client = null;
42
+ let wallet = null;
43
+ let walletOpenned = null;
44
+ let privateKey = Buffer.from(prvList[0],'hex')
45
+
46
+ describe('sendRawTransaction', () => {
47
+
48
+ beforeAll(async () => {
49
+ client = await getClient();
50
+ wallet = await getWalletByPrvKey(privateKey);
51
+ walletOpenned = await openWalletByPrvKey(client , privateKey);
52
+ queryID = await getQueryID();
53
+ },50000);
54
+
55
+ it('build rawTrans, sign, and send rawTrans', async () => {
56
+ // build body
57
+ let opt = {
58
+ value: TON_FEE.TRANS_FEE_NORMAL,
59
+ queryID,
60
+ tokenPairId: tokenPairId3,
61
+ fromChainID: srcChainId3,
62
+ fromAccount: srcTokenAcc3,
63
+ toChainID: dstChainId3,
64
+ toAccount: dstTokenAcc3,
65
+ jettonAdminAddr:jettonAdmin,
66
+ }
67
+ let bodyCell = codeTable[OP_TOKENPAIR_Upsert]["enCode"](opt);
68
+
69
+ // build message
70
+ let message = await buildInternalMessage(wallet.address,{
71
+ to:scAddresses.bridgeAddress,
72
+ value:TON_FEE.TRANS_FEE_NORMAL,
73
+ bounce:true,
74
+ body:bodyCell,
75
+ })
76
+
77
+ // build data for sign
78
+ let seqno = await walletOpenned.getSeqno();
79
+ let walletId = 698983191; // provide by user
80
+ let timeoutTimeStamp = Math.floor(Date.now() / 1e3) + 60; // experiment timestamp.
81
+ let dataNeedSign = await buildSignData({
82
+ seqno:seqno,
83
+ sendMode:SendMode.PAY_GAS_SEPARATELY,
84
+ walletId:walletId,
85
+ messages:[message],
86
+ timeout:timeoutTimeStamp,
87
+ });
88
+
89
+ for(let msgHash of dataNeedSign.msgHashs){
90
+ console.log("msgHash is ",msgHash);
91
+ }
92
+
93
+ console.log("hash of signData =>",dataNeedSign.hash.toString('hex'));
94
+
95
+ // sign data
96
+ let signature = sign(dataNeedSign.hash,privateKey);
97
+
98
+ // build rawTrans
99
+ let rawTrans = await buildRawTransaction(signature,dataNeedSign.rawDataBuilder);
100
+
101
+ // send raw trans
102
+ console.log("wallet.address=>",wallet.address);
103
+ let ret = await sendRawTransaction(client,wallet.address,rawTrans);
104
+ console.log("ret of sendRawTransaction ==>",ret);
105
+
106
+ await sleep(10000);
107
+ // for(let msgHash of dataNeedSign.msgHashs){
108
+ // let ret1 = await getTranResultByMsgHash(client,scAddresses.bridgeAddress,msgHash);
109
+ // console.log(ret1);
110
+ // }
111
+
112
+ let ret1 = await getTranResultByMsgHash(client,ret.scAddr,ret.msgBodyCellHash, ret.externalInMessageHash);
113
+ console.log("*******************TranResult=>",ret1);
114
+
115
+ },500000);
116
+
117
+ });
@@ -0,0 +1,122 @@
1
+ import {bigIntReplacer} from "../utils/utils";
2
+
3
+ import {configTestnet,configMainnet} from "../config/config-ex";
4
+ import {
5
+ Address,
6
+ beginCell,
7
+ Builder,
8
+ Cell,
9
+ MessageRelaxed,
10
+ storeMessage,
11
+ Message,
12
+ storeMessageRelaxed,
13
+ external
14
+ } from "@ton/core";
15
+ import {Maybe} from "@ton/ton/dist/utils/maybe";
16
+ import {sign} from "@ton/crypto";
17
+ import {getClient, TonClientConfig} from "../client/client";
18
+ import {internal} from "@ton/core";
19
+ import {StateInit} from "@ton/core";
20
+ import {TonClient} from "@ton/ton";
21
+
22
+ export async function buildInternalMessage(from:Address,src: {
23
+ to: Address | string,
24
+ value: bigint | string,
25
+ bounce?: Maybe<boolean>,
26
+ init?: Maybe<StateInit>,
27
+ body?: Maybe<Cell | string>
28
+ }){
29
+ let msgRelaxed = await internal(src);
30
+ msgRelaxed.info.src = from;
31
+ return msgRelaxed as unknown as Message;
32
+ }
33
+
34
+ export async function buildExternalMessage(src: {
35
+ to: Address | string,
36
+ init?: Maybe<StateInit>,
37
+ body?: Maybe<Cell>
38
+ }){
39
+ return await external(src);
40
+ }
41
+
42
+ export async function buildSignData(args: {
43
+ seqno: number;
44
+ sendMode: number;
45
+ walletId: number;
46
+ messages: Message[];
47
+ timeout?: Maybe<number>;
48
+ }){
49
+ // Check number of messages
50
+ if (args.messages.length > 4) {
51
+ throw Error("Maximum number of messages in a single transfer is 4");
52
+ }
53
+ let signingMessage = beginCell()
54
+ .storeUint(args.walletId, 32);
55
+ if (args.seqno === 0) {
56
+ for (let i = 0; i < 32; i++) {
57
+ signingMessage.storeBit(1);
58
+ }
59
+ }
60
+ else {
61
+ signingMessage.storeUint(args.timeout || Math.floor(Date.now() / 1e3) + 60, 32); // Default timeout: 60 seconds
62
+ }
63
+ signingMessage.storeUint(args.seqno, 32);
64
+ signingMessage.storeUint(0, 8); // Simple order
65
+ for (let m of args.messages) {
66
+ signingMessage.storeUint(args.sendMode, 8);
67
+ //signingMessage.storeRef(beginCell().store(storeMessageRelaxed(m)));
68
+ signingMessage.storeRef(beginCell().store(storeMessage(m)));
69
+ }
70
+ return {
71
+ rawData: signingMessage.endCell(),
72
+ hash:signingMessage.endCell().hash(),
73
+ rawDataBuilder:signingMessage,
74
+ msgHashs:await getMsgHash(args.messages),
75
+ }
76
+ }
77
+
78
+ export async function signData(hash:Buffer,secretKey:Buffer) {
79
+ return sign(hash,secretKey)}
80
+
81
+ export async function buildRawTransaction(signature:Buffer, rawDataBuilder:Builder){
82
+ return beginCell()
83
+ .storeBuffer(signature)
84
+ .storeBuilder(rawDataBuilder)
85
+ .endCell();
86
+ }
87
+
88
+ /*
89
+ external_message = {
90
+ to: <wallet_sc_address>,
91
+ init?: neededInit,
92
+ body: rawTransactionCell
93
+ }
94
+ */
95
+ export async function sendRawTransaction(client:TonClient, senderAddress:Address,rawTrans:Cell){
96
+ // let client = await getClient();
97
+ let provider = client.provider(senderAddress)
98
+ await provider.external(rawTrans)
99
+
100
+
101
+ let externalInMessage = await buildExternalMessage({
102
+ to:senderAddress,
103
+ body:rawTrans,
104
+ })
105
+ return {
106
+ scAddr:senderAddress,
107
+ msgBodyCellHash:rawTrans.hash().toString('hex'),
108
+ externalInMessageHash:beginCell().store(storeMessage(externalInMessage)).endCell().hash().toString('hex'),
109
+ }
110
+ }
111
+
112
+
113
+
114
+
115
+ export function getMsgHash(msgs:Message[]){
116
+ let ret = [];
117
+ for(let m of msgs){
118
+ console.log("==========msg=>",JSON.stringify(m,bigIntReplacer));
119
+ ret.push(beginCell().store(storeMessage(m)).endCell().hash().toString('hex'));
120
+ }
121
+ return ret;
122
+ }
@@ -0,0 +1,210 @@
1
+ const crypto = require('crypto');
2
+ const BigInteger = require('bigi');
3
+ const ecurve = require('ecurve');
4
+ const Web3EthAbi = require('web3-eth-abi');
5
+ const ecparams = ecurve.getCurveByName('secp256k1');
6
+ const Web3Utils = require("web3-utils");
7
+
8
+ const logger = require('../utils/logger').logger
9
+ const formatUtil = require('util');
10
+
11
+ // buffer
12
+ const r = new Buffer("e7e59bebdcee876e84d03832544f5a517e96a9e3f60cd8f564bece6719d5af52", 'hex');
13
+ // buffer
14
+ let R = baseScarMulti(r);
15
+
16
+ // sk*G
17
+ // return: buff
18
+ function baseScarMulti(sk) {
19
+ let curvePt = ecparams.G.multiply(BigInteger.fromBuffer(sk));
20
+ let ret = curvePt.getEncoded(false);
21
+ let str = ret.toString('hex');
22
+ str = str.slice(2);
23
+ return Buffer.from(str, 'hex');
24
+ }
25
+
26
+ // hash
27
+ // return:buffer
28
+ function h(buff) {
29
+ let sha = crypto.createHash('sha256').update(buff).digest();
30
+ return sha;
31
+ }
32
+
33
+ // get s
34
+ // s = r+sk*m
35
+ // return: buffer
36
+ function getSBuff(sk, m) {
37
+ let rBig = BigInteger.fromBuffer(r);
38
+ let skBig = BigInteger.fromBuffer(sk);
39
+ let mBig = BigInteger.fromBuffer(m);
40
+ let retBig;
41
+ retBig = rBig.add(skBig.multiply(mBig).mod(ecparams.n)).mod(ecparams.n);
42
+ return retBig.toBuffer(32);
43
+ }
44
+
45
+ // return: buffer
46
+ function computeM1(M) {
47
+ let M1 = h(M);
48
+ return M1;
49
+ }
50
+
51
+ // compute m
52
+ // M1=hash(M)
53
+ // m=hash(M1||R)
54
+ // M: buffer
55
+ // R: buffer
56
+ // return: buffer
57
+ function computem(M1, R) {
58
+ let list = [];
59
+ list.push(M1);
60
+ list.push(R);
61
+ // hash(M1||R)
62
+ let m = Buffer.concat(list);
63
+ return h(m)
64
+ }
65
+
66
+ // input:pk Buffer
67
+ // return buffer (1byte)
68
+ function getPararity(pk) {
69
+ let b = BigInteger.fromBuffer(pk.slice(32)) // get pkY
70
+ if (b.testBit(0)) { // odd
71
+ return Buffer.from("1c", 'hex');
72
+ } else {
73
+ return Buffer.from("1b", 'hex');
74
+ }
75
+ }
76
+
77
+ // pk: Buffer
78
+ // return buffer (20bytes)
79
+ function getAddrFromPk(pk) {
80
+ logger.info(formatUtil.format("len of pk", pk.length));
81
+ const publicKeyToAddress = require('ethereum-public-key-to-address')
82
+ return publicKeyToAddress(pk)
83
+ }
84
+
85
+ // pk: Buffer (64bytes)
86
+ // return buffer (32bytes)
87
+ function getXBytesFromPk(pk) {
88
+ return pk.slice(0, 32)
89
+ }
90
+
91
+ function getYBytesFromPk(pk) {
92
+ return pk.slice(32)
93
+ }
94
+
95
+ // input Buffer
96
+ //
97
+ // output Buffer
98
+ // hash(R_addr||gpk[0]+27-2||gpk.XBytes||message_hash)
99
+ function computeE(RBuf, pararityBuf, gpkXBuf, MBuf) {
100
+ let list = [];
101
+ //list.push(RBuf);
102
+ logger.info(formatUtil.format("RBuf", bufferToHexString(RBuf)));
103
+ let RAddr = getAddrFromPk(Buffer.concat([Buffer.from("04", 'hex'), RBuf]));
104
+ logger.info(formatUtil.format("RAddr", RAddr))
105
+
106
+
107
+ let RAddrBytes = Buffer.from(RAddr.substring(2), 'hex');
108
+
109
+ list.push(RAddrBytes);
110
+ logger.info(formatUtil.format("R_addr", bufferToHexString(RAddrBytes)))
111
+
112
+ list.push(pararityBuf);
113
+ logger.info(formatUtil.format("pararityBuf", bufferToHexString(pararityBuf)));
114
+
115
+ list.push(gpkXBuf);
116
+ logger.info(formatUtil.format("gpkXBuf", bufferToHexString(gpkXBuf)));
117
+
118
+ list.push(MBuf);
119
+ logger.info(formatUtil.format("MBuf", bufferToHexString(MBuf)));
120
+
121
+ let eBuffer = Buffer.concat(list);
122
+ logger.info(formatUtil.format("eBuf before hash", bufferToHexString(eBuffer)));
123
+ return (Web3Utils.keccak256(eBuffer))
124
+ }
125
+
126
+
127
+ //typesArray:['uint256','string']
128
+ //parameters: ['2345675643', 'Hello!%']
129
+ //return : buff
130
+ function computeM(typesArray, parameters) {
131
+ let mStrHex = Web3EthAbi.encodeParameters(typesArray, parameters);
132
+ return new Buffer(mStrHex.substring(2), 'hex');
133
+ }
134
+
135
+ // return : hexString
136
+ function getR() {
137
+ return "0x" + R.toString('hex');
138
+ }
139
+
140
+ // return: hexString
141
+ function bufferToHexString(buff) {
142
+ return "0x" + buff.toString('hex');
143
+ }
144
+
145
+ // sk: buff
146
+ // return: hexString
147
+ function getPKBySk(sk) {
148
+ return bufferToHexString(baseScarMulti(sk));
149
+ }
150
+
151
+ //typesArray:['uint256','string']
152
+ //parameters: ['2345675643', 'Hello!%']
153
+ //return :hexString
154
+ function getS(sk, typesArray, parameters) {
155
+ let MBuff = computeM(typesArray, parameters);
156
+ let M1Buff = computeM1(MBuff);
157
+ let mBuff = computem(M1Buff, R);
158
+ let sBuff = getSBuff(sk, mBuff);
159
+ return bufferToHexString(sBuff);
160
+ }
161
+
162
+ //typesArray:['uint256','string']
163
+ //parameters: ['2345675643', 'Hello!%']
164
+ //return :hexString
165
+ function getSecSchnorrS(sk, typesArray, parameters) {
166
+ let MBuff = computeM(typesArray, parameters);
167
+ let M1Buff = computeM1(MBuff);
168
+ let pk = getPKBySk(sk);
169
+ let pkBuf = Buffer.from(pk.substring(2), 'hex');
170
+ let eBuffHexStr = computeE(R, getPararity(pkBuf), getXBytesFromPk(pkBuf), M1Buff);
171
+ logger.info(formatUtil.format("eBuf after hash", eBuffHexStr));
172
+ let sBuff = getSBuff(sk, Buffer.from(eBuffHexStr.substring(2), 'hex'));
173
+ return {
174
+ s: bufferToHexString(sBuff),
175
+ e: (eBuffHexStr),
176
+ p: bufferToHexString(getPararity(pkBuf)),
177
+ pkX: bufferToHexString(getXBytesFromPk(pkBuf)),
178
+ pkY: bufferToHexString(getYBytesFromPk(pkBuf)),
179
+ msgHash: bufferToHexString(M1Buff),
180
+ }
181
+ }
182
+ // msgHash: buffer
183
+ function getSecSchnorrSByMsgHash(sk, msgHash) {
184
+ // let MBuff = computeM(typesArray, parameters);
185
+ // let M1Buff = computeM1(MBuff);
186
+ console.log("getSecSchnorrSByMsgHash sk=>",sk);
187
+ let pk = getPKBySk(sk);
188
+ let pkBuf = Buffer.from(pk.substring(2), 'hex');
189
+ let eBuffHexStr = computeE(R, getPararity(pkBuf), getXBytesFromPk(pkBuf), msgHash);
190
+ logger.info(formatUtil.format("eBuf after hash", eBuffHexStr));
191
+ let sBuff = getSBuff(sk, Buffer.from(eBuffHexStr.substring(2), 'hex'));
192
+ return {
193
+ s: bufferToHexString(sBuff),
194
+ e: eBuffHexStr,
195
+ p: bufferToHexString(getPararity(pkBuf)),
196
+ pkX: bufferToHexString(getXBytesFromPk(pkBuf)),
197
+ pkY: bufferToHexString(getYBytesFromPk(pkBuf)),
198
+ msgHash: bufferToHexString(msgHash),
199
+ }
200
+ }
201
+
202
+ module.exports = {
203
+ getS: getS,
204
+ getPKBySk: getPKBySk,
205
+ getR: getR,
206
+ computeE: computeE,
207
+ getPararity: getPararity,
208
+ getSecSchnorrS: getSecSchnorrS,
209
+ getSecSchnorrSByMsgHash:getSecSchnorrSByMsgHash,
210
+ };