carbon-js-sdk 0.4.19 → 0.4.21

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 (63) hide show
  1. package/lib/clients/TokenClient.js +1 -2
  2. package/lib/codec/ethermint/crypto/v1/ethsecp256k1/export.d.ts +1 -0
  3. package/lib/codec/ethermint/crypto/v1/ethsecp256k1/export.js +6 -0
  4. package/lib/codec/ethermint/evm/v1/export.d.ts +5 -0
  5. package/lib/codec/ethermint/evm/v1/export.js +53 -0
  6. package/lib/codec/ethermint/feemarket/v1/export.d.ts +4 -0
  7. package/lib/codec/ethermint/feemarket/v1/export.js +18 -0
  8. package/lib/codec/ethermint/types/v1/export.d.ts +4 -0
  9. package/lib/codec/ethermint/types/v1/export.js +11 -0
  10. package/lib/codec/ethermint-models.d.ts +4 -0
  11. package/lib/codec/ethermint-models.js +26 -0
  12. package/lib/codec/market/fee.d.ts +123 -0
  13. package/lib/codec/market/fee.js +334 -0
  14. package/lib/codec/perpsliquidity/event.d.ts +102 -0
  15. package/lib/codec/perpsliquidity/event.js +581 -0
  16. package/lib/codec/perpsliquidity/genesis.d.ts +35 -0
  17. package/lib/codec/perpsliquidity/genesis.js +162 -0
  18. package/lib/codec/perpsliquidity/params.d.ts +24 -0
  19. package/lib/codec/perpsliquidity/params.js +103 -0
  20. package/lib/codec/perpsliquidity/pool.d.ts +137 -0
  21. package/lib/codec/perpsliquidity/pool.js +747 -0
  22. package/lib/codec/perpsliquidity/query.d.ts +167 -0
  23. package/lib/codec/perpsliquidity/query.js +719 -0
  24. package/lib/codec/perpsliquidity/tx.d.ts +199 -0
  25. package/lib/codec/perpsliquidity/tx.js +940 -0
  26. package/lib/constant/eip712.d.ts +46 -0
  27. package/lib/constant/eip712.js +106 -0
  28. package/lib/constant/network.js +2 -2
  29. package/lib/modules/evm.d.ts +46 -0
  30. package/lib/modules/evm.js +127 -0
  31. package/lib/modules/evmmerge.d.ts +12 -0
  32. package/lib/modules/evmmerge.js +35 -0
  33. package/lib/modules/feemarket.d.ts +20 -0
  34. package/lib/modules/feemarket.js +45 -0
  35. package/lib/modules/vault.d.ts +44 -0
  36. package/lib/modules/vault.js +95 -0
  37. package/lib/provider/amino/types/evm.d.ts +4 -0
  38. package/lib/provider/amino/types/evm.js +40 -0
  39. package/lib/provider/amino/types/evmmerge.d.ts +4 -0
  40. package/lib/provider/amino/types/evmmerge.js +34 -0
  41. package/lib/provider/amino/types/feemarket.d.ts +4 -0
  42. package/lib/provider/amino/types/feemarket.js +34 -0
  43. package/lib/provider/metamask/legacy-accounts.d.ts +6 -0
  44. package/lib/provider/metamask/legacy-accounts.js +44 -0
  45. package/lib/util/eip712.d.ts +10 -0
  46. package/lib/util/eip712.js +195 -0
  47. package/lib/util/ethermint.d.ts +8 -0
  48. package/lib/util/ethermint.js +68 -0
  49. package/lib/util/legacyEIP712.d.ts +20 -0
  50. package/lib/util/legacyEIP712.js +98 -0
  51. package/package.json +1 -1
  52. package/lib/clients/EvmIbcClient.d.ts +0 -86
  53. package/lib/clients/EvmIbcClient.js +0 -90
  54. package/lib/codec/broker/incoming_pool_swap.d.ts +0 -23
  55. package/lib/codec/broker/incoming_pool_swap.js +0 -136
  56. package/lib/util/ethermint/evm-ibc.d.ts +0 -97
  57. package/lib/util/ethermint/evm-ibc.js +0 -134
  58. package/lib/util/ethermint/index.d.ts +0 -3
  59. package/lib/util/ethermint/index.js +0 -12
  60. package/lib/util/ethermint/keys.d.ts +0 -39
  61. package/lib/util/ethermint/keys.js +0 -138
  62. package/lib/util/ethermint/web3.d.ts +0 -36
  63. package/lib/util/ethermint/web3.js +0 -124
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ const CarbonTx = __importStar(require("../../../util/tx"));
23
+ const utils_1 = require("../utils");
24
+ const TxTypes = {
25
+ MergeAccount: "evmmerge/MsgMergeAccount",
26
+ };
27
+ const MsgMergeAccount = {
28
+ aminoType: TxTypes.MergeAccount,
29
+ valueMap: {},
30
+ };
31
+ const EvmMergeAmino = {
32
+ [CarbonTx.Types.MsgMergeAccount]: utils_1.generateAminoType(MsgMergeAccount),
33
+ };
34
+ exports.default = EvmMergeAmino;
@@ -0,0 +1,4 @@
1
+ import { TypeUtils } from "../../../util";
2
+ import { AminoConverter } from "@cosmjs/stargate";
3
+ declare const FeeMarketAmino: TypeUtils.SimpleMap<AminoConverter>;
4
+ export default FeeMarketAmino;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ const CarbonTx = __importStar(require("../../../util/tx"));
23
+ const utils_1 = require("../utils");
24
+ const TxTypes = {
25
+ UpdateParams: "ethermint/feemarket/MsgUpdateParams",
26
+ };
27
+ const MsgUpdateParams = {
28
+ aminoType: TxTypes.UpdateParams,
29
+ valueMap: {},
30
+ };
31
+ const FeeMarketAmino = {
32
+ [CarbonTx.Types.MsgUpdateParams]: utils_1.generateAminoType(MsgUpdateParams),
33
+ };
34
+ exports.default = FeeMarketAmino;
@@ -0,0 +1,6 @@
1
+ export declare const LEGACY_ACCOUNTS_TESTNET: {
2
+ Ethereum: string[];
3
+ };
4
+ export declare const LEGACY_ACCOUNTS_MAINNET: {
5
+ Ethereum: string[];
6
+ };
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LEGACY_ACCOUNTS_MAINNET = exports.LEGACY_ACCOUNTS_TESTNET = void 0;
4
+ // As of 16 May 2023
5
+ exports.LEGACY_ACCOUNTS_TESTNET = {
6
+ "Ethereum": [
7
+ "0x32c1069dcf38a7294ee9633fcb897c18cabd1668",
8
+ "0x6a8247bc77084acf32901f6a29369bc702d25769",
9
+ "0xe3f544b81a8e60087b25b1e6f0e71d579686c798",
10
+ "0xc6a53d96f92be41b9a7ccd9daa5d52cb7a8fdb92",
11
+ "0x8440a2e6e763ee99b29dcf663a72896817d6a6fd",
12
+ "0x04d40c9e02e0f793c25d2e4c993b853f6a1c7b6c",
13
+ "0x1e9eb8d2320a52dcd91a4016b1471947844ed903",
14
+ "0x7d831dd3a6f1553b9b995f499f4d31d4935b34f8",
15
+ "0xb88cb290efff1017a27db762fba8f15e754fdd67",
16
+ "0xc120adde16e45c1a36e65fb9bdb5bd8b1f620c19",
17
+ "0x0897fbc3eb27992cadbffa506e1403d180c94735",
18
+ "0x49367893FF5e5481312159D1EC7dfFd1bCD082B7",
19
+ "0x6fC1b1665BF2EC94d3d936EAb45ACa6b58eE2797",
20
+ "0x3619d35d2ca763d63387138Fff3a13A5fFE7c8Ce",
21
+ "0xEEe3c236312a81f3CC3CB78763968e24355b513F",
22
+ "0x184268C2eA4332234d6Ee5b0bA1C212E63fa55Cc",
23
+ "0x05a2F90551Dcba19C42c6E85FB00843C0859a427",
24
+ "0xad045ec15DbBe6FebE8e7D1EC85Eb7D496c4ebe1",
25
+ "0xcFa83a56f1479BB9CD5a25f36a0C3E44EE08D7DB",
26
+ "0x8e2C2E908195F87Bc6C14fAEC31F7726d094b3c6"
27
+ ]
28
+ };
29
+ // To be updated
30
+ exports.LEGACY_ACCOUNTS_MAINNET = {
31
+ "Ethereum": [
32
+ "0x32c1069dcf38a7294ee9633fcb897c18cabd1668",
33
+ "0x6a8247bc77084acf32901f6a29369bc702d25769",
34
+ "0xe3f544b81a8e60087b25b1e6f0e71d579686c798",
35
+ "0xc6a53d96f92be41b9a7ccd9daa5d52cb7a8fdb92",
36
+ "0x8440a2e6e763ee99b29dcf663a72896817d6a6fd",
37
+ "0x04d40c9e02e0f793c25d2e4c993b853f6a1c7b6c",
38
+ "0x1e9eb8d2320a52dcd91a4016b1471947844ed903",
39
+ "0x7d831dd3a6f1553b9b995f499f4d31d4935b34f8",
40
+ "0xb88cb290efff1017a27db762fba8f15e754fdd67",
41
+ "0xc120adde16e45c1a36e65fb9bdb5bd8b1f620c19",
42
+ "0x0897fbc3eb27992cadbffa506e1403d180c94735"
43
+ ]
44
+ };
@@ -0,0 +1,10 @@
1
+ import { TypedDataDomain, TypedDataField } from "@ethersproject/abstract-signer";
2
+ import { TypeUtils } from ".";
3
+ import { CarbonTx } from "../util";
4
+ export interface EIP712Tx {
5
+ readonly types: TypeUtils.SimpleMap<TypedDataField[]>;
6
+ readonly primaryType: string;
7
+ readonly domain: TypedDataDomain;
8
+ readonly message: any;
9
+ }
10
+ export declare function constructEIP712Tx(doc: CarbonTx.StdSignDoc): EIP712Tx;
@@ -0,0 +1,195 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.constructEIP712Tx = void 0;
7
+ const eip712_1 = require("../constant/eip712");
8
+ const ethermint_1 = require("../util/ethermint");
9
+ const codec_1 = require("../codec");
10
+ const AminoTypesMap_1 = __importDefault(require("../provider/amino/AminoTypesMap"));
11
+ const lodash_1 = require("lodash");
12
+ function getTypes(msgs) {
13
+ let types = Object.assign({}, eip712_1.DEFAULT_EIP712_TYPES);
14
+ const includedTypes = [];
15
+ let valueIndex = 0;
16
+ msgs.forEach((msg, index) => {
17
+ const typeUrl = AminoTypesMap_1.default.fromAmino(msg).typeUrl;
18
+ const msgType = msg.type.split('/')[1];
19
+ const msgTypeIndex = getLatestMsgTypeIndex(`Type${msgType}`, types);
20
+ //cosmos-sdk/MsgSend => TypeMsgSend1
21
+ const typeKey = `Type${msgType}${msgTypeIndex}`;
22
+ if (!includedTypes.includes(msg.type)) {
23
+ types['Tx'] = [...types['Tx'], { name: `msg${index}`, type: typeKey }];
24
+ types[typeKey] = [{ name: 'value', type: `TypeValue${valueIndex}` }, { name: 'type', type: 'string' }];
25
+ //cosmos-sdk/MsgSend => Msg_Send
26
+ types = Object.assign(Object.assign({}, types), sortByNameDescending(getMsgValueType(typeUrl, msg.value, `TypeValue${valueIndex}`, valueIndex, types)));
27
+ includedTypes.push(msg.type);
28
+ valueIndex++;
29
+ return;
30
+ }
31
+ const typeFound = matchingType(msg, types);
32
+ if (typeFound) {
33
+ types['Tx'] = [...types['Tx'], { name: `msg${index}`, type: typeFound }];
34
+ return;
35
+ }
36
+ //same type, but different fields populated, so new type defnition is required
37
+ types['Tx'] = [...types['Tx'], { name: `msg${index}`, type: typeKey }];
38
+ types[typeKey] = [{ name: 'value', type: `TypeValue${valueIndex}` }, { name: 'type', type: 'string' }];
39
+ types = Object.assign(Object.assign({}, types), sortByNameDescending(getMsgValueType(typeUrl, msg.value, `TypeValue${valueIndex}`, valueIndex, types)));
40
+ valueIndex++;
41
+ });
42
+ return types;
43
+ }
44
+ function getLatestMsgTypeIndex(typeName, types) {
45
+ let index = 0;
46
+ Object.entries(types).forEach(([key, _]) => {
47
+ if (key.includes(typeName)) {
48
+ index++;
49
+ }
50
+ });
51
+ return index;
52
+ }
53
+ function sortByNameDescending(types) {
54
+ Object.entries(types).forEach(([key, _]) => {
55
+ types[key].sort((a, b) => b.name.localeCompare(a.name));
56
+ });
57
+ return types;
58
+ }
59
+ // Checks if there is a need to create new type for the same message type because of different populated fields
60
+ function matchingType(msg, eipTypes) {
61
+ const msgType = msg.type.split('/')[1];
62
+ let match = false;
63
+ for (const key in eipTypes) {
64
+ if (key.includes(msgType)) {
65
+ match = compareValues(msg, key, eipTypes);
66
+ }
67
+ if (match) {
68
+ return key;
69
+ }
70
+ }
71
+ return '';
72
+ }
73
+ function compareValues(msg, key, eipTypes) {
74
+ let match = true;
75
+ for (let { name, type } of eipTypes[key]) {
76
+ if (Object.keys(msg).length > eipTypes[key].length) {
77
+ return false;
78
+ }
79
+ let value = msg[name];
80
+ if (!isNonZeroField(value)) {
81
+ return false;
82
+ }
83
+ const typeIsArray = type.includes('[]');
84
+ if (typeIsArray) {
85
+ type = type.split('[]')[0];
86
+ //Assumption: Take first value in array to determine which fields are populated
87
+ value = value[0];
88
+ }
89
+ if (eipTypes[type]) {
90
+ match = compareValues(value, type, eipTypes);
91
+ }
92
+ }
93
+ return match;
94
+ }
95
+ function getMsgValueType(msgTypeUrl, msgValue, msgTypeName, msgTypeIndex, types, objectName, nestedType = false, msgTypeDefinitions = {}) {
96
+ const packageName = msgTypeUrl.split(".").slice(0, -1).join(".");
97
+ const msgFieldType = msgTypeUrl.split(".").pop();
98
+ const typeName = getTypeName(msgTypeName, msgTypeIndex, objectName, nestedType, false);
99
+ const fieldsDefinition = codec_1.EIP712Types[packageName][msgFieldType];
100
+ if (isNonZeroField(msgValue)) {
101
+ if (!msgTypeDefinitions[typeName]) {
102
+ msgTypeDefinitions[typeName] = [];
103
+ }
104
+ fieldsDefinition.forEach(({ packageName, name, type }) => {
105
+ if (Array.isArray(msgValue) && msgValue.length === 0) {
106
+ msgTypeDefinitions[typeName] = [...msgTypeDefinitions[typeName], { name, type: 'string[]' }];
107
+ return;
108
+ }
109
+ //Assumption: Take first value in array to determine which fields are populated
110
+ const fieldValue = Array.isArray(msgValue) && msgValue.length > 0 ? msgValue[0][name] : msgValue[name];
111
+ if (isNonZeroField(fieldValue)) {
112
+ //For nested structs
113
+ if (packageName) {
114
+ const isArray = type.includes('[]') ? true : false;
115
+ // TypeValue0 --> Value
116
+ const objectName = typeName.split('Type')[1].split(`${msgTypeIndex}`)[0];
117
+ const nestedTypeName = `Type${objectName ? objectName : ''}${name.split('_').map((subName) => lodash_1.capitalize(subName)).join('')}`;
118
+ const nestedMsgTypeIndex = getLatestMsgTypeIndex(nestedTypeName, types);
119
+ const nestedType = getTypeName(name, nestedMsgTypeIndex, objectName, true, isArray);
120
+ msgTypeDefinitions[typeName] = [...msgTypeDefinitions[typeName], { name, type: nestedType }];
121
+ //Special logic if nested struct is google protobuf's Any type
122
+ if (isGoogleProtobufAnyPackage(packageName, type)) {
123
+ const nestedAnyTypeName = isArray ? nestedType.split('[]')[0].split(`${msgTypeIndex}`)[0] : nestedType.split(`${msgTypeIndex}`)[0];
124
+ const nestedMsgTypeIndex = getLatestMsgTypeIndex(`${nestedAnyTypeName}Value`, types);
125
+ const nestedAnyValueType = `${nestedAnyTypeName}Value${nestedMsgTypeIndex}`;
126
+ msgTypeDefinitions[`${nestedAnyTypeName}${msgTypeIndex}`] = [{ name: "type", type: "string" }, { name: "value", type: nestedAnyValueType }];
127
+ const anyObjectTypeNameSplit = nestedAnyTypeName.split('Type')[1].split(`${msgTypeIndex}`)[0];
128
+ const messageTypeUrl = AminoTypesMap_1.default.fromAmino(fieldValue).typeUrl;
129
+ getMsgValueType(messageTypeUrl, fieldValue.value, "value", nestedMsgTypeIndex, types, anyObjectTypeNameSplit, true, msgTypeDefinitions);
130
+ }
131
+ else {
132
+ const typeStructName = type.includes('[]') ? type.split('[]')[0].split(`${nestedMsgTypeIndex}`)[0] : type.split(`${nestedMsgTypeIndex}`)[0];
133
+ const messageTypeUrl = `${packageName}.${typeStructName}`;
134
+ getMsgValueType(messageTypeUrl, fieldValue, name, nestedMsgTypeIndex, types, objectName, true, msgTypeDefinitions);
135
+ }
136
+ }
137
+ else {
138
+ msgTypeDefinitions[typeName] = [...msgTypeDefinitions[typeName], { name, type: getGjsonPrimitiveType(fieldValue) }];
139
+ }
140
+ }
141
+ });
142
+ }
143
+ return msgTypeDefinitions;
144
+ }
145
+ function getGjsonPrimitiveType(value) {
146
+ if (typeof value === 'number') {
147
+ return 'int64';
148
+ }
149
+ if (typeof value === 'boolean') {
150
+ return 'bool';
151
+ }
152
+ return 'string';
153
+ }
154
+ function getTypeName(name, index, objectName, nestedType = false, isArray = false) {
155
+ if (nestedType) {
156
+ return `Type${objectName ? objectName : ''}${name.split('_').map(subName => lodash_1.capitalize(subName)).join('')}${index}${isArray ? '[]' : ''}`;
157
+ }
158
+ return name;
159
+ }
160
+ function isGoogleProtobufAnyPackage(packageName, type) {
161
+ return packageName === '/google.protobuf' && type == 'Any';
162
+ }
163
+ function isNonZeroField(fieldValue) {
164
+ // zero fields are considered falsey,except if it is string "0"
165
+ if (fieldValue == "0" && typeof fieldValue !== "string") {
166
+ return false;
167
+ }
168
+ // empty arrays are considered truthy
169
+ if (Array.isArray(fieldValue)) {
170
+ return fieldValue.length !== 0;
171
+ }
172
+ // empty objects are considered truthy
173
+ if (fieldValue && typeof fieldValue === 'object' && Object.keys(fieldValue).length === 0) {
174
+ return false;
175
+ }
176
+ return fieldValue;
177
+ }
178
+ function constructEIP712Tx(doc) {
179
+ const { account_number, chain_id, fee, memo, sequence } = doc;
180
+ const eip712Tx = {
181
+ types: getTypes(doc.msgs),
182
+ primaryType: "Tx",
183
+ domain: Object.assign(Object.assign({}, eip712_1.DEFAULT_CARBON_DOMAIN_FIELDS), { chainId: ethermint_1.parseChainId(doc.chain_id) }),
184
+ message: Object.assign({ account_number, chain_id, fee, memo, sequence }, convertMsgs(doc.msgs))
185
+ };
186
+ return eip712Tx;
187
+ }
188
+ exports.constructEIP712Tx = constructEIP712Tx;
189
+ function convertMsgs(msgs) {
190
+ const convertedMsgs = {};
191
+ msgs.forEach((msg, index) => {
192
+ convertedMsgs[`msg${index}`] = msg;
193
+ });
194
+ return convertedMsgs;
195
+ }
@@ -0,0 +1,8 @@
1
+ import CarbonSDK from "../CarbonSDK";
2
+ import { Any } from "../codec";
3
+ import { ethers } from "ethers";
4
+ export declare const ETH_SECP256K1_TYPE = "/ethermint.crypto.v1.ethsecp256k1.PubKey";
5
+ export declare const PUBLIC_KEY_SIGNING_TEXT = "Sign your public key to merge your Carbon account: ";
6
+ export declare function encodeAnyEthSecp256k1PubKey(pubkey: Uint8Array): Any;
7
+ export declare function parseChainId(evmChainId: string): string;
8
+ export declare function populateEvmTransactionDetails(api: CarbonSDK, req: ethers.providers.TransactionRequest): Promise<ethers.providers.TransactionRequest>;
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.populateEvmTransactionDetails = exports.parseChainId = exports.encodeAnyEthSecp256k1PubKey = exports.PUBLIC_KEY_SIGNING_TEXT = exports.ETH_SECP256K1_TYPE = void 0;
13
+ const codec_1 = require("../codec");
14
+ const keys_1 = require("../codec/ethermint/crypto/v1/ethsecp256k1/keys");
15
+ exports.ETH_SECP256K1_TYPE = '/ethermint.crypto.v1.ethsecp256k1.PubKey';
16
+ exports.PUBLIC_KEY_SIGNING_TEXT = 'Sign your public key to merge your Carbon account: ';
17
+ function encodeAnyEthSecp256k1PubKey(pubkey) {
18
+ const ethPubKey = keys_1.PubKey.fromPartial({
19
+ key: pubkey,
20
+ });
21
+ return codec_1.Any.fromPartial({
22
+ typeUrl: exports.ETH_SECP256K1_TYPE,
23
+ value: keys_1.PubKey.encode(ethPubKey).finish(),
24
+ });
25
+ }
26
+ exports.encodeAnyEthSecp256k1PubKey = encodeAnyEthSecp256k1PubKey;
27
+ function parseChainId(evmChainId) {
28
+ const chainId = evmChainId.trim();
29
+ if (chainId.length > 48) {
30
+ throw new Error(`chain-id '${chainId}' cannot exceed 48 chars`);
31
+ }
32
+ if (!chainId.match(/^[a-z]+_\d+-\d+$/)) {
33
+ throw new Error(`chain-id '${chainId}' does not conform to the required format`);
34
+ }
35
+ return chainId.split("_")[1].split("-")[0];
36
+ }
37
+ exports.parseChainId = parseChainId;
38
+ function populateEvmTransactionDetails(api, req) {
39
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
40
+ return __awaiter(this, void 0, void 0, function* () {
41
+ const provider = api.evmJsonRpc;
42
+ const evmHexAddress = (_b = (_a = api.wallet) === null || _a === void 0 ? void 0 : _a.evmHexAddress) !== null && _b !== void 0 ? _b : '';
43
+ let request = {
44
+ to: (_c = req.to) !== null && _c !== void 0 ? _c : '',
45
+ from: (_d = req.from) !== null && _d !== void 0 ? _d : (_e = api.wallet) === null || _e === void 0 ? void 0 : _e.evmHexAddress,
46
+ nonce: (_f = req.nonce) !== null && _f !== void 0 ? _f : (yield provider.getTransactionCount(evmHexAddress)),
47
+ data: req.data,
48
+ value: `0x${Number(req.value).toString(16)}`,
49
+ chainId: (_g = req.chainId) !== null && _g !== void 0 ? _g : Number(parseChainId(yield ((_h = api.wallet) === null || _h === void 0 ? void 0 : _h.getEvmChainId()))),
50
+ // type = 0, 1 or 2 where 0 = legacyTx, 1 = AccessListTx, 2 = DynamicTx. Defaults to DynamicTx
51
+ type: (_j = req.type) !== null && _j !== void 0 ? _j : 2,
52
+ accessList: req.accessList,
53
+ };
54
+ const gasLimit = (yield provider.estimateGas(request)).toHexString();
55
+ const gasFee = yield provider.getFeeData();
56
+ if (!req.gasPrice) {
57
+ // Dynamic Tx
58
+ return Object.assign(Object.assign({}, request), { maxPriorityFeePerGas: (_k = req.maxPriorityFeePerGas) !== null && _k !== void 0 ? _k : (_l = gasFee.maxPriorityFeePerGas) === null || _l === void 0 ? void 0 : _l.toHexString(), maxFeePerGas: (_m = req.maxFeePerGas) !== null && _m !== void 0 ? _m : (_o = gasFee.maxFeePerGas) === null || _o === void 0 ? void 0 : _o.toHexString(), gasLimit, type: 2 });
59
+ }
60
+ if (req.accessList) {
61
+ // AccessList Tx
62
+ return Object.assign(Object.assign({}, request), { gasLimit, type: 1 });
63
+ }
64
+ // LegacyTx
65
+ return Object.assign(Object.assign({}, request), { gasLimit, type: 0 });
66
+ });
67
+ }
68
+ exports.populateEvmTransactionDetails = populateEvmTransactionDetails;
@@ -0,0 +1,20 @@
1
+ import { StdSignDoc } from "@cosmjs/amino/build";
2
+ import { TypedDataDomain, TypedDataField } from "@ethersproject/abstract-signer";
3
+ import { TypeUtils } from ".";
4
+ import { Coin } from "@cosmjs/proto-signing";
5
+ export interface LegacyEIP712Tx {
6
+ readonly types: TypeUtils.SimpleMap<TypedDataField[]>;
7
+ readonly primaryType: string;
8
+ readonly domain: TypedDataDomain;
9
+ readonly message: LegacyEIP712StdSignDoc;
10
+ }
11
+ export declare type EIP712Fee = {
12
+ readonly amount: readonly Coin[];
13
+ readonly gas: string;
14
+ readonly feePayer: string;
15
+ };
16
+ declare type LegacyEIP712StdSignDoc = StdSignDoc & {
17
+ fee: EIP712Fee;
18
+ };
19
+ export declare function legacyConstructEIP712Tx(doc: LegacyEIP712StdSignDoc): LegacyEIP712Tx;
20
+ export {};
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.legacyConstructEIP712Tx = void 0;
7
+ const eip712_1 = require("../constant/eip712");
8
+ const ethermint_1 = require("../util/ethermint");
9
+ const codec_1 = require("../codec");
10
+ const AminoTypesMap_1 = __importDefault(require("../provider/amino/AminoTypesMap"));
11
+ const lodash_1 = require("lodash");
12
+ function getTypes(msgTypeUrl, aminoMsgValue) {
13
+ return Object.assign(Object.assign({}, eip712_1.LEGACY_DEFAULT_EIP712_TYPES), getMsgValueType(msgTypeUrl, aminoMsgValue, "MsgValue"));
14
+ }
15
+ function getMsgValueType(msgTypeUrl, msgValue, msgTypeName, objectName, nestedType = false, msgTypeDefinitions = {}) {
16
+ const packageName = msgTypeUrl.split(".").slice(0, -1).join(".");
17
+ const msgFieldType = msgTypeUrl.split(".").pop();
18
+ const typeName = getTypeName(msgTypeName, objectName, nestedType, false);
19
+ const fieldsDefinition = codec_1.EIP712Types[packageName][msgFieldType];
20
+ if (isNonZeroField(msgValue)) {
21
+ if (!msgTypeDefinitions[typeName]) {
22
+ msgTypeDefinitions[typeName] = [];
23
+ }
24
+ fieldsDefinition.forEach(({ packageName, name, type }) => {
25
+ let fieldValue;
26
+ if (Array.isArray(msgValue)) {
27
+ //Assumption: Take first value in array to determine which fields are populated
28
+ fieldValue = msgValue.length > 0 ? msgValue[0][name] : [];
29
+ }
30
+ else {
31
+ fieldValue = msgValue[name];
32
+ }
33
+ if (isNonZeroField(fieldValue)) {
34
+ //For nested structs
35
+ if (packageName) {
36
+ const isArray = type.includes('[]') ? true : false;
37
+ const objectName = typeName === 'MsgValue' ? 'MsgValue' : typeName.split('Type')[1];
38
+ const nestedType = getTypeName(name, objectName, true, isArray);
39
+ msgTypeDefinitions[typeName] = [...msgTypeDefinitions[typeName], { name, type: nestedType }];
40
+ //Special logic if nested struct is google protobuf's Any type
41
+ if (isGoogleProtobufAnyPackage(packageName, type)) {
42
+ const nestedAnyTypeName = isArray ? nestedType.split('[]')[0] : nestedType;
43
+ const nestedAnyValueType = `${nestedAnyTypeName}Value`;
44
+ msgTypeDefinitions[nestedAnyTypeName] = [{ name: "type", type: "string" }, { name: "value", type: nestedAnyValueType }];
45
+ const anyObjectTypeNameSplit = nestedAnyTypeName.split('Type')[1];
46
+ const messageTypeUrl = AminoTypesMap_1.default.fromAmino(fieldValue).typeUrl;
47
+ getMsgValueType(messageTypeUrl, fieldValue.value, "value", anyObjectTypeNameSplit, true, msgTypeDefinitions);
48
+ }
49
+ else {
50
+ const typeStructName = type.includes('[]') ? type.split('[]')[0] : type;
51
+ const messageTypeUrl = `${packageName}.${typeStructName}`;
52
+ getMsgValueType(messageTypeUrl, fieldValue, name, objectName, true, msgTypeDefinitions);
53
+ }
54
+ }
55
+ else {
56
+ msgTypeDefinitions[typeName] = [...msgTypeDefinitions[typeName], { name, type }];
57
+ }
58
+ }
59
+ });
60
+ }
61
+ return msgTypeDefinitions;
62
+ }
63
+ function getTypeName(name, objectName, nestedType = false, isArray = false) {
64
+ if (nestedType) {
65
+ return `Type${objectName === 'MsgValue' ? '' : objectName}${name.split('_').map(subName => lodash_1.capitalize(subName)).join('')}${isArray ? '[]' : ''}`;
66
+ }
67
+ return name;
68
+ }
69
+ function isGoogleProtobufAnyPackage(packageName, type) {
70
+ return packageName === '/google.protobuf' && type == 'Any';
71
+ }
72
+ function isNonZeroField(fieldValue) {
73
+ // zero fields are considered falsey
74
+ if (fieldValue == "0") {
75
+ return false;
76
+ }
77
+ // empty arrays are considered truthy
78
+ if (Array.isArray(fieldValue)) {
79
+ return fieldValue.length !== 0;
80
+ }
81
+ // empty objects are considered truthy
82
+ if (fieldValue && typeof fieldValue === 'object' && Object.keys(fieldValue).length === 0) {
83
+ return false;
84
+ }
85
+ return fieldValue;
86
+ }
87
+ function legacyConstructEIP712Tx(doc) {
88
+ // Legacy EIP-712 can only accept batch msgs of the same type
89
+ const msg = doc.msgs[0];
90
+ const eip712Tx = {
91
+ types: getTypes(AminoTypesMap_1.default.fromAmino(msg).typeUrl, msg.value),
92
+ primaryType: "Tx",
93
+ domain: Object.assign(Object.assign({}, eip712_1.DEFAULT_CARBON_DOMAIN_FIELDS), { chainId: ethermint_1.parseChainId(doc.chain_id) }),
94
+ message: doc
95
+ };
96
+ return eip712Tx;
97
+ }
98
+ exports.legacyConstructEIP712Tx = legacyConstructEIP712Tx;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "carbon-js-sdk",
3
- "version": "0.4.19",
3
+ "version": "0.4.21",
4
4
  "description": "TypeScript SDK for Carbon blockchain",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -1,86 +0,0 @@
