@agoric/orchestration 0.1.1-dev-acbb5da.0 → 0.1.1-dev-5fb1074.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.
Files changed (43) hide show
  1. package/package.json +18 -17
  2. package/src/examples/auto-stake-it.contract.d.ts.map +1 -1
  3. package/src/examples/auto-stake-it.contract.js +1 -1
  4. package/src/examples/basic-flows.contract.d.ts.map +1 -1
  5. package/src/examples/basic-flows.contract.js +1 -1
  6. package/src/examples/send-anywhere.contract.d.ts +11 -8
  7. package/src/examples/send-anywhere.contract.d.ts.map +1 -1
  8. package/src/examples/send-anywhere.contract.js +5 -4
  9. package/src/examples/staking-combinations.contract.d.ts.map +1 -1
  10. package/src/examples/staking-combinations.contract.js +1 -1
  11. package/src/examples/swap.contract.d.ts.map +1 -1
  12. package/src/examples/swap.contract.js +1 -1
  13. package/src/examples/unbond.contract.d.ts.map +1 -1
  14. package/src/examples/unbond.contract.js +1 -1
  15. package/src/exos/cosmos-orchestration-account.d.ts +14 -10
  16. package/src/exos/cosmos-orchestration-account.d.ts.map +1 -1
  17. package/src/exos/cosmos-orchestration-account.js +18 -13
  18. package/src/exos/local-chain-facade.d.ts +1 -1
  19. package/src/exos/local-chain-facade.d.ts.map +1 -1
  20. package/src/exos/local-chain-facade.js +9 -7
  21. package/src/exos/local-orchestration-account.d.ts +2 -2
  22. package/src/exos/local-orchestration-account.d.ts.map +1 -1
  23. package/src/exos/local-orchestration-account.js +11 -5
  24. package/src/exos/remote-chain-facade.d.ts +1 -1
  25. package/src/exos/remote-chain-facade.d.ts.map +1 -1
  26. package/src/exos/remote-chain-facade.js +12 -8
  27. package/src/facade.d.ts +1 -4
  28. package/src/facade.d.ts.map +1 -1
  29. package/src/facade.js +2 -9
  30. package/src/fixtures/query-flows.contract.d.ts.map +1 -1
  31. package/src/fixtures/query-flows.contract.js +1 -1
  32. package/src/utils/start-helper.d.ts +10 -2
  33. package/src/utils/start-helper.d.ts.map +1 -1
  34. package/src/utils/start-helper.js +15 -6
  35. package/tools/ibc-mocks.d.ts +116 -0
  36. package/tools/ibc-mocks.d.ts.map +1 -0
  37. package/tools/ibc-mocks.ts +262 -0
  38. package/tools/make-test-address.d.ts +2 -0
  39. package/tools/make-test-address.d.ts.map +1 -0
  40. package/tools/make-test-address.js +21 -0
  41. package/tools/protobuf-decoder.d.ts +19 -0
  42. package/tools/protobuf-decoder.d.ts.map +1 -0
  43. package/tools/protobuf-decoder.js +153 -0
