carbon-js-sdk 0.4.16 → 0.4.17

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.
@@ -644,15 +644,16 @@ class TokenClient {
644
644
  const geckoIdToUsdPriceMap = yield this.getUSDValuesFromCoinGecko(geckoIds);
645
645
  const carbonTokenPrices = yield this.getUSDValuesFromPricingModule();
646
646
  const uscStablecoin = this.getNativeStablecoin();
647
+ // add carbon token prices first
648
+ Object.entries(carbonTokenPrices).forEach(([key, value]) => {
649
+ this.usdValues[key] = value;
650
+ });
647
651
  //store price based on denoms
648
652
  for (const denom of denoms) {
649
- const carbonTokenPrice = carbonTokenPrices[denom];
650
653
  // if token price in pricing module exists for denom, return that as usd price first
651
654
  // else check coingecko
652
- if (carbonTokenPrice) {
653
- this.usdValues[denom] = carbonTokenPrice;
655
+ if (this.usdValues[denom])
654
656
  continue;
655
- }
656
657
  const coinId = (_a = this.geckoTokenNames[denom]) !== null && _a !== void 0 ? _a : denom;
657
658
  const price = util_1.NumberUtils.bnOrZero((_b = geckoIdToUsdPriceMap === null || geckoIdToUsdPriceMap === void 0 ? void 0 : geckoIdToUsdPriceMap[coinId]) === null || _b === void 0 ? void 0 : _b.usd);
658
659
  if (price.gt(0)) {
@@ -1,4 +1,4 @@
1
- export declare const DEFAULT_EIP712_TYPES: {
1
+ export declare const LEGACY_DEFAULT_EIP712_TYPES: {
2
2
  EIP712Domain: {
3
3
  name: string;
4
4
  type: string;
@@ -20,6 +20,24 @@ export declare const DEFAULT_EIP712_TYPES: {
20
20
  type: string;
21
21
  }[];
22
22
  };
23
+ export declare const DEFAULT_EIP712_TYPES: {
24
+ Coin: {
25
+ name: string;
26
+ type: string;
27
+ }[];
28
+ EIP712Domain: {
29
+ name: string;
30
+ type: string;
31
+ }[];
32
+ Fee: {
33
+ name: string;
34
+ type: string;
35
+ }[];
36
+ Tx: {
37
+ name: string;
38
+ type: string;
39
+ }[];
40
+ };
23
41
  export declare const DEFAULT_CARBON_DOMAIN_FIELDS: {
24
42
  name: string;
25
43
  version: string;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DEFAULT_CARBON_DOMAIN_FIELDS = exports.DEFAULT_EIP712_TYPES = void 0;
4
- exports.DEFAULT_EIP712_TYPES = {
3
+ exports.DEFAULT_CARBON_DOMAIN_FIELDS = exports.DEFAULT_EIP712_TYPES = exports.LEGACY_DEFAULT_EIP712_TYPES = void 0;
4
+ exports.LEGACY_DEFAULT_EIP712_TYPES = {
5
5
  EIP712Domain: [
6
6
  { name: 'name', type: 'string' },
7
7
  { name: 'version', type: 'string' },
@@ -31,10 +31,76 @@ exports.DEFAULT_EIP712_TYPES = {
31
31
  { name: 'value', type: 'MsgValue' },
32
32
  ],
33
33
  };
34
+ exports.DEFAULT_EIP712_TYPES = {
35
+ Coin: [
36
+ {
37
+ name: 'denom',
38
+ type: 'string'
39
+ },
40
+ {
41
+ name: 'amount',
42
+ type: 'string'
43
+ }
44
+ ],
45
+ EIP712Domain: [
46
+ {
47
+ name: 'name',
48
+ type: 'string'
49
+ },
50
+ {
51
+ name: 'version',
52
+ type: 'string'
53
+ },
54
+ {
55
+ name: 'chainId',
56
+ type: 'uint256'
57
+ },
58
+ {
59
+ name: 'verifyingContract',
60
+ type: 'string'
61
+ },
62
+ {
63
+ name: 'salt',
64
+ type: 'string'
65
+ }
66
+ ],
67
+ Fee: [
68
+ {
69
+ name: 'amount',
70
+ type: 'Coin[]'
71
+ },
72
+ {
73
+ name: 'gas',
74
+ type: 'string'
75
+ }
76
+ ],
77
+ Tx: [
78
+ {
79
+ name: 'account_number',
80
+ type: 'string'
81
+ },
82
+ {
83
+ name: 'chain_id',
84
+ type: 'string'
85
+ },
86
+ {
87
+ name: 'fee',
88
+ type: 'Fee'
89
+ },
90
+ {
91
+ name: 'memo',
92
+ type: 'string'
93
+ },
94
+ {
95
+ name: 'sequence',
96
+ type: 'string'
97
+ },
98
+ ]
99
+ };
34
100
  // Note that the chainId field is delibrately omitted because it is dynamic
35
101
  exports.DEFAULT_CARBON_DOMAIN_FIELDS = {
36
- name: "Carbon",
37
- version: "1.0.0",
38
- verifyingContract: "cosmos",
39
- salt: "1",
102
+ name: 'Carbon',
103
+ version: '1.0.0',
104
+ verifyingContract: 'cosmos',
105
+ salt: '1',
40
106
  };
@@ -68,7 +68,7 @@ class EvmModule extends base_1.default {
68
68
  })
69
69
  });
70
70
  return yield wallet.sendTx({
71
- typeUrl: util_1.CarbonTx.Types.MsgUpdateParams,
71
+ typeUrl: util_1.CarbonTx.Types.MsgEvmUpdateParams,
72
72
  value,
73
73
  }, opts);
74
74
  });
@@ -0,0 +1,4 @@
1
+ import { TypeUtils } from "../../../util";
2
+ import { AminoConverter } from "@cosmjs/stargate";
3
+ declare const EvmAmino: TypeUtils.SimpleMap<AminoConverter>;
4
+ export default EvmAmino;
@@ -0,0 +1,40 @@
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/MsgUpdateParams",
26
+ EthereumTx: "evm/v1/MsgEthereumTx"
27
+ };
28
+ const MsgEthereumTx = {
29
+ aminoType: TxTypes.EthereumTx,
30
+ valueMap: {},
31
+ };
32
+ const MsgUpdateParams = {
33
+ aminoType: TxTypes.UpdateParams,
34
+ valueMap: {},
35
+ };
36
+ const EvmAmino = {
37
+ [CarbonTx.Types.MsgEthereumTx]: utils_1.generateAminoType(MsgEthereumTx),
38
+ [CarbonTx.Types.MsgEvmUpdateParams]: utils_1.generateAminoType(MsgUpdateParams),
39
+ };
40
+ exports.default = EvmAmino;
@@ -0,0 +1,4 @@
1
+ import { TypeUtils } from "../../../util";
2
+ import { AminoConverter } from "@cosmjs/stargate";
3
+ declare const EvmMergeAmino: TypeUtils.SimpleMap<AminoConverter>;
4
+ export default EvmMergeAmino;
@@ -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;
@@ -1,10 +1,10 @@
1
- import { StdSignDoc } from "@cosmjs/amino/build";
2
1
  import { TypedDataDomain, TypedDataField } from "@ethersproject/abstract-signer";
3
2
  import { TypeUtils } from ".";
3
+ import { CarbonTx } from "../util";
4
4
  export interface EIP712Tx {
5
- readonly types: TypeUtils.SimpleMap<Array<TypedDataField>>;
5
+ readonly types: TypeUtils.SimpleMap<TypedDataField[]>;
6
6
  readonly primaryType: string;
7
7
  readonly domain: TypedDataDomain;
8
- readonly message: StdSignDoc;
8
+ readonly message: any;
9
9
  }
10
- export declare function constructEIP712Tx(doc: StdSignDoc): EIP712Tx;
10
+ export declare function constructEIP712Tx(doc: CarbonTx.StdSignDoc): EIP712Tx;
@@ -8,39 +8,188 @@ const eip712_1 = require("../constant/eip712");
8
8
  const ethermint_1 = require("../util/ethermint");
9
9
  const codec_1 = require("../codec");
10
10
  const AminoTypesMap_1 = __importDefault(require("../provider/amino/AminoTypesMap"));
11
- function getTypes(msgTypeUrl, aminoMsgValue) {
12
- return Object.assign(Object.assign({}, eip712_1.DEFAULT_EIP712_TYPES), getMsgValueType(msgTypeUrl, aminoMsgValue, "MsgValue"));
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;
13
43
  }
14
- function getMsgValueType(msgTypeUrl, aminoMsgValue, msgTypeName, msgTypeDefinitions = {}) {
15
- const packageName = msgTypeUrl.split(".").splice(-1).join();
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(".");
16
97
  const msgFieldType = msgTypeUrl.split(".").pop();
98
+ const typeName = getTypeName(msgTypeName, msgTypeIndex, objectName, nestedType, false);
17
99
  const fieldsDefinition = codec_1.EIP712Types[packageName][msgFieldType];
18
- const nonZeroFieldsDefinition = [];
19
- fieldsDefinition.forEach(({ packageName, name, type }) => {
20
- const fieldValue = aminoMsgValue[name];
21
- if (fieldValue && fieldValue.length) {
22
- nonZeroFieldsDefinition.push({ name, type });
23
- msgTypeDefinitions[msgTypeName] = Object.assign(Object.assign({}, msgTypeDefinitions[msgTypeName]), nonZeroFieldsDefinition);
24
- if (packageName) {
25
- const typeUrl = `${packageName}.${type}`;
26
- const nestedAminoMsgValue = isMessage(typeUrl) ? aminoMsgValue[name]["value"] : aminoMsgValue[name];
27
- getMsgValueType(typeUrl, nestedAminoMsgValue, type, msgTypeDefinitions);
28
- }
100
+ if (isNonZeroField(msgValue)) {
101
+ if (!msgTypeDefinitions[typeName]) {
102
+ msgTypeDefinitions[typeName] = [];
29
103
  }
30
- });
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
+ }
31
143
  return msgTypeDefinitions;
32
144
  }
33
- function isMessage(msgTypeUrl) {
34
- return codec_1.registry.lookupType(msgTypeUrl) !== undefined;
145
+ function getGjsonPrimitiveType(value) {
146
+ if (typeof value === 'number') {
147
+ return 'number';
148
+ }
149
+ if (typeof value === 'boolean') {
150
+ return 'boolean';
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
165
+ if (fieldValue == "0") {
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;
35
177
  }
36
178
  function constructEIP712Tx(doc) {
37
- // EIP-712 can only accept batch msgs of the same type
38
- const msg = doc.msgs[0];
39
- return {
40
- types: getTypes(AminoTypesMap_1.default.fromAmino(msg).typeUrl, msg.value),
179
+ const { account_number, chain_id, fee, memo, sequence } = doc;
180
+ const eip712Tx = {
181
+ types: getTypes(doc.msgs),
41
182
  primaryType: "Tx",
42
183
  domain: Object.assign(Object.assign({}, eip712_1.DEFAULT_CARBON_DOMAIN_FIELDS), { chainId: ethermint_1.parseChainId(doc.chain_id) }),
43
- message: doc
184
+ message: Object.assign({ account_number, chain_id, fee, memo, sequence }, convertMsgs(doc.msgs))
44
185
  };
186
+ return eip712Tx;
45
187
  }
46
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
+ }
@@ -1,4 +1,8 @@
1
+ import CarbonSDK from "../CarbonSDK";
1
2
  import { Any } from "../codec";
3
+ import { ethers } from "ethers";
2
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: ";
3
6
  export declare function encodeAnyEthSecp256k1PubKey(pubkey: Uint8Array): Any;
4
7
  export declare function parseChainId(evmChainId: string): string;
8
+ export declare function populateEvmTransactionDetails(api: CarbonSDK, req: ethers.providers.TransactionRequest): Promise<ethers.providers.TransactionRequest>;
@@ -1,15 +1,26 @@
1
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
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseChainId = exports.encodeAnyEthSecp256k1PubKey = exports.ETH_SECP256K1_TYPE = void 0;
12
+ exports.populateEvmTransactionDetails = exports.parseChainId = exports.encodeAnyEthSecp256k1PubKey = exports.PUBLIC_KEY_SIGNING_TEXT = exports.ETH_SECP256K1_TYPE = void 0;
4
13
  const codec_1 = require("../codec");
5
14
  const keys_1 = require("../codec/ethermint/crypto/v1/ethsecp256k1/keys");
6
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: ';
7
17
  function encodeAnyEthSecp256k1PubKey(pubkey) {
8
- return codec_1.registry.encodeAsAny({
18
+ const ethPubKey = keys_1.PubKey.fromPartial({
19
+ key: pubkey,
20
+ });
21
+ return codec_1.Any.fromPartial({
9
22
  typeUrl: exports.ETH_SECP256K1_TYPE,
10
- value: keys_1.PubKey.fromPartial({
11
- key: pubkey,
12
- }),
23
+ value: keys_1.PubKey.encode(ethPubKey).finish(),
13
24
  });
14
25
  }
15
26
  exports.encodeAnyEthSecp256k1PubKey = encodeAnyEthSecp256k1PubKey;
@@ -18,9 +29,40 @@ function parseChainId(evmChainId) {
18
29
  if (chainId.length > 48) {
19
30
  throw new Error(`chain-id '${chainId}' cannot exceed 48 chars`);
20
31
  }
21
- if (!chainId.match(/^[0-9a-f]+$/i)) {
32
+ if (!chainId.match(/^[a-z]+_\d+-\d+$/)) {
22
33
  throw new Error(`chain-id '${chainId}' does not conform to the required format`);
23
34
  }
24
35
  return chainId.split("_")[1].split("-")[0];
25
36
  }
26
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.16",
3
+ "version": "0.4.17",
4
4
  "description": "TypeScript SDK for Carbon blockchain",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",