Skip to content

Commit b332870

Browse files
committed
feat: include requestContext in inboundCommand, and use it in wallet
1 parent b4a806c commit b332870

File tree

5 files changed

+73
-41
lines changed

5 files changed

+73
-41
lines changed

packages/cosmic-swingset/lib/ag-solo/vats/lib-wallet.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -387,14 +387,19 @@ export async function makeWallet(
387387
return { zoeKind, publicAPI, offerRules, purses };
388388
}
389389

390-
async function propose(proposal) {
391-
const { id } = proposal;
392-
const newProposal = { ...proposal, status: undefined, wait: undefined };
393-
idToProposal.set(id, newProposal);
394-
updateInboxState(id, newProposal);
390+
async function propose(rawProposal, requestContext) {
391+
const { id } = rawProposal;
392+
const proposal = {
393+
...rawProposal,
394+
requestContext,
395+
status: undefined,
396+
wait: undefined,
397+
};
398+
idToProposal.set(id, proposal);
399+
updateInboxState(id, proposal);
395400

396401
// Start compiling the template, saving a promise for it.
397-
idToCompiledProposalP.set(id, compileProposal(id, newProposal));
402+
idToCompiledProposalP.set(id, compileProposal(id, proposal));
398403

399404
// Our inbox state may have an enriched proposal.
400405
updateInboxState(id, idToProposal.get(id));

packages/cosmic-swingset/lib/ag-solo/vats/vat-wallet.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function build(E, D, _log) {
2626
function getCommandHandler() {
2727
return {
2828
async processInbound(obj) {
29-
const { type, data } = obj;
29+
const { type, data, requestContext } = obj;
3030
switch (type) {
3131
case 'walletGetPurses': {
3232
if (!pursesState) return {};
@@ -45,7 +45,7 @@ function build(E, D, _log) {
4545
case 'walletPropose': {
4646
return {
4747
type: 'walletOfferAdded',
48-
data: await wallet.propose(data),
48+
data: await wallet.propose(data, requestContext),
4949
};
5050
}
5151
case 'walletDeclineOffer': {

packages/cosmic-swingset/lib/ag-solo/web.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ const send = (ws, msg) => {
1919
}
2020
};
2121

22-
export function makeHTTPListener(basedir, port, host, inboundCommand) {
22+
export function makeHTTPListener(basedir, port, host, rawInboundCommand) {
23+
// Enrich the inbound command with some metadata.
24+
const inboundCommand = (body, { headers: { origin } = {} } = {}) => {
25+
const requestContext = { origin, date: Date.now() };
26+
return rawInboundCommand({ ...body, requestContext });
27+
};
28+
2329
const app = express();
2430
// HTTP logging.
2531
app.use(
@@ -36,7 +42,9 @@ export function makeHTTPListener(basedir, port, host, inboundCommand) {
3642
fs.statSync(dapphtmldir);
3743
console.log(`Serving Dapp files from ${dapphtmldir}`);
3844
app.use(express.static(dapphtmldir));
39-
} catch (e) {}
45+
} catch (e) {
46+
// Do nothing.
47+
}
4048

4149
// serve the static HTML for the UI
4250
const htmldir = path.join(basedir, 'html');
@@ -80,7 +88,7 @@ export function makeHTTPListener(basedir, port, host, inboundCommand) {
8088
}
8189

8290
// console.log(`POST /vat got`, req.body); // should be jsonable
83-
inboundCommand(req.body).then(
91+
inboundCommand(req.body, req).then(
8492
r => res.json({ ok: true, res: r }),
8593
rej => res.json({ ok: false, rej }),
8694
);
@@ -124,23 +132,23 @@ export function makeHTTPListener(basedir, port, host, inboundCommand) {
124132
console.log(id, 'client closed');
125133
broadcasts.delete(ws);
126134
if (req.url === '/captp') {
127-
inboundCommand({ type: 'CTP_CLOSE', connectionID }).catch(_ => {});
135+
inboundCommand({ type: 'CTP_CLOSE', connectionID }, req).catch(_ => {});
128136
points.delete(connectionID);
129137
}
130138
});
131139

132140
if (req.url === '/captp') {
133141
// This is a point-to-point connection, not broadcast.
134142
points.set(connectionID, ws);
135-
inboundCommand({ type: 'CTP_OPEN', connectionID }).catch(err => {
143+
inboundCommand({ type: 'CTP_OPEN', connectionID }, req).catch(err => {
136144
console.log(id, `error establishing connection`, err);
137145
});
138146
ws.on('message', async message => {
139147
try {
140148
// some things use inbound messages
141149
const obj = JSON.parse(message);
142150
obj.connectionID = connectionID;
143-
await inboundCommand(obj);
151+
await inboundCommand(obj, req);
144152
} catch (e) {
145153
console.log(id, 'client error', e);
146154
const error = e.message || JSON.stringify(e);

packages/wallet-frontend/src/components/Inbox.jsx

+38-27
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,20 @@ const GreenChip = withStyles(theme => ({
7979
},
8080
}))(Chip);
8181

82+
const pet = petname => petname || '???';
83+
8284
export default function Inbox() {
8385
const classes = useStyles();
8486
const { state, dispatch } = useApplicationContext();
8587
const { inbox } = state;
8688

87-
function formatDate(date) {
88-
const options = {
89-
year: 'numeric',
90-
month: '2-digit',
91-
day: '2-digit',
92-
hour: '2-digit',
93-
minute: '2-digit',
94-
second: '2-digit',
95-
};
96-
return new Date(date).toLocaleDateString('en-US', options);
89+
function formatDateNow(stamp) {
90+
const date = new Date(stamp);
91+
const isoStamp = date.getTime() - date.getTimezoneOffset() * 60 * 1000;
92+
const isoDate = new Date(isoStamp);
93+
const isoStr = isoDate.toISOString();
94+
const match = isoStr.match(/^(.*)T(.*)\..*/);
95+
return <>{match[1]}&nbsp;{match[2]}</>;
9796
}
9897

9998
function handleCancel(id) {
@@ -141,10 +140,11 @@ export default function Inbox() {
141140
<List>
142141
{inbox.map(
143142
({
143+
requestContext: { date, origin = 'unknown origin'} = {},
144144
id,
145-
id: date,
146145
instanceRegKey,
147-
offerRulesTemplate,
146+
instancePetname,
147+
offerRulesTemplate: { offer = {}, want = {} },
148148
status,
149149
wait,
150150
}) => (
@@ -154,42 +154,53 @@ export default function Inbox() {
154154
</ListItemIcon>
155155
<Grid container direction="column">
156156
<Grid item>
157-
<Typography variant="body2" display="block">
158-
<i>({instanceRegKey})</i> - {formatDate(date)}
157+
<Typography variant="body2" display="block" color="secondary">
158+
At&nbsp;
159+
{date ? formatDateNow(date) : <i>unknown time</i>} via&nbsp;
160+
{origin}
161+
</Typography>
162+
</Grid>
163+
<Grid item>
164+
<Typography variant="body2" display="block" color="secondary">
165+
<Box component="span" fontWeight={800}>
166+
{pet(instancePetname)}
167+
&nbsp;
168+
</Box>
169+
{!instancePetname && <i>({instanceRegKey})&nbsp;</i>}
170+
says:
159171
</Typography>
160172
</Grid>
161173
<Grid item>
162-
{Object.entries(offerRulesTemplate.offer || {}).map(
174+
{Object.entries(offer).map(
163175
([role, { issuerPetname, pursePetname, brandRegKey, extent }], i) => (
164176
<Typography key={`offer${role}`} variant="body1">
165177
{i === 0 ? 'Pay' : <>and&nbsp;pay</>}&nbsp;
166178
<Box component="span" fontWeight={800}>
167179
{extent}
168180
&nbsp;
169-
{issuerPetname || '??'}
170-
&nbsp;
181+
{pet(issuerPetname)}
171182
</Box>
172-
{brandRegKey && <i>({brandRegKey})&nbsp;</i>}
173-
from&nbsp;
183+
{!issuerPetname && <>&nbsp;<i>({brandRegKey})</i></>} from&nbsp;
174184
<Box component="span" fontWeight={800}>
175-
{pursePetname}
185+
{pet(pursePetname)}
176186
</Box>
177187
</Typography>
178188
))}
179-
{Object.entries(offerRulesTemplate.want || {}).map(
189+
{Object.entries(want).map(
180190
([role, { issuerPetname, pursePetname, brandRegKey, extent }], i) => (
181191
<Typography key={`offer${role}`} variant="body1">
182-
{i === 0 ? 'Receive' : <>and&nbsp;receieve</>}&nbsp;
192+
{i === 0 ?
193+
(Object.keys(offer).length > 0 ? <>to&nbsp;receive</> : 'Receive') :
194+
<>and&nbsp;receieve</>
195+
}&nbsp;
183196
<Box component="span" fontWeight={800}>
184197
{extent}
185198
&nbsp;
186-
{issuerPetname || '??'}
187-
&nbsp;
199+
{pet(issuerPetname)}
188200
</Box>
189-
{brandRegKey && <i>({brandRegKey})&nbsp;</i>}
190-
into&nbsp;
201+
{!issuerPetname && <>&nbsp;<i>({brandRegKey})</i></>} into&nbsp;
191202
<Box component="span" fontWeight={800}>
192-
{pursePetname}
203+
{pet(pursePetname)}
193204
</Box>
194205
</Typography>
195206
))}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#! /bin/sh
2+
awb=../agoric-cli/agoric-wallet-build
3+
set -xe
4+
yarn build
5+
git rm -rf "$awb"
6+
rm -rf "$awb"
7+
cp -r build "$awb"
8+
git add "$awb"

0 commit comments

Comments
 (0)