-
Notifications
You must be signed in to change notification settings - Fork 0
/
convertObject.ts
165 lines (156 loc) · 6.49 KB
/
convertObject.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { ContractExecutionAuthorization, ContractMigrationAuthorization, MaxCallsLimit, MaxFundsLimit, CombinedLimit, AllowAllMessagesFilter, AcceptedMessageKeysFilter, AcceptedMessagesFilter } from "cosmjs-types/cosmwasm/wasm/v1/authz";
export const snakeToCamel = (str) =>
str.toLowerCase().replace(/([-_][a-z])/g, group =>
group
.toUpperCase()
.replace('-', '')
.replace('_', '')
);
export const convertStruct = (type, value) => {
switch (type) {
case "ContractExecutionAuthorization":
return ContractExecutionAuthorization.encode(value).finish()
case "ContractMigrationAuthorization":
return ContractMigrationAuthorization.encode(value).finish()
case "MaxCallsLimit":
return MaxCallsLimit.encode(value).finish()
case "MaxFundsLimit":
return MaxFundsLimit.encode(value).finish()
case "CombinedLimit":
return CombinedLimit.encode(value).finish()
case "AllowAllMessagesFilter":
return AllowAllMessagesFilter.encode(value).finish()
case "AcceptedMessageKeysFilter":
return AcceptedMessageKeysFilter.encode(value).finish()
case "AcceptedMessagesFilter":
return AcceptedMessagesFilter.encode(value).finish()
default:
throw "Type not supported"
}
}
export const convertObjProperties = (obj) => {
let newObj = {}
for (const [key, value] of Object.entries(obj)) {
if (key !== 'msg' && !Array.isArray(value)) {
const camelCaseKey = snakeToCamel(key)
if (typeof value === 'object') {
const newVal = convertObjProperties(value)
newObj[camelCaseKey] = newVal
continue
}
newObj[camelCaseKey] = value
} else {
newObj[key] = value
}
}
return newObj
}
function recursivelyEncodeObjectToBytes(obj) {
let grantCon = obj.value.grant.authorization.value.grants
for (let key in grantCon) {
if (grantCon[key].limit) {
let lastVal = grantCon[key].limit.value;
delete grantCon[key].limit.value
let splitArr = grantCon[key].limit.typeUrl.split(".")
grantCon[key].limit["value"] = convertStruct(splitArr[splitArr.length-1], lastVal)
}
if (grantCon[key].filter) {
let lastVal = grantCon[key].filter.value;
delete grantCon[key].filter.value
let splitArr = grantCon[key].limit.typeUrl.split(".")
grantCon[key].filter["value"] = convertStruct(splitArr[splitArr.length-1], lastVal)
}
}
let lastValGrants = obj.value.grant.authorization.value
delete obj.value.grant.authorization.value
let splitArr = obj.value.grant.authorization.typeUrl.split(".")
obj.value.grant.authorization["value"] = convertStruct(splitArr[splitArr.length-1], lastValGrants)
return obj
}
export const convertObjEncode = (obj) => {
for (const [key, value] of Object.entries(obj)) {
}
}
const runAll = async () => {
let mock = {
grantee: "migaloo1sxpxhzprejg5rgth0k74ws782mw00ten640zl0",
granter: "migaloo1s5898aa2tj0yaccg3xwewv3dft3q5nf8x7fvr8",
grant: {
authorization: {
typeUrl: "/cosmwasm.wasm.v1.ContractExecutionAuthorization",
value: {
grants: [
{
contract: "migaloo14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s58v48z",
limit: {
typeUrl: "/cosmwasm.wasm.v1.CombinedLimit",
value: {
callsRemaining: BigInt("100"),
amounts: [
{
denom: "uwhale",
amount: "250",
}
]
}
},
filter: {
typeUrl: "/cosmwasm.wasm.v1.AcceptedMessageKeysFilter",
value: {
keys: [
'transfer_price'
]
}
},
}
],
}
},
expiration: undefined
}
}
const encoder = new TextEncoder();
let rightMock = {
typeUrl: "/cosmos.authz.v1beta1.MsgGrant",
value: {
grantee: "migaloo1sxpxhzprejg5rgth0k74ws782mw00ten640zl0",
granter: "migaloo1s5898aa2tj0yaccg3xwewv3dft3q5nf8x7fvr8",
grant: {
authorization: {
typeUrl: "/cosmwasm.wasm.v1.ContractExecutionAuthorization",
value: encoder.encode(JSON.stringify({
grants: [
{
contract: "migaloo14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s58v48z",
limit: {
typeUrl: "/cosmwasm.wasm.v1.CombinedLimit",
value: encoder.encode(JSON.stringify({
callsRemaining: "100",
amounts: [
{
denom: "uwhale",
amount: "250",
}
]
})),
},
filter: {
typeUrl: "/cosmwasm.wasm.v1.AcceptedMessageKeysFilter",
value: encoder.encode(JSON.stringify({
keys: [
'transfer_price'
]
})),
},
}
],
})),
},
expiration: undefined
}
},
}
console.log(rightMock.value.grant)
convertObjProperties(mock)
}
runAll()