Skip to content

Commit

Permalink
Low level wallet API
Browse files Browse the repository at this point in the history
  • Loading branch information
tuler committed Sep 1, 2023
1 parent d031407 commit 38202d0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/violet-meals-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@deroll/wallet": minor
---

expose low level wallet API
34 changes: 33 additions & 1 deletion packages/wallet/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { parseAbi } from "viem";
import { hexToBigInt, hexToBool, parseAbi, slice } from "viem";
import { WalletApp, WalletAppImpl } from "./wallet";
import { AdvanceRequestData, Payload } from "@deroll/app";
import { erc20PortalAddress, etherPortalAddress } from "./rollups";

export type { WalletApp } from "./wallet";

Expand All @@ -12,3 +14,33 @@ export const WalletABI = parseAbi([
export const createWallet = (): WalletApp => {
return new WalletAppImpl();
};

/**
* Decode input according to https://github.com/cartesi/rollups/tree/v1.0.0#input-encodings-for-deposits
* @param payload input payload
* @returns
*/
export const parseEtherDeposit = (payload: Payload) => {
const sender = slice(payload, 0, 20); // 20 bytes for address
const value = hexToBigInt(slice(payload, 20, 52)); // 32 bytes for uint256
return { sender, value };
};

/**
* Decode input according to https://github.com/cartesi/rollups/tree/v1.0.0#input-encodings-for-deposits
* @param payload input payload
* @returns
*/
export const parseERC20Deposit = (payload: Payload) => {
const success = hexToBool(slice(payload, 0, 1)); // 1 byte for boolean
const token = slice(payload, 1, 21); // 20 bytes for address
const sender = slice(payload, 21, 41); // 20 bytes for address
const amount = hexToBigInt(slice(payload, 41, 73)); // 32 bytes for uint256
return { success, token, sender, amount };
};

export const isEtherDeposit = (data: AdvanceRequestData) =>
data.metadata.msg_sender === etherPortalAddress;

export const isERC20Deposit = (data: AdvanceRequestData) =>
data.metadata.msg_sender === erc20PortalAddress;
38 changes: 13 additions & 25 deletions packages/wallet/src/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import { AdvanceRequestHandler, Voucher } from "@deroll/app";
import {
Address,
encodeFunctionData,
hexToBigInt,
hexToBool,
slice,
} from "viem";
import { Address, encodeFunctionData } from "viem";

import { cartesiDAppABI, dAppAddressRelayAddress, erc20ABI } from "./rollups";
import {
cartesiDAppABI,
dAppAddressRelayAddress,
erc20ABI,
erc20PortalAddress,
etherPortalAddress,
} from "./rollups";
isERC20Deposit,
isEtherDeposit,
parseERC20Deposit,
parseEtherDeposit,
} from ".";

export type Wallet = {
ether: bigint;
Expand Down Expand Up @@ -54,23 +48,17 @@ export class WalletAppImpl implements WalletApp {
}

public handler: AdvanceRequestHandler = async (data) => {
if (data.metadata.msg_sender === etherPortalAddress) {
// decode input according to https://github.com/cartesi/rollups/tree/v1.0.0#input-encodings-for-deposits
// address sender, uint256 value, bytes execLayerData
const sender = slice(data.payload, 0, 20); // 20 bytes for address
const value = hexToBigInt(slice(data.payload, 20, 52)); // 32 bytes for uint256
if (isEtherDeposit(data)) {
const { sender, value } = parseEtherDeposit(data.payload);

const wallet = this.wallets[sender] ?? {};
wallet.ether += value;
this.wallets[sender] = wallet;
return "accept";
} else if (data.metadata.msg_sender === erc20PortalAddress) {
// decode input according to https://github.com/cartesi/rollups/tree/v1.0.0#input-encodings-for-deposits
// bool success, address token, address sender, uint256 amount, bytes execLayerData
const success = hexToBool(slice(data.payload, 0, 1)); // 1 byte for boolean
const token = slice(data.payload, 1, 21); // 20 bytes for address
const sender = slice(data.payload, 21, 41); // 20 bytes for address
const amount = hexToBigInt(slice(data.payload, 41, 73)); // 32 bytes for uint256
} else if (isERC20Deposit(data)) {
const { success, token, sender, amount } = parseERC20Deposit(
data.payload,
);

const wallet = this.wallets[sender] ?? {};
wallet.erc20[token] = wallet.erc20[token]
Expand Down

0 comments on commit 38202d0

Please sign in to comment.