@@ -0,0 +1,153 @@
1
+ /* eslint-env node */
2
+ /* eslint-disable -- generated by Sonnet, easier to leave alone */
3
+
4
+ const WIRE_TYPES = {
5
+ VARINT: 0,
6
+ FIXED64: 1,
7
+ LENGTH_DELIMITED: 2,
8
+ START_GROUP: 3,
9
+ END_GROUP: 4,
10
+ FIXED32: 5,
11
+ };
12
+
13
+ function decodeVarint(buffer, offset) {
14
+ let result = 0n;
15
+ let shift = 0;
16
+ let byte;
17
+
18
+ do {
19
+ if (offset >= buffer.length) {
20
+ throw new Error('Buffer overflow while decoding varint');
21
+ }
22
+ byte = buffer[offset];
23
+ result |= BigInt(byte & 0x7f) << BigInt(shift);
24
+ shift += 7;
25
+ offset++;
26
+ } while (byte & 0x80);
27
+
28
+ return { value: result, bytesRead: shift / 7 };
29
+ }
30
+
31
+ function decodeFixed32(buffer, offset) {
32
+ if (offset + 4 > buffer.length) {
33
+ throw new Error('Buffer overflow while decoding fixed32');
34
+ }
35
+ return {
36
+ value: buffer.readUInt32LE(offset),
37
+ bytesRead: 4,
38
+ };
39
+ }
40
+
41
+ function decodeFixed64(buffer, offset) {
42
+ if (offset + 8 > buffer.length) {
43
+ throw new Error('Buffer overflow while decoding fixed64');
44
+ }
45
+ const low = buffer.readUInt32LE(offset);
46
+ const high = buffer.readUInt32LE(offset + 4);
47
+ return {
48
+ value: BigInt(high) * 2n ** 32n + BigInt(low),
49
+ bytesRead: 8,
50
+ };
51
+ }
52
+
53
+ function decodeString(buffer, offset, length) {
54
+ if (offset + length > buffer.length) {
55
+ throw new Error('Buffer overflow while decoding string');
56
+ }
57
+ return {
58
+ value: buffer.toString('utf8', offset, offset + length),
59
+ bytesRead: length,
60
+ };
61
+ }
62
+
63
+ function decodeField(buffer, offset) {
64
+ const tag = decodeVarint(buffer, offset);
65
+ offset += tag.bytesRead;
66
+
67
+ const fieldNumber = Number(tag.value >> 3n);
68
+ const wireType = Number(tag.value & 0x7n);
69
+
70
+ let value;
71
+ let bytesRead;
72
+
73
+ switch (wireType) {
74
+ case WIRE_TYPES.VARINT:
75
+ ({ value, bytesRead } = decodeVarint(buffer, offset));
76
+ break;
77
+ case WIRE_TYPES.FIXED64:
78
+ ({ value, bytesRead } = decodeFixed64(buffer, offset));
79
+ break;
80
+ case WIRE_TYPES.LENGTH_DELIMITED:
81
+ const length = decodeVarint(buffer, offset);
82
+ offset += length.bytesRead;
83
+ // Try to decode as a nested message first
84
+ try {
85
+ ({ value, bytesRead } = decodeProtobuf(
86
+ buffer.slice(offset, offset + Number(length.value)),
87
+ ));
88
+ } catch (e) {
89
+ // If it fails, decode as a string
90
+ ({ value, bytesRead } = decodeString(
91
+ buffer,
92
+ offset,
93
+ Number(length.value),
94
+ ));
95
+ }
96
+ bytesRead += length.bytesRead;
97
+ break;
98
+ case WIRE_TYPES.FIXED32:
99
+ ({ value, bytesRead } = decodeFixed32(buffer, offset));
100
+ break;
101
+ default:
102
+ throw new Error(`Unsupported wire type: ${wireType}`);
103
+ }
104
+
105
+ return {
106
+ fieldNumber,
107
+ wireType,
108
+ value,
109
+ bytesRead: tag.bytesRead + bytesRead,
110
+ };
111
+ }
112
+
113
+ function getFieldName(fieldNumber, wireType) {
114
+ const typePrefix =
115
+ wireType === WIRE_TYPES.LENGTH_DELIMITED
116
+ ? 'subMessage'
117
+ : wireType === WIRE_TYPES.VARINT
118
+ ? 'int'
119
+ : wireType === WIRE_TYPES.FIXED32
120
+ ? 'fixed32'
121
+ : wireType === WIRE_TYPES.FIXED64
122
+ ? 'fixed64'
123
+ : 'string';
124
+ return `${typePrefix}_${fieldNumber}`;
125
+ }
126
+
127
+ /**
128
+ * Decodes a protobuf message from the given buffer.
129
+ *
130
+ * @param {Buffer} buffer
131
+ */
132
+ export function decodeProtobuf(buffer) {
133
+ let offset = 0;
134
+ const message = {};
135
+
136
+ while (offset < buffer.length) {
137
+ const field = decodeField(buffer, offset);
138
+ const fieldName = getFieldName(field.fieldNumber, field.wireType);
139
+ message[fieldName] = field.value;
140
+ offset += field.bytesRead;
141
+ }
142
+
143
+ return { value: message, bytesRead: offset };
144
+ }
145
+ /**
146
+ * Decodes a protobuf message from the given base64-encoded data.
147
+ *
148
+ * @param {string} base64String
149
+ */
150
+ export function decodeProtobufBase64(base64String) {
151
+ const buffer = Buffer.from(base64String, 'base64');
152
+ return decodeProtobuf(buffer);
153
+ }