1
- import { Network } from "../constant";
2
- import { StdSignDoc } from "@cosmjs/amino";
3
- import { BlockchainUtils } from "../util";
4
- declare type SupportedBlockchains = BlockchainUtils.Blockchain.Canto | BlockchainUtils.Blockchain.Evmos;
5
- declare namespace EvmIbcClient {
6
- interface TypedDomain {
7
- name: string;
8
- version: string;
9
- chainId: number;
10
- verifyingContract: string;
11
- salt: string;
12
- }
13
- interface TypedDataV4 {
14
- types: object;
15
- primaryType: string;
16
- message: StdSignDoc;
17
- domain: TypedDomain;
18
- }
19
- const TYPED_DATA_REQUEST_METHOD = "eth_signTypedData_v4";
20
- const DEFAULT_EIP712_TYPES: {
21
- EIP712Domain: {
22
- name: string;
23
- type: string;
24
- }[];
25
- Tx: {
26
- name: string;
27
- type: string;
28
- }[];
29
- Fee: {
30
- name: string;
31
- type: string;
32
- }[];
33
- Coin: {
34
- name: string;
35
- type: string;
36
- }[];
37
- Msg: {
38
- name: string;
39
- type: string;
40
- }[];
41
- };
42
- const getEvmChainId: (blockchain: SupportedBlockchains, network?: Network) => 7700 | 9001;
43
- const getIbcTransferTypes: () => {
44
- MsgValue: {
45
- name: string;
46
- type: string;
47
- }[];
48
- TypeToken: {
49
- name: string;
50
- type: string;
51
- }[];
52
- TypeTimeoutHeight: {
53
- name: string;
54
- type: string;
55
- }[];
56
- EIP712Domain: {
57
- name: string;
58
- type: string;
59
- }[];
60
- Tx: {
61
- name: string;
62
- type: string;
63
- }[];
64
- Fee: {
65
- name: string;
66
- type: string;
67
- }[];
68
- Coin: {
69
- name: string;
70
- type: string;
71
- }[];
72
- Msg: {
73
- name: string;
74
- type: string;
75
- }[];
76
- };
77
- const getCosmosWeb3Domain: (blockchain: SupportedBlockchains) => {
78
- name: string;
79
- version: string;
80
- chainId: number;
81
- verifyingContract: string;
82
- salt: string;
83
- };
84
- const getEIP712TypedData: (tx: StdSignDoc, blockchain: SupportedBlockchains, types?: object, domain?: EvmIbcClient.TypedDomain, primaryType?: string) => TypedDataV4;
85
- }
86
- export default EvmIbcClient;