Skip to content

Commit

Permalink
Add invitations
Browse files Browse the repository at this point in the history
  • Loading branch information
danfinlay committed May 28, 2022
1 parent ceefb4c commit ed1243c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
15 changes: 10 additions & 5 deletions packages/js-eth-delegatable-utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,21 +272,26 @@ exports.createInvitation = function createInvitation (contractInfo, recipientAdd
/* This is designed to be a particularly convenient method for interacting with delegations.
* It creates an object that can be used to sign a delegation, or revoke a delegation.
*/
exports.createMembership = function createMembership (contractInfo, power) {
let { invitation, key } = power;
exports.createMembership = function createMembership (opts = {}) {
let { invitation, key, contractInfo } = opts;
if (!invitation && !key) {
throw new Error('Either an invitation or a key is required.');
}
if (!key) {
key = invitation.key;

if (!contractInfo && invitation.contractInfo) {
contractInfo = invitation.contractInfo;
}
}

return {
createInvitation (recipientAddress) {
if (invitation) {
return createInvitation(contractInfo, recipientAddress, invitation);
if (recipientAddress) {
return exports.createInvitation(contractInfo, recipientAddress, invitation);
} else {
return signDelegation(contractInfo, key);
// When there is no recipient specified
return exports.signDelegation(contractInfo, key);
}
},

Expand Down
40 changes: 39 additions & 1 deletion packages/js-eth-delegatable-utils/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const test = require('tape');
const { recoverSigner, signDelegation } = require('./index.js');
const { recoverSigner, signDelegation, createMembership } = require('./index.js');
const sigUtil = require('@metamask/eth-sig-util');

const address = '0xa2c5B479d1758C48c68540F554cDAeDda2340630';
Expand Down Expand Up @@ -162,6 +162,44 @@ test('recover a signature', async (t) => {

});

test('membership API requires power', async (t) => {
const contractInfo = {
chainId: 1337,
verifyingContract: '0x336E14B1723Dc5A57769F0aa5C409F476Ee8B333',
name: "PhisherRegistry",
}

try {
const aliceMembership = createMembership(contractInfo);
} catch (err) {
t.equals(err.message, 'Either an invitation or a key is required.');
}
});

test('membership API can delegate to new key', async (t) => {
const contractInfo = {
chainId: 1337,
verifyingContract: '0x336E14B1723Dc5A57769F0aa5C409F476Ee8B333',
name: "PhisherRegistry",
}

const aliceMembership = createMembership({
contractInfo,
key: PRIV_KEY,
});
t.ok(aliceMembership, 'membership should be created');

const bobInvitation = aliceMembership.createInvitation();
console.dir(bobInvitation);
t.ok(bobInvitation.key);

const bobMembership = createMembership({
invitation: bobInvitation,
});


});

function fromHexString (hexString) {
if (!hexString || typeof hexString !== 'string') {
throw new Error('Expected a hex string.');
Expand Down

0 comments on commit ed1243c

Please sign in to comment.