This section will help you get started with running a verified transaction using Newton Protocol. For this example we will use an existing policy that checks whether the transaction is being sent to a sanctioned address. If it is not sanctioned, the transaction is permitted.Here, the policy and the policy client is already deployed to Newton and Ethereum Sepolia respectively. We will submit a task to Newton Protocol, then a transaction to Ethereum Mainnet to execute the compliant transaction. See here if you want to write your own policy.You can follow along in codesandbox here.
In order to run verified transactions, you must first set up a policy and policy client. For the purposes of the quickstart, these two have already been written and deployed. See below for details on how they work:
Show Policy
PolicyThe below contains the policy code that we will use for this example. This is written in a language called rego, see here for how to write your own policy code.
The policy code for this example has already been deployed and uploaded to IPFS and a pointer is stored on Ethereum here. No action is required
Copy
Ask AI
# Chainalysis Sanctions Policy# --------------------------------package chainalysis_sanctions_agent# By default, deny requests.default allow := false# From Intentto_address := input.to# From Policy Dataapi_call_status := data.data.statuschecked_address := data.data.addressis_sanctioned := data.data.sanctioned# Allow the action if the `to` address is not sanctionedallow if { api_call_status == 200 checked_address == to_address not is_sanctioned}
The policy returns true if the API call to Chainalysis with the address returned is not sanctioned. The next section will show how this API call is made via the policy data oracle.Third party offchain data can be executed as part of a policy. The below shows the source code for the data oracle that is pulled from Chainalysis for the policy outlined in the previous section.See here if you want to write your own data oracle.
The following data oracle is already deployed. No action is required
Here the Chainlysis endpoint for sanctions screenining is checked, see here for more details about the endpoint.
Show Policy client
The following shows a smart contract that has implemented the above policy. This is known in the Newton protocol as a policy client. This particular example can be found on Ethereum Sepolia here.Note that the method sanctionProtectedTransfercontains a require condition that enforces the Newton policy. A proof must be provided when using this smart contract else the method will fail.
This policy has been deployed and is located here on Eth Sepolia. No action is required
Copy
Ask AI
// SPDX-License-Identifier: MITpragma solidity ^0.8.27;import {NewtonPolicyClient} from "@newton/contracts/src/mixins/NewtonPolicyClient.sol";import {INewtonPolicy} from "@newton/contracts/src/interfaces/INewtonPolicy.sol";import {NewtonMessage} from "@newton/contracts/src/core/NewtonMessage.sol";contract YourVault is NewtonPolicyClient { event Success(); error InvalidAttestation(); error TransferFailed(); // since the factory is used to clone the client, the constructor doesn't need to do anything constructor() {} // this is called by the deploy script function initialize( address policyTaskManager, address policy, //refers to the policy address address owner ) external { _initNewtonPolicyClient(policyTaskManager, policy, owner); } // this function duplicates the functionality the base NewtonPolicyClient without permissioning // don't do this in production function setParameters(INewtonPolicy.PolicyConfig memory _config) external { _setPolicy(_config); } // this is the policy guarded function function sanctionProtectedTransfer(NewtonMessage.Attestation memory attestation) external payable { require(_validateAttestation(attestation), InvalidAttestation()); // Your function's business logic goes here // Extract recipient and value from the attestation intent address recipient = attestation.intent.to; uint256 amount = attestation.intent.value; // Transfer ETH to the recipient (bool success, ) = recipient.call{value: amount}(""); require(success, TransferFailed()); emit Success(); }}
Now that a policy is deployed and ready we can submit a transaction against it. But first a proof needs to be generated. To do that, we will submit a task to the Newton protocol which will return the proof.Before proceeding, ensure that you install the newton-protocol-sdk:
Copy
Ask AI
npm install @magicnewton/newton-protocol-sdk
Note the public address of your PRIVATE_KEY needs to be allowlisted by the Newton team before proceeding with this step.
At this point we have a proof that be used execute the transaction. The following code submits a transaction with the proof using viem.
Copy
Ask AI
import { createPublicClient, custom, walletActions } from "viem";const browserWalletClient = createWalletClient({ chain: sepolia, transport: custom(window.ethereum),}).extend(walletActions);console.log("Submitting transaction with attestation:", attestation);// Submit the transactionconst hash = await browserWalletClient.writeContract({ address: POLICY_CLIENT_ADDRESS as `0x${string}`, abi: POLICY_CLIENT_ABI, functionName: "sanctionProtectedTransfer", args: [attestation], account: connectedAddress as `0x${string}`,});console.log("Transaction submitted:", hash);
The transaction should go through. We have demonstrated how a transaction can be validated against offchain data in a policy enforced by Newton Protocol.You can follow along in codesandbox here.