@1money/protocol-ts-sdk 1.1.2 → 2.0.0
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/.claude/settings.local.json +2 -1
- package/README.md +216 -134
- package/es/__integration__/helpers.d.ts +0 -7
- package/es/api/checkpoints/types.d.ts +0 -1
- package/es/api/index.js +17 -10
- package/es/api/tokens/index.d.ts +7 -1
- package/es/api/tokens/types.d.ts +21 -6
- package/es/api/transactions/index.d.ts +4 -3
- package/es/api/transactions/types.d.ts +14 -5
- package/es/client/index.js +4 -4
- package/es/index.d.ts +1 -0
- package/es/index.js +481 -22
- package/es/signing/builders/index.d.ts +11 -0
- package/es/signing/builders/payment.d.ts +3 -0
- package/es/signing/builders/tokenAuthority.d.ts +3 -0
- package/es/signing/builders/tokenBridgeAndMint.d.ts +3 -0
- package/es/signing/builders/tokenBurn.d.ts +3 -0
- package/es/signing/builders/tokenBurnAndBridge.d.ts +3 -0
- package/es/signing/builders/tokenClawback.d.ts +3 -0
- package/es/signing/builders/tokenIssue.d.ts +3 -0
- package/es/signing/builders/tokenManageList.d.ts +3 -0
- package/es/signing/builders/tokenMetadata.d.ts +3 -0
- package/es/signing/builders/tokenMint.d.ts +3 -0
- package/es/signing/builders/tokenPause.d.ts +3 -0
- package/es/signing/builders/validate.d.ts +18 -0
- package/es/signing/core.d.ts +27 -0
- package/es/signing/index.d.ts +18 -0
- package/es/signing/signer.d.ts +3 -0
- package/es/utils/encode.d.ts +11 -0
- package/es/utils/index.d.ts +2 -1
- package/es/utils/index.js +90 -10
- package/es/utils/interface.d.ts +27 -0
- package/es/utils/sign.d.ts +6 -1
- package/lib/__integration__/helpers.d.ts +0 -7
- package/lib/api/checkpoints/types.d.ts +0 -1
- package/lib/api/index.js +17 -10
- package/lib/api/tokens/index.d.ts +7 -1
- package/lib/api/tokens/types.d.ts +21 -6
- package/lib/api/transactions/index.d.ts +4 -3
- package/lib/api/transactions/types.d.ts +14 -5
- package/lib/client/index.js +4 -4
- package/lib/index.d.ts +1 -0
- package/lib/index.js +469 -21
- package/lib/signing/builders/index.d.ts +11 -0
- package/lib/signing/builders/payment.d.ts +3 -0
- package/lib/signing/builders/tokenAuthority.d.ts +3 -0
- package/lib/signing/builders/tokenBridgeAndMint.d.ts +3 -0
- package/lib/signing/builders/tokenBurn.d.ts +3 -0
- package/lib/signing/builders/tokenBurnAndBridge.d.ts +3 -0
- package/lib/signing/builders/tokenClawback.d.ts +3 -0
- package/lib/signing/builders/tokenIssue.d.ts +3 -0
- package/lib/signing/builders/tokenManageList.d.ts +3 -0
- package/lib/signing/builders/tokenMetadata.d.ts +3 -0
- package/lib/signing/builders/tokenMint.d.ts +3 -0
- package/lib/signing/builders/tokenPause.d.ts +3 -0
- package/lib/signing/builders/validate.d.ts +18 -0
- package/lib/signing/core.d.ts +27 -0
- package/lib/signing/index.d.ts +18 -0
- package/lib/signing/signer.d.ts +3 -0
- package/lib/utils/encode.d.ts +11 -0
- package/lib/utils/index.d.ts +2 -1
- package/lib/utils/index.js +90 -10
- package/lib/utils/interface.d.ts +27 -0
- package/lib/utils/sign.d.ts +6 -1
- package/package.json +2 -2
- package/umd/1money-protocol-ts-sdk.min.js +3 -2
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AuthorityType, RestSignature } from '../tokens/types';
|
|
2
|
-
import { AddressSchema, B256Schema } from '../types';
|
|
2
|
+
import type { AddressSchema, B256Schema, BytesSchema } from '../types';
|
|
3
3
|
export interface TransactionReceipt {
|
|
4
4
|
success: boolean;
|
|
5
5
|
transaction_hash: B256Schema;
|
|
@@ -71,18 +71,24 @@ export interface TokenBridgeAndMintData {
|
|
|
71
71
|
value: string;
|
|
72
72
|
}
|
|
73
73
|
export interface TokenBurnData {
|
|
74
|
-
recipient: AddressSchema;
|
|
75
74
|
token: AddressSchema;
|
|
76
75
|
value: string;
|
|
77
76
|
}
|
|
78
77
|
export interface TokenBurnAndBridgeData {
|
|
79
|
-
|
|
80
|
-
|
|
78
|
+
value: string;
|
|
79
|
+
sender: AddressSchema;
|
|
81
80
|
destination_chain_id: number;
|
|
81
|
+
destination_address: AddressSchema;
|
|
82
82
|
escrow_fee: string;
|
|
83
|
-
|
|
83
|
+
bridge_metadata: string | null;
|
|
84
|
+
bridge_param: BytesSchema;
|
|
84
85
|
token: AddressSchema;
|
|
86
|
+
}
|
|
87
|
+
export interface TokenClawbackData {
|
|
88
|
+
from: AddressSchema;
|
|
89
|
+
recipient: AddressSchema;
|
|
85
90
|
value: string;
|
|
91
|
+
token: AddressSchema;
|
|
86
92
|
}
|
|
87
93
|
export interface TokenCloseAccountData {
|
|
88
94
|
token: AddressSchema;
|
|
@@ -152,6 +158,9 @@ export type Transaction = (BaseTransaction & {
|
|
|
152
158
|
}) | (BaseTransaction & {
|
|
153
159
|
transaction_type: 'TokenBurnAndBridge';
|
|
154
160
|
data: TokenBurnAndBridgeData;
|
|
161
|
+
}) | (BaseTransaction & {
|
|
162
|
+
transaction_type: 'TokenClawback';
|
|
163
|
+
data: TokenClawbackData;
|
|
155
164
|
}) | (BaseTransaction & {
|
|
156
165
|
transaction_type: 'TokenCloseAccount';
|
|
157
166
|
data: TokenCloseAccountData;
|
package/es/client/index.js
CHANGED
|
@@ -46,7 +46,7 @@ import'viem';import'@ethereumjs/rlp';import axios from'axios';function _typeof(e
|
|
|
46
46
|
this._restScope = scope || this._restScope;
|
|
47
47
|
// @ts-ignore
|
|
48
48
|
if (this._restScope.length === 0) {
|
|
49
|
-
console.warn('[1Money
|
|
49
|
+
console.warn('[1Money SDK]: The ".rest(cb, scope)" scope is empty and will never be triggered!');
|
|
50
50
|
}
|
|
51
51
|
else {
|
|
52
52
|
let deletedCounter = 0;
|
|
@@ -61,7 +61,7 @@ import'viem';import'@ethereumjs/rlp';import axios from'axios';function _typeof(e
|
|
|
61
61
|
}
|
|
62
62
|
});
|
|
63
63
|
if (deletedCounter === this._restScope.length) {
|
|
64
|
-
console.warn(`[1Money
|
|
64
|
+
console.warn(`[1Money SDK]: The "${this._restScope.join(', ')}" had been called and the "rest" will never be triggered!`);
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
67
|
return wrapper;
|
|
@@ -110,7 +110,7 @@ class Request {
|
|
|
110
110
|
}
|
|
111
111
|
setting(config) {
|
|
112
112
|
if (!config)
|
|
113
|
-
return console.warn('[1Money
|
|
113
|
+
return console.warn('[1Money SDK]: setting method required correct parameters!');
|
|
114
114
|
this._config = { ...this._config, ...config };
|
|
115
115
|
}
|
|
116
116
|
request(options) {
|
|
@@ -266,7 +266,7 @@ class Request {
|
|
|
266
266
|
return;
|
|
267
267
|
cleanup();
|
|
268
268
|
const data = err.response?.data ?? {};
|
|
269
|
-
console.error(`[1Money
|
|
269
|
+
console.error(`[1Money SDK]: Error(${err.status ?? 500}, ${err.code ?? 'UNKNOWN'}), Message: ${err.message}, Config: ${err.config?.method}, ${err.config?.baseURL ?? ''}, ${err.config?.url ?? ''}, ${JSON.stringify(err.config?.headers ?? {})}, Request: ${JSON.stringify(err.config?.data ?? {})}, Response: ${JSON.stringify(data)};`);
|
|
270
270
|
const status = err.response?.status ?? 500;
|
|
271
271
|
const headers = err.response?.headers ?? {};
|
|
272
272
|
try {
|
package/es/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { api } from './api/index.js';
|
|
2
2
|
export { api } from './api/index.js';
|
|
3
3
|
export { default as client } from './client/index.js';
|
|
4
|
+
export * from './signing';
|
|
4
5
|
export * from './utils/index.js';
|
|
5
6
|
export type { AxiosStatic } from 'axios';
|
|
6
7
|
export type { InitConfig, Options, ParsedError, PromiseWrapper } from './client/core';
|
package/es/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {hexToBytes as hexToBytes$1,stringToBytes,keccak256,bytesToHex as bytesToHex$1,numberToHex,stringToHex,boolToHex}from'viem';import {encode}from'@ethereumjs/rlp';import axios from'axios';/**
|
|
1
|
+
import {hexToBytes as hexToBytes$1,stringToBytes,keccak256,bytesToHex as bytesToHex$1,numberToHex,stringToHex,boolToHex,isAddress}from'viem';import {encode,decode}from'@ethereumjs/rlp';import axios from'axios';/**
|
|
2
2
|
* Derives the token account address given the wallet address and mint address.
|
|
3
3
|
*
|
|
4
4
|
* Address is 20 byte, 160 bits. Let's say if we want to support 50 billion
|
|
@@ -25,6 +25,81 @@ function deriveTokenAddress(walletAddress, mintAddress) {
|
|
|
25
25
|
const hashBytes = hexToBytes$1(hashHex);
|
|
26
26
|
const addressBytes = hashBytes.slice(12);
|
|
27
27
|
return bytesToHex$1(addressBytes);
|
|
28
|
+
}const ADDRESS_HEX_RE = /^0x[0-9a-fA-F]{40}$/;
|
|
29
|
+
const BYTES_HEX_RE = /^0x([0-9a-fA-F]{2})*$/;
|
|
30
|
+
function innerEncodeRlpPayload(value) {
|
|
31
|
+
if (value === null || value === undefined) {
|
|
32
|
+
return new Uint8Array([]);
|
|
33
|
+
}
|
|
34
|
+
switch (value.kind) {
|
|
35
|
+
case 'string':
|
|
36
|
+
return new TextEncoder().encode(value.value);
|
|
37
|
+
case 'uint': {
|
|
38
|
+
if (typeof value.value === 'string' &&
|
|
39
|
+
!/^\d+$/.test(value.value)) {
|
|
40
|
+
throw new Error(`[1Money SDK]: Invalid uint string: ${value.value}`);
|
|
41
|
+
}
|
|
42
|
+
const n = typeof value.value === 'bigint'
|
|
43
|
+
? value.value
|
|
44
|
+
: BigInt(value.value);
|
|
45
|
+
return n === BigInt(0)
|
|
46
|
+
? new Uint8Array([])
|
|
47
|
+
: hexToBytes$1(numberToHex(n));
|
|
48
|
+
}
|
|
49
|
+
case 'bool':
|
|
50
|
+
return value.value
|
|
51
|
+
? Uint8Array.from([1])
|
|
52
|
+
: new Uint8Array([]);
|
|
53
|
+
case 'bytes':
|
|
54
|
+
return value.value;
|
|
55
|
+
case 'list':
|
|
56
|
+
return value.value.map(v => innerEncodeRlpPayload(v));
|
|
57
|
+
case 'address':
|
|
58
|
+
case 'hex':
|
|
59
|
+
if (!BYTES_HEX_RE.test(value.value)) {
|
|
60
|
+
throw new Error(`[1Money SDK]: Invalid hex value: ${value.value}`);
|
|
61
|
+
}
|
|
62
|
+
if (value.kind === 'address' &&
|
|
63
|
+
!ADDRESS_HEX_RE.test(value.value)) {
|
|
64
|
+
throw new Error(`[1Money SDK]: Invalid address value: ${value.value}`);
|
|
65
|
+
}
|
|
66
|
+
return value.value === '0x'
|
|
67
|
+
? new Uint8Array([])
|
|
68
|
+
: hexToBytes$1(value.value);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const rlpValue = {
|
|
72
|
+
address: (v) => ({
|
|
73
|
+
kind: 'address',
|
|
74
|
+
value: v
|
|
75
|
+
}),
|
|
76
|
+
hex: (v) => ({
|
|
77
|
+
kind: 'hex',
|
|
78
|
+
value: v
|
|
79
|
+
}),
|
|
80
|
+
string: (v) => ({
|
|
81
|
+
kind: 'string',
|
|
82
|
+
value: v
|
|
83
|
+
}),
|
|
84
|
+
uint: (v) => ({
|
|
85
|
+
kind: 'uint',
|
|
86
|
+
value: v
|
|
87
|
+
}),
|
|
88
|
+
bool: (v) => ({
|
|
89
|
+
kind: 'bool',
|
|
90
|
+
value: v
|
|
91
|
+
}),
|
|
92
|
+
bytes: (v) => ({
|
|
93
|
+
kind: 'bytes',
|
|
94
|
+
value: v
|
|
95
|
+
}),
|
|
96
|
+
list: (v) => ({
|
|
97
|
+
kind: 'list',
|
|
98
|
+
value: v
|
|
99
|
+
})
|
|
100
|
+
};
|
|
101
|
+
function encodeRlpPayload(payload) {
|
|
102
|
+
return encode(innerEncodeRlpPayload(payload));
|
|
28
103
|
}// concurrent
|
|
29
104
|
function safePromiseAll(arr) {
|
|
30
105
|
// @ts-expect-error
|
|
@@ -46,11 +121,6 @@ async function safePromiseLine(arr) {
|
|
|
46
121
|
}
|
|
47
122
|
}
|
|
48
123
|
return res;
|
|
49
|
-
}function _typeof(ele) {
|
|
50
|
-
if (typeof ele !== 'object')
|
|
51
|
-
return (typeof ele).toLowerCase();
|
|
52
|
-
const typeStr = Object.prototype.toString.call(ele);
|
|
53
|
-
return typeStr.slice(8, typeStr.length - 1).toLowerCase();
|
|
54
124
|
}/*! noble-secp256k1 - MIT License (c) 2019 Paul Miller (paulmillr.com) */
|
|
55
125
|
/**
|
|
56
126
|
* 4KB JS implementation of secp256k1 ECDSA / Schnorr signatures & ECDH.
|
|
@@ -706,8 +776,15 @@ const wNAF = (n) => {
|
|
|
706
776
|
}
|
|
707
777
|
}
|
|
708
778
|
return { p, f }; // return both real and fake points for JIT
|
|
709
|
-
}
|
|
779
|
+
};function _typeof(ele) {
|
|
780
|
+
if (typeof ele !== 'object')
|
|
781
|
+
return (typeof ele).toLowerCase();
|
|
782
|
+
const typeStr = Object.prototype.toString.call(ele);
|
|
783
|
+
return typeStr.slice(8, typeStr.length - 1).toLowerCase();
|
|
784
|
+
}/**
|
|
710
785
|
* RLP encode a payload into a digest
|
|
786
|
+
* @deprecated Prefer the TransactionBuilder flow in `src/signing`
|
|
787
|
+
* (`prepare*Tx` + `.sign(...)`) to build and sign transactions.
|
|
711
788
|
* @param payload Payload to encode
|
|
712
789
|
* @returns RLP encoded payload
|
|
713
790
|
*/
|
|
@@ -715,9 +792,9 @@ function encodePayload(payload) {
|
|
|
715
792
|
if (_typeof(payload) === 'array') {
|
|
716
793
|
const formatted = payload.map((v) => {
|
|
717
794
|
if (_typeof(v) === 'string') {
|
|
718
|
-
if (/^0x[0-9a-fA-F]
|
|
795
|
+
if (/^0x([0-9a-fA-F]{2})*$/.test(v)) {
|
|
719
796
|
// hex-encoded data → raw bytes
|
|
720
|
-
return hexToBytes$1(v);
|
|
797
|
+
return v === '0x' ? new Uint8Array([]) : hexToBytes$1(v);
|
|
721
798
|
}
|
|
722
799
|
else if (/^\d+$/.test(v)) {
|
|
723
800
|
// number-like string → hex → bytes
|
|
@@ -749,6 +826,9 @@ function encodePayload(payload) {
|
|
|
749
826
|
}
|
|
750
827
|
/**
|
|
751
828
|
* Sign a message using the provided private key
|
|
829
|
+
* @deprecated Prefer the TransactionBuilder flow in `src/signing`
|
|
830
|
+
* (`prepare*Tx` + `.sign(createPrivateKeySigner(privateKey))`)
|
|
831
|
+
* for transaction signing.
|
|
752
832
|
* @param payload Payload to sign
|
|
753
833
|
* @param privateKey Private key to sign with
|
|
754
834
|
* @returns Signature object with r, s, v components
|
|
@@ -809,7 +889,7 @@ function toHex(value) {
|
|
|
809
889
|
}
|
|
810
890
|
}
|
|
811
891
|
catch (e) {
|
|
812
|
-
console.error('[1Money
|
|
892
|
+
console.error('[1Money SDK]: toHex error:', e);
|
|
813
893
|
return '0x';
|
|
814
894
|
}
|
|
815
895
|
}function encodeRlpListHeader(length) {
|
|
@@ -890,7 +970,7 @@ function calcTxHash(payload, signature) {
|
|
|
890
970
|
this._restScope = scope || this._restScope;
|
|
891
971
|
// @ts-ignore
|
|
892
972
|
if (this._restScope.length === 0) {
|
|
893
|
-
console.warn('[1Money
|
|
973
|
+
console.warn('[1Money SDK]: The ".rest(cb, scope)" scope is empty and will never be triggered!');
|
|
894
974
|
}
|
|
895
975
|
else {
|
|
896
976
|
let deletedCounter = 0;
|
|
@@ -905,7 +985,7 @@ function calcTxHash(payload, signature) {
|
|
|
905
985
|
}
|
|
906
986
|
});
|
|
907
987
|
if (deletedCounter === this._restScope.length) {
|
|
908
|
-
console.warn(`[1Money
|
|
988
|
+
console.warn(`[1Money SDK]: The "${this._restScope.join(', ')}" had been called and the "rest" will never be triggered!`);
|
|
909
989
|
}
|
|
910
990
|
}
|
|
911
991
|
return wrapper;
|
|
@@ -954,7 +1034,7 @@ class Request {
|
|
|
954
1034
|
}
|
|
955
1035
|
setting(config) {
|
|
956
1036
|
if (!config)
|
|
957
|
-
return console.warn('[1Money
|
|
1037
|
+
return console.warn('[1Money SDK]: setting method required correct parameters!');
|
|
958
1038
|
this._config = { ...this._config, ...config };
|
|
959
1039
|
}
|
|
960
1040
|
request(options) {
|
|
@@ -1110,7 +1190,7 @@ class Request {
|
|
|
1110
1190
|
return;
|
|
1111
1191
|
cleanup();
|
|
1112
1192
|
const data = err.response?.data ?? {};
|
|
1113
|
-
console.error(`[1Money
|
|
1193
|
+
console.error(`[1Money SDK]: Error(${err.status ?? 500}, ${err.code ?? 'UNKNOWN'}), Message: ${err.message}, Config: ${err.config?.method}, ${err.config?.baseURL ?? ''}, ${err.config?.url ?? ''}, ${JSON.stringify(err.config?.headers ?? {})}, Request: ${JSON.stringify(err.config?.data ?? {})}, Response: ${JSON.stringify(data)};`);
|
|
1114
1194
|
const status = err.response?.status ?? 500;
|
|
1115
1195
|
const headers = err.response?.headers ?? {};
|
|
1116
1196
|
try {
|
|
@@ -1389,6 +1469,14 @@ const tokensApi = {
|
|
|
1389
1469
|
*/
|
|
1390
1470
|
burnAndBridge: (payload) => {
|
|
1391
1471
|
return post(`${API_PREFIX$2}/burn_and_bridge`, payload, { withCredentials: false });
|
|
1472
|
+
},
|
|
1473
|
+
/**
|
|
1474
|
+
* Claw back tokens from a wallet
|
|
1475
|
+
* @param payload Token clawback request payload
|
|
1476
|
+
* @returns Promise with transaction hash response
|
|
1477
|
+
*/
|
|
1478
|
+
clawbackToken: (payload) => {
|
|
1479
|
+
return post(`${API_PREFIX$2}/clawback`, payload, { withCredentials: false });
|
|
1392
1480
|
}
|
|
1393
1481
|
};const API_PREFIX$1 = `/${API_VERSION}/transactions`;
|
|
1394
1482
|
/**
|
|
@@ -1422,15 +1510,13 @@ const transactionsApi = {
|
|
|
1422
1510
|
/**
|
|
1423
1511
|
* Estimate transaction fee
|
|
1424
1512
|
* @param from Address of the transaction author
|
|
1513
|
+
* @param to Address of the transaction recipient
|
|
1425
1514
|
* @param value Value of the transaction
|
|
1426
|
-
* @param token
|
|
1515
|
+
* @param token Token address
|
|
1427
1516
|
* @returns Promise with fee estimate response
|
|
1428
1517
|
*/
|
|
1429
|
-
estimateFee: (from, value, token) => {
|
|
1430
|
-
|
|
1431
|
-
if (token) {
|
|
1432
|
-
url += `&token=${token}`;
|
|
1433
|
-
}
|
|
1518
|
+
estimateFee: (from, to, value, token) => {
|
|
1519
|
+
const url = `${API_PREFIX$1}/estimate_fee?from=${from}&value=${value}&to=${to}&token=${token}`;
|
|
1434
1520
|
return get(url, { withCredentials: false });
|
|
1435
1521
|
},
|
|
1436
1522
|
/**
|
|
@@ -1462,6 +1548,7 @@ var AuthorityType;
|
|
|
1462
1548
|
AuthorityType["ManageList"] = "ManageList";
|
|
1463
1549
|
AuthorityType["UpdateMetadata"] = "UpdateMetadata";
|
|
1464
1550
|
AuthorityType["Bridge"] = "Bridge";
|
|
1551
|
+
AuthorityType["Clawback"] = "Clawback";
|
|
1465
1552
|
})(AuthorityType || (AuthorityType = {}));
|
|
1466
1553
|
var AuthorityAction;
|
|
1467
1554
|
(function (AuthorityAction) {
|
|
@@ -1528,7 +1615,379 @@ function api(options) {
|
|
|
1528
1615
|
*/
|
|
1529
1616
|
chain: chainApi,
|
|
1530
1617
|
};
|
|
1531
|
-
}
|
|
1618
|
+
}// secp256k1 curve order / 2 (maximum value for low-S signatures)
|
|
1619
|
+
// This prevents signature malleability by ensuring S is in the lower half of the curve order
|
|
1620
|
+
const SECP256K1_N_DIV_2 = BigInt('0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0');
|
|
1621
|
+
function validateSignature(signature) {
|
|
1622
|
+
// Validate that S value is in the lower half of the curve order
|
|
1623
|
+
// This prevents signature malleability attacks
|
|
1624
|
+
const s = BigInt(signature.s);
|
|
1625
|
+
if (s > SECP256K1_N_DIV_2) {
|
|
1626
|
+
throw new Error('[1Money SDK]: Invalid signature - high S value detected (potential malleability)');
|
|
1627
|
+
}
|
|
1628
|
+
}
|
|
1629
|
+
function calcSignedTxHash(payloadRlpBytes, signature) {
|
|
1630
|
+
// Decode the payload to get the transaction fields
|
|
1631
|
+
const payloadFields = decode(payloadRlpBytes);
|
|
1632
|
+
// Prepare v value based on its type
|
|
1633
|
+
const v = typeof signature.v === 'boolean'
|
|
1634
|
+
? signature.v
|
|
1635
|
+
? Uint8Array.from([1])
|
|
1636
|
+
: new Uint8Array([])
|
|
1637
|
+
: BigInt(signature.v);
|
|
1638
|
+
// Use library's encode to create the signed transaction structure: [[fields], v, r, s]
|
|
1639
|
+
const encoded = encode([
|
|
1640
|
+
payloadFields,
|
|
1641
|
+
v,
|
|
1642
|
+
hexToBytes$1(signature.r),
|
|
1643
|
+
hexToBytes$1(signature.s),
|
|
1644
|
+
]);
|
|
1645
|
+
return keccak256(encoded);
|
|
1646
|
+
}
|
|
1647
|
+
function createPreparedTx(params) {
|
|
1648
|
+
const signatureHash = keccak256(params.rlpBytes);
|
|
1649
|
+
const attachSignature = (signature) => {
|
|
1650
|
+
// Validate signature to prevent malleability attacks
|
|
1651
|
+
validateSignature(signature);
|
|
1652
|
+
return {
|
|
1653
|
+
kind: params.kind,
|
|
1654
|
+
unsigned: params.unsigned,
|
|
1655
|
+
signatureHash,
|
|
1656
|
+
txHash: calcSignedTxHash(params.rlpBytes, signature),
|
|
1657
|
+
signature,
|
|
1658
|
+
toRequest: () => params.toRequest(params.unsigned, signature),
|
|
1659
|
+
};
|
|
1660
|
+
};
|
|
1661
|
+
return {
|
|
1662
|
+
kind: params.kind,
|
|
1663
|
+
unsigned: params.unsigned,
|
|
1664
|
+
rlpBytes: params.rlpBytes,
|
|
1665
|
+
signatureHash,
|
|
1666
|
+
attachSignature,
|
|
1667
|
+
sign: async (signer) => attachSignature(await signer.signDigest(signatureHash)),
|
|
1668
|
+
};
|
|
1669
|
+
}const UINT_STRING_RE = /^\d+$/;
|
|
1670
|
+
function fail(name, value) {
|
|
1671
|
+
throw new Error(`[1Money SDK]: Invalid ${name}: ${String(value)}`);
|
|
1672
|
+
}
|
|
1673
|
+
function assertPositiveInteger(name, value) {
|
|
1674
|
+
if (!Number.isSafeInteger(value) ||
|
|
1675
|
+
value <= 0) {
|
|
1676
|
+
fail(name, value);
|
|
1677
|
+
}
|
|
1678
|
+
}
|
|
1679
|
+
function assertNonNegativeInteger(name, value) {
|
|
1680
|
+
if (!Number.isSafeInteger(value) ||
|
|
1681
|
+
value < 0) {
|
|
1682
|
+
fail(name, value);
|
|
1683
|
+
}
|
|
1684
|
+
}
|
|
1685
|
+
function assertUintString(name, value) {
|
|
1686
|
+
if (!UINT_STRING_RE.test(value)) {
|
|
1687
|
+
fail(name, value);
|
|
1688
|
+
}
|
|
1689
|
+
}
|
|
1690
|
+
function assertOptionalUintString(name, value) {
|
|
1691
|
+
if (value === undefined)
|
|
1692
|
+
return;
|
|
1693
|
+
assertUintString(name, value);
|
|
1694
|
+
}
|
|
1695
|
+
function assertAddress(name, value) {
|
|
1696
|
+
// viem's isAddress validates both format and EIP-55 checksum
|
|
1697
|
+
// It accepts: lowercase, uppercase, and correctly checksummed addresses
|
|
1698
|
+
// It rejects: invalid format or incorrect checksum (when mixed case is used)
|
|
1699
|
+
if (!isAddress(value)) {
|
|
1700
|
+
fail(name, value);
|
|
1701
|
+
}
|
|
1702
|
+
}
|
|
1703
|
+
function validateChainAndNonce(unsigned) {
|
|
1704
|
+
assertPositiveInteger('chain_id', unsigned.chain_id);
|
|
1705
|
+
assertNonNegativeInteger('nonce', unsigned.nonce);
|
|
1706
|
+
}
|
|
1707
|
+
function validateRecipientValueToken(unsigned) {
|
|
1708
|
+
assertAddress('recipient', unsigned.recipient);
|
|
1709
|
+
assertUintString('value', unsigned.value);
|
|
1710
|
+
assertAddress('token', unsigned.token);
|
|
1711
|
+
}
|
|
1712
|
+
function validateValueToken(unsigned) {
|
|
1713
|
+
assertUintString('value', unsigned.value);
|
|
1714
|
+
assertAddress('token', unsigned.token);
|
|
1715
|
+
}function preparePaymentTx(unsigned) {
|
|
1716
|
+
validateChainAndNonce(unsigned);
|
|
1717
|
+
validateRecipientValueToken(unsigned);
|
|
1718
|
+
const rlpBytes = encodeRlpPayload(rlpValue.list([
|
|
1719
|
+
rlpValue.uint(unsigned.chain_id),
|
|
1720
|
+
rlpValue.uint(unsigned.nonce),
|
|
1721
|
+
rlpValue.address(unsigned.recipient),
|
|
1722
|
+
rlpValue.uint(unsigned.value),
|
|
1723
|
+
rlpValue.address(unsigned.token),
|
|
1724
|
+
]));
|
|
1725
|
+
return createPreparedTx({
|
|
1726
|
+
kind: 'payment',
|
|
1727
|
+
unsigned,
|
|
1728
|
+
rlpBytes,
|
|
1729
|
+
toRequest: (payload, signature) => ({
|
|
1730
|
+
...payload,
|
|
1731
|
+
signature,
|
|
1732
|
+
}),
|
|
1733
|
+
});
|
|
1734
|
+
}function prepareTokenAuthorityTx(unsigned) {
|
|
1735
|
+
validateChainAndNonce(unsigned);
|
|
1736
|
+
assertAddress('authority_address', unsigned.authority_address);
|
|
1737
|
+
assertAddress('token', unsigned.token);
|
|
1738
|
+
assertOptionalUintString('value', unsigned.value);
|
|
1739
|
+
const values = [
|
|
1740
|
+
rlpValue.uint(unsigned.chain_id),
|
|
1741
|
+
rlpValue.uint(unsigned.nonce),
|
|
1742
|
+
rlpValue.string(unsigned.action),
|
|
1743
|
+
rlpValue.string(unsigned.authority_type),
|
|
1744
|
+
rlpValue.address(unsigned.authority_address),
|
|
1745
|
+
rlpValue.address(unsigned.token),
|
|
1746
|
+
];
|
|
1747
|
+
if (unsigned.value !== undefined) {
|
|
1748
|
+
values.push(rlpValue.uint(unsigned.value));
|
|
1749
|
+
}
|
|
1750
|
+
const rlpBytes = encodeRlpPayload(rlpValue.list(values));
|
|
1751
|
+
return createPreparedTx({
|
|
1752
|
+
kind: 'tokenAuthority',
|
|
1753
|
+
unsigned,
|
|
1754
|
+
rlpBytes,
|
|
1755
|
+
toRequest: (payload, signature) => ({
|
|
1756
|
+
...payload,
|
|
1757
|
+
signature,
|
|
1758
|
+
}),
|
|
1759
|
+
});
|
|
1760
|
+
}function prepareTokenBridgeAndMintTx(unsigned) {
|
|
1761
|
+
validateChainAndNonce(unsigned);
|
|
1762
|
+
validateRecipientValueToken(unsigned);
|
|
1763
|
+
assertPositiveInteger('source_chain_id', unsigned.source_chain_id);
|
|
1764
|
+
const rlpBytes = encodeRlpPayload(rlpValue.list([
|
|
1765
|
+
rlpValue.uint(unsigned.chain_id),
|
|
1766
|
+
rlpValue.uint(unsigned.nonce),
|
|
1767
|
+
rlpValue.address(unsigned.recipient),
|
|
1768
|
+
rlpValue.uint(unsigned.value),
|
|
1769
|
+
rlpValue.address(unsigned.token),
|
|
1770
|
+
rlpValue.uint(unsigned.source_chain_id),
|
|
1771
|
+
rlpValue.string(unsigned.source_tx_hash),
|
|
1772
|
+
rlpValue.string(unsigned.bridge_metadata),
|
|
1773
|
+
]));
|
|
1774
|
+
return createPreparedTx({
|
|
1775
|
+
kind: 'tokenBridgeAndMint',
|
|
1776
|
+
unsigned,
|
|
1777
|
+
rlpBytes,
|
|
1778
|
+
toRequest: (payload, signature) => ({
|
|
1779
|
+
...payload,
|
|
1780
|
+
signature,
|
|
1781
|
+
}),
|
|
1782
|
+
});
|
|
1783
|
+
}function prepareTokenBurnTx(unsigned) {
|
|
1784
|
+
validateChainAndNonce(unsigned);
|
|
1785
|
+
validateValueToken(unsigned);
|
|
1786
|
+
const rlpBytes = encodeRlpPayload(rlpValue.list([
|
|
1787
|
+
rlpValue.uint(unsigned.chain_id),
|
|
1788
|
+
rlpValue.uint(unsigned.nonce),
|
|
1789
|
+
rlpValue.uint(unsigned.value),
|
|
1790
|
+
rlpValue.address(unsigned.token),
|
|
1791
|
+
]));
|
|
1792
|
+
return createPreparedTx({
|
|
1793
|
+
kind: 'tokenBurn',
|
|
1794
|
+
unsigned,
|
|
1795
|
+
rlpBytes,
|
|
1796
|
+
toRequest: (payload, signature) => ({
|
|
1797
|
+
...payload,
|
|
1798
|
+
signature,
|
|
1799
|
+
}),
|
|
1800
|
+
});
|
|
1801
|
+
}function prepareTokenBurnAndBridgeTx(unsigned) {
|
|
1802
|
+
validateChainAndNonce(unsigned);
|
|
1803
|
+
assertAddress('sender', unsigned.sender);
|
|
1804
|
+
validateValueToken(unsigned);
|
|
1805
|
+
assertPositiveInteger('destination_chain_id', unsigned.destination_chain_id);
|
|
1806
|
+
assertAddress('destination_address', unsigned.destination_address);
|
|
1807
|
+
assertUintString('escrow_fee', unsigned.escrow_fee);
|
|
1808
|
+
const rlpBytes = encodeRlpPayload(rlpValue.list([
|
|
1809
|
+
rlpValue.uint(unsigned.chain_id),
|
|
1810
|
+
rlpValue.uint(unsigned.nonce),
|
|
1811
|
+
rlpValue.address(unsigned.sender),
|
|
1812
|
+
rlpValue.uint(unsigned.value),
|
|
1813
|
+
rlpValue.address(unsigned.token),
|
|
1814
|
+
rlpValue.uint(unsigned.destination_chain_id),
|
|
1815
|
+
rlpValue.string(unsigned.destination_address),
|
|
1816
|
+
rlpValue.uint(unsigned.escrow_fee),
|
|
1817
|
+
rlpValue.string(unsigned.bridge_metadata),
|
|
1818
|
+
rlpValue.hex(unsigned.bridge_param),
|
|
1819
|
+
]));
|
|
1820
|
+
return createPreparedTx({
|
|
1821
|
+
kind: 'tokenBurnAndBridge',
|
|
1822
|
+
unsigned,
|
|
1823
|
+
rlpBytes,
|
|
1824
|
+
toRequest: (payload, signature) => ({
|
|
1825
|
+
...payload,
|
|
1826
|
+
signature,
|
|
1827
|
+
}),
|
|
1828
|
+
});
|
|
1829
|
+
}function prepareTokenClawbackTx(unsigned) {
|
|
1830
|
+
validateChainAndNonce(unsigned);
|
|
1831
|
+
validateRecipientValueToken(unsigned);
|
|
1832
|
+
assertAddress('from', unsigned.from);
|
|
1833
|
+
const rlpBytes = encodeRlpPayload(rlpValue.list([
|
|
1834
|
+
rlpValue.uint(unsigned.chain_id),
|
|
1835
|
+
rlpValue.uint(unsigned.nonce),
|
|
1836
|
+
rlpValue.address(unsigned.token),
|
|
1837
|
+
rlpValue.address(unsigned.from),
|
|
1838
|
+
rlpValue.address(unsigned.recipient),
|
|
1839
|
+
rlpValue.uint(unsigned.value),
|
|
1840
|
+
]));
|
|
1841
|
+
return createPreparedTx({
|
|
1842
|
+
kind: 'tokenClawback',
|
|
1843
|
+
unsigned,
|
|
1844
|
+
rlpBytes,
|
|
1845
|
+
toRequest: (payload, signature) => ({
|
|
1846
|
+
...payload,
|
|
1847
|
+
signature,
|
|
1848
|
+
}),
|
|
1849
|
+
});
|
|
1850
|
+
}function prepareTokenIssueTx(unsigned) {
|
|
1851
|
+
validateChainAndNonce(unsigned);
|
|
1852
|
+
assertNonNegativeInteger('decimals', unsigned.decimals);
|
|
1853
|
+
assertAddress('master_authority', unsigned.master_authority);
|
|
1854
|
+
const clawbackEnabled = unsigned.clawback_enabled ?? true;
|
|
1855
|
+
const unsignedWithDefaults = {
|
|
1856
|
+
...unsigned,
|
|
1857
|
+
clawback_enabled: clawbackEnabled,
|
|
1858
|
+
};
|
|
1859
|
+
const rlpBytes = encodeRlpPayload(rlpValue.list([
|
|
1860
|
+
rlpValue.uint(unsignedWithDefaults.chain_id),
|
|
1861
|
+
rlpValue.uint(unsignedWithDefaults.nonce),
|
|
1862
|
+
rlpValue.string(unsignedWithDefaults.symbol),
|
|
1863
|
+
rlpValue.string(unsignedWithDefaults.name),
|
|
1864
|
+
rlpValue.uint(unsignedWithDefaults.decimals),
|
|
1865
|
+
rlpValue.address(unsignedWithDefaults.master_authority),
|
|
1866
|
+
rlpValue.bool(unsignedWithDefaults.is_private),
|
|
1867
|
+
rlpValue.bool(clawbackEnabled),
|
|
1868
|
+
]));
|
|
1869
|
+
return createPreparedTx({
|
|
1870
|
+
kind: 'tokenIssue',
|
|
1871
|
+
unsigned: unsignedWithDefaults,
|
|
1872
|
+
rlpBytes,
|
|
1873
|
+
toRequest: (payload, signature) => ({
|
|
1874
|
+
...payload,
|
|
1875
|
+
signature,
|
|
1876
|
+
}),
|
|
1877
|
+
});
|
|
1878
|
+
}function prepareTokenManageListTx(unsigned) {
|
|
1879
|
+
validateChainAndNonce(unsigned);
|
|
1880
|
+
assertAddress('address', unsigned.address);
|
|
1881
|
+
assertAddress('token', unsigned.token);
|
|
1882
|
+
const rlpBytes = encodeRlpPayload(rlpValue.list([
|
|
1883
|
+
rlpValue.uint(unsigned.chain_id),
|
|
1884
|
+
rlpValue.uint(unsigned.nonce),
|
|
1885
|
+
rlpValue.string(unsigned.action),
|
|
1886
|
+
rlpValue.address(unsigned.address),
|
|
1887
|
+
rlpValue.address(unsigned.token),
|
|
1888
|
+
]));
|
|
1889
|
+
return createPreparedTx({
|
|
1890
|
+
kind: 'tokenManageList',
|
|
1891
|
+
unsigned,
|
|
1892
|
+
rlpBytes,
|
|
1893
|
+
toRequest: (payload, signature) => ({
|
|
1894
|
+
...payload,
|
|
1895
|
+
signature,
|
|
1896
|
+
}),
|
|
1897
|
+
});
|
|
1898
|
+
}function prepareTokenMetadataTx(unsigned) {
|
|
1899
|
+
validateChainAndNonce(unsigned);
|
|
1900
|
+
assertAddress('token', unsigned.token);
|
|
1901
|
+
const additionalMetadataRlp = unsigned.additional_metadata.map(item => rlpValue.list([
|
|
1902
|
+
rlpValue.string(item.key),
|
|
1903
|
+
rlpValue.string(item.value),
|
|
1904
|
+
]));
|
|
1905
|
+
const rlpBytes = encodeRlpPayload(rlpValue.list([
|
|
1906
|
+
rlpValue.uint(unsigned.chain_id),
|
|
1907
|
+
rlpValue.uint(unsigned.nonce),
|
|
1908
|
+
rlpValue.string(unsigned.name),
|
|
1909
|
+
rlpValue.string(unsigned.uri),
|
|
1910
|
+
rlpValue.address(unsigned.token),
|
|
1911
|
+
rlpValue.list(additionalMetadataRlp),
|
|
1912
|
+
]));
|
|
1913
|
+
return createPreparedTx({
|
|
1914
|
+
kind: 'tokenMetadata',
|
|
1915
|
+
unsigned,
|
|
1916
|
+
rlpBytes,
|
|
1917
|
+
toRequest: (payload, signature) => ({
|
|
1918
|
+
...payload,
|
|
1919
|
+
signature,
|
|
1920
|
+
}),
|
|
1921
|
+
});
|
|
1922
|
+
}function prepareTokenMintTx(unsigned) {
|
|
1923
|
+
validateChainAndNonce(unsigned);
|
|
1924
|
+
validateRecipientValueToken(unsigned);
|
|
1925
|
+
const rlpBytes = encodeRlpPayload(rlpValue.list([
|
|
1926
|
+
rlpValue.uint(unsigned.chain_id),
|
|
1927
|
+
rlpValue.uint(unsigned.nonce),
|
|
1928
|
+
rlpValue.address(unsigned.recipient),
|
|
1929
|
+
rlpValue.uint(unsigned.value),
|
|
1930
|
+
rlpValue.address(unsigned.token),
|
|
1931
|
+
]));
|
|
1932
|
+
return createPreparedTx({
|
|
1933
|
+
kind: 'tokenMint',
|
|
1934
|
+
unsigned,
|
|
1935
|
+
rlpBytes,
|
|
1936
|
+
toRequest: (payload, signature) => ({
|
|
1937
|
+
...payload,
|
|
1938
|
+
signature,
|
|
1939
|
+
}),
|
|
1940
|
+
});
|
|
1941
|
+
}function prepareTokenPauseTx(unsigned) {
|
|
1942
|
+
validateChainAndNonce(unsigned);
|
|
1943
|
+
assertAddress('token', unsigned.token);
|
|
1944
|
+
const rlpBytes = encodeRlpPayload(rlpValue.list([
|
|
1945
|
+
rlpValue.uint(unsigned.chain_id),
|
|
1946
|
+
rlpValue.uint(unsigned.nonce),
|
|
1947
|
+
rlpValue.string(unsigned.action),
|
|
1948
|
+
rlpValue.address(unsigned.token),
|
|
1949
|
+
]));
|
|
1950
|
+
return createPreparedTx({
|
|
1951
|
+
kind: 'tokenPause',
|
|
1952
|
+
unsigned,
|
|
1953
|
+
rlpBytes,
|
|
1954
|
+
toRequest: (payload, signature) => ({
|
|
1955
|
+
...payload,
|
|
1956
|
+
signature,
|
|
1957
|
+
}),
|
|
1958
|
+
});
|
|
1959
|
+
}const DIGEST_HEX_RE = /^0x[0-9a-fA-F]{64}$/;
|
|
1960
|
+
function createPrivateKeySigner(privateKey) {
|
|
1961
|
+
const privateKeyBytes = hexToBytes$1(privateKey);
|
|
1962
|
+
return {
|
|
1963
|
+
signDigest: async (digest) => {
|
|
1964
|
+
if (!DIGEST_HEX_RE.test(digest)) {
|
|
1965
|
+
throw new Error(`[1Money SDK]: Invalid digest: ${digest}`);
|
|
1966
|
+
}
|
|
1967
|
+
const signature = await signAsync(hexToBytes$1(digest), privateKeyBytes, { lowS: true });
|
|
1968
|
+
const compact = signature.toCompactRawBytes();
|
|
1969
|
+
const rBytes = compact.subarray(0, 32);
|
|
1970
|
+
const sBytes = compact.subarray(32, 64);
|
|
1971
|
+
return {
|
|
1972
|
+
r: bytesToHex$1(rBytes),
|
|
1973
|
+
s: bytesToHex$1(sBytes),
|
|
1974
|
+
v: signature.recovery,
|
|
1975
|
+
};
|
|
1976
|
+
},
|
|
1977
|
+
};
|
|
1978
|
+
}const TransactionBuilder = {
|
|
1979
|
+
payment: preparePaymentTx,
|
|
1980
|
+
tokenManageList: prepareTokenManageListTx,
|
|
1981
|
+
tokenBurn: prepareTokenBurnTx,
|
|
1982
|
+
tokenAuthority: prepareTokenAuthorityTx,
|
|
1983
|
+
tokenIssue: prepareTokenIssueTx,
|
|
1984
|
+
tokenMint: prepareTokenMintTx,
|
|
1985
|
+
tokenPause: prepareTokenPauseTx,
|
|
1986
|
+
tokenMetadata: prepareTokenMetadataTx,
|
|
1987
|
+
tokenBridgeAndMint: prepareTokenBridgeAndMintTx,
|
|
1988
|
+
tokenBurnAndBridge: prepareTokenBurnAndBridgeTx,
|
|
1989
|
+
tokenClawback: prepareTokenClawbackTx,
|
|
1990
|
+
};var index = {
|
|
1532
1991
|
api,
|
|
1533
1992
|
client: client$1,
|
|
1534
|
-
};export{_typeof,api,calcTxHash,client$1 as client,index as default,deriveTokenAddress,encodePayload,safePromiseAll,safePromiseLine,signMessage,toHex};
|
|
1993
|
+
};export{TransactionBuilder,_typeof,api,calcSignedTxHash,calcTxHash,client$1 as client,createPreparedTx,createPrivateKeySigner,index as default,deriveTokenAddress,encodePayload,encodeRlpPayload,preparePaymentTx,prepareTokenAuthorityTx,prepareTokenBridgeAndMintTx,prepareTokenBurnAndBridgeTx,prepareTokenBurnTx,prepareTokenClawbackTx,prepareTokenIssueTx,prepareTokenManageListTx,prepareTokenMetadataTx,prepareTokenMintTx,prepareTokenPauseTx,rlpValue,safePromiseAll,safePromiseLine,signMessage,toHex};
|