4.1.2 • Published 1 year ago

eth-delegatable-utils v4.1.2

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

Eth Delegatable Utils

A JavaScript library for more easily managing invite links and signatures for The Delegatable Framework.

Installation

npm install eth-delegatable-utils -S

Usage

To understand how Delegatable works, it might help to review the types it involves.

In short:

  • Any user can sign a delegation message.
  • A delegation can have any number of caveats.
  • A caveat specifies a contract that has the right to reject a proposed transaction.
  • A transaction is specified within an invocation.
  • An invocation is included in the batch of an invocations object (sorry!)
  • Each invocation has an authority which is either an empty array (to be a normal MetaTransaction) or is an array of delegation objects that chain from the original signer to act as, up to the signer of this invocation.
  • A SignedInvocations object is an authorized set of invocations to perform.
import { createMembership } from 'eth-delegatable-utils';

// To initialize, you need the basic EIP-712 domain info:
const contractInfo = {
  chainId: 1337,
  verifyingContract: '0x336E14B1723Dc5A57769F0aa5C409F476Ee8B333',
  name: "PhisherRegistry",
}

// You can initialize a membership with a private key to allow a key to delegate:
const aliceMembership = createMembership({
  contractInfo,
  key: PRIV_KEY,
});

// Users can create invitations of their own
// Providing no arguments will result in an invitation that includes a key
// The recipient has to trust the sender, but hey, they already kinda did.
// The invitation object is JSON-serializable.
const invitation = aliceMembership.createInvitation();

// Memberships can be initialized with `invitation` objects:
const bobMembership = createMembership({
  contractInfo,
  invitation,
});

// Invitations can also include arbitrary caveats, which are enforced by CaveatEnforcer contracts.
// https://github.com/danfinlay/delegatable-eth/tree/main/packages/hardhat/contracts/caveat-enforcers
// If no `delegation` paramter is provided, a default one is generated that points at the verifyingContract.
const carolInvitation = bobMembership.createInvitation({
  delegation: {
    caveats: [
      {
        enforcer: ENFORCER_CONTRACT_ADDRESS,
        terms: BYTES_TERMS_INTERPRETED_BY_ENFORCER,
      }
    ],
  }
});


// Users can then sign invocations with their memberships.
// First we'll construct an invocation.
// Here's how you would use ethers to generate transaction data for a MetaTransaction.
// We'll add a better convenience method for this kind of thing later:
const desiredTx = await registry.populateTransaction.claimIfPhisher(`TWT:${_phisher.toLowerCase()}`, true);
const invocation = {
  transaction: {
    to: address,
    data: desiredTx.data,
    gasLimit: 500000,
  },
  authority: signedDelegations,
}

// The nonces don't need to block here because we also have a queue for the nonces.
// If you don't want to keep track of the nonces you can usually just pick a random queue.
const queue = Math.floor(Math.random() * 100000000);

// Then you can just sign the invocations:
const signedInvocations = carolMembership.signInvocations({
  batch: invocations,
  replayProtection: {
    nonce: 1,
    queue,
  }
});

// Once the invocations are signed, then you can just call the `invoke()` function.
// Anyone can do this. They're just relaying the transaction now. The invocation signing
// is what authorized it.
return await registry.invoke([signedInvocations]);
4.0.4

1 year ago

4.1.0

1 year ago

4.0.1

1 year ago

4.0.0

1 year ago

4.1.2

1 year ago

4.0.3

1 year ago

4.1.1

1 year ago

4.0.2

1 year ago

2.1.2

2 years ago

2.1.1

2 years ago

2.1.4

2 years ago

2.1.3

2 years ago

2.1.5

2 years ago

2.1.0

2 years ago

2.0.0

2 years ago

1.5.2

2 years ago

1.5.1

2 years ago

1.5.0

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.3.2

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago