Skip to content

Commit

Permalink
feat: include requestContext in inboundCommand, and use it in wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Mar 11, 2020
1 parent b4a806c commit b332870
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 41 deletions.
17 changes: 11 additions & 6 deletions packages/cosmic-swingset/lib/ag-solo/vats/lib-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,14 +387,19 @@ export async function makeWallet(
return { zoeKind, publicAPI, offerRules, purses };
}

async function propose(proposal) {
const { id } = proposal;
const newProposal = { ...proposal, status: undefined, wait: undefined };
idToProposal.set(id, newProposal);
updateInboxState(id, newProposal);
async function propose(rawProposal, requestContext) {
const { id } = rawProposal;
const proposal = {
...rawProposal,
requestContext,
status: undefined,
wait: undefined,
};
idToProposal.set(id, proposal);
updateInboxState(id, proposal);

// Start compiling the template, saving a promise for it.
idToCompiledProposalP.set(id, compileProposal(id, newProposal));
idToCompiledProposalP.set(id, compileProposal(id, proposal));

// Our inbox state may have an enriched proposal.
updateInboxState(id, idToProposal.get(id));
Expand Down
4 changes: 2 additions & 2 deletions packages/cosmic-swingset/lib/ag-solo/vats/vat-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function build(E, D, _log) {
function getCommandHandler() {
return {
async processInbound(obj) {
const { type, data } = obj;
const { type, data, requestContext } = obj;
switch (type) {
case 'walletGetPurses': {
if (!pursesState) return {};
Expand All @@ -45,7 +45,7 @@ function build(E, D, _log) {
case 'walletPropose': {
return {
type: 'walletOfferAdded',
data: await wallet.propose(data),
data: await wallet.propose(data, requestContext),
};
}
case 'walletDeclineOffer': {
Expand Down
20 changes: 14 additions & 6 deletions packages/cosmic-swingset/lib/ag-solo/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ const send = (ws, msg) => {
}
};

export function makeHTTPListener(basedir, port, host, inboundCommand) {
export function makeHTTPListener(basedir, port, host, rawInboundCommand) {
// Enrich the inbound command with some metadata.
const inboundCommand = (body, { headers: { origin } = {} } = {}) => {
const requestContext = { origin, date: Date.now() };
return rawInboundCommand({ ...body, requestContext });
};

const app = express();
// HTTP logging.
app.use(
Expand All @@ -36,7 +42,9 @@ export function makeHTTPListener(basedir, port, host, inboundCommand) {
fs.statSync(dapphtmldir);
console.log(`Serving Dapp files from ${dapphtmldir}`);
app.use(express.static(dapphtmldir));
} catch (e) {}
} catch (e) {
// Do nothing.
}

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

// console.log(`POST /vat got`, req.body); // should be jsonable
inboundCommand(req.body).then(
inboundCommand(req.body, req).then(
r => res.json({ ok: true, res: r }),
rej => res.json({ ok: false, rej }),
);
Expand Down Expand Up @@ -124,23 +132,23 @@ export function makeHTTPListener(basedir, port, host, inboundCommand) {
console.log(id, 'client closed');
broadcasts.delete(ws);
if (req.url === '/captp') {
inboundCommand({ type: 'CTP_CLOSE', connectionID }).catch(_ => {});
inboundCommand({ type: 'CTP_CLOSE', connectionID }, req).catch(_ => {});
points.delete(connectionID);
}
});

if (req.url === '/captp') {
// This is a point-to-point connection, not broadcast.
points.set(connectionID, ws);
inboundCommand({ type: 'CTP_OPEN', connectionID }).catch(err => {
inboundCommand({ type: 'CTP_OPEN', connectionID }, req).catch(err => {
console.log(id, `error establishing connection`, err);
});
ws.on('message', async message => {
try {
// some things use inbound messages
const obj = JSON.parse(message);
obj.connectionID = connectionID;
await inboundCommand(obj);
await inboundCommand(obj, req);
} catch (e) {
console.log(id, 'client error', e);
const error = e.message || JSON.stringify(e);
Expand Down
65 changes: 38 additions & 27 deletions packages/wallet-frontend/src/components/Inbox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,20 @@ const GreenChip = withStyles(theme => ({
},
}))(Chip);

const pet = petname => petname || '???';

export default function Inbox() {
const classes = useStyles();
const { state, dispatch } = useApplicationContext();
const { inbox } = state;

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

function handleCancel(id) {
Expand Down Expand Up @@ -141,10 +140,11 @@ export default function Inbox() {
<List>
{inbox.map(
({
requestContext: { date, origin = 'unknown origin'} = {},
id,
id: date,
instanceRegKey,
offerRulesTemplate,
instancePetname,
offerRulesTemplate: { offer = {}, want = {} },
status,
wait,
}) => (
Expand All @@ -154,42 +154,53 @@ export default function Inbox() {
</ListItemIcon>
<Grid container direction="column">
<Grid item>
<Typography variant="body2" display="block">
<i>({instanceRegKey})</i> - {formatDate(date)}
<Typography variant="body2" display="block" color="secondary">
At&nbsp;
{date ? formatDateNow(date) : <i>unknown time</i>} via&nbsp;
{origin}
</Typography>
</Grid>
<Grid item>
<Typography variant="body2" display="block" color="secondary">
<Box component="span" fontWeight={800}>
{pet(instancePetname)}
&nbsp;
</Box>
{!instancePetname && <i>({instanceRegKey})&nbsp;</i>}
says:
</Typography>
</Grid>
<Grid item>
{Object.entries(offerRulesTemplate.offer || {}).map(
{Object.entries(offer).map(
([role, { issuerPetname, pursePetname, brandRegKey, extent }], i) => (
<Typography key={`offer${role}`} variant="body1">
{i === 0 ? 'Pay' : <>and&nbsp;pay</>}&nbsp;
<Box component="span" fontWeight={800}>
{extent}
&nbsp;
{issuerPetname || '??'}
&nbsp;
{pet(issuerPetname)}
</Box>
{brandRegKey && <i>({brandRegKey})&nbsp;</i>}
from&nbsp;
{!issuerPetname && <>&nbsp;<i>({brandRegKey})</i></>} from&nbsp;
<Box component="span" fontWeight={800}>
{pursePetname}
{pet(pursePetname)}
</Box>
</Typography>
))}
{Object.entries(offerRulesTemplate.want || {}).map(
{Object.entries(want).map(
([role, { issuerPetname, pursePetname, brandRegKey, extent }], i) => (
<Typography key={`offer${role}`} variant="body1">
{i === 0 ? 'Receive' : <>and&nbsp;receieve</>}&nbsp;
{i === 0 ?
(Object.keys(offer).length > 0 ? <>to&nbsp;receive</> : 'Receive') :
<>and&nbsp;receieve</>
}&nbsp;
<Box component="span" fontWeight={800}>
{extent}
&nbsp;
{issuerPetname || '??'}
&nbsp;
{pet(issuerPetname)}
</Box>
{brandRegKey && <i>({brandRegKey})&nbsp;</i>}
into&nbsp;
{!issuerPetname && <>&nbsp;<i>({brandRegKey})</i></>} into&nbsp;
<Box component="span" fontWeight={800}>
{pursePetname}
{pet(pursePetname)}
</Box>
</Typography>
))}
Expand Down
8 changes: 8 additions & 0 deletions packages/wallet-frontend/update-agoric-cli.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#! /bin/sh
awb=../agoric-cli/agoric-wallet-build
set -xe
yarn build
git rm -rf "$awb"
rm -rf "$awb"
cp -r build "$awb"
git add "$awb"

0 comments on commit b332870

Please sign in to comment.