Skip to content

Commit

Permalink
Merge pull request #30 from Bundlr-Network/feat/eip712-verify
Browse files Browse the repository at this point in the history
Feat: EIP712 verify
  • Loading branch information
bluekirby1111 authored May 17, 2023
2 parents 9194267 + 3b741e5 commit 873db3a
Show file tree
Hide file tree
Showing 21 changed files with 1,648 additions and 212 deletions.
204 changes: 156 additions & 48 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,20 @@ derive_builder = "0.10.2"
derive_more = "0.99.17"
ed25519-dalek = { version = "1.0.1", optional = true }
futures = "0.3.19"
indexmap = "1.9.3"
lazy_static = "1.4.0"
logos = "0.13.0"
mime_guess = "2.0.4"
num = "0.4"
num-derive = "0.3.3"
num-traits = "0.2.14"
pipe = "0.4.0"
primitive-types = "0.11.1"
rand = "0.8.5"
regex = "1.8.1"
reqwest = { version = "0.11.11", default-features = false, features = ["hyper-rustls", "tokio-rustls", "json"] }
ring = "0.16.20"
rustc-hex = "2.1.0"
secp256k1 = { version = "0.22.1", optional = true, features = [ "recovery" ] }
serde = "1.0.132"
serde_json = "1.0.73"
Expand All @@ -42,6 +46,7 @@ strum_macros = "0.24"
thiserror = "1.0.30"
tokio = { version = "1.14.0", features = [ "fs" ]}
tokio-util = "0.6.9"
validator = { version = "0.16", features = ["derive"] }
web3 = { version = "0.18.0", optional = true }

[dev-dependencies]
Expand Down
39 changes: 18 additions & 21 deletions bundles.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
globalThis.crypto = require("crypto").webcrypto;
import { AptosAccount } from "aptos";
import { bundleAndSignData, createData, DataItem } from "arbundles";
import { AlgorandSigner, AptosSigner, ArweaveSigner, Signer } from "arbundles/src/signing";
import EthereumSigner from "arbundles/src/signing/chains/ethereumSigner";
import SolanaSigner from "arbundles/src/signing/chains/SolanaSigner";
import keythereum from "keythereum";
import Bundlr from "@bundlr-network/client";
import { AlgorandSigner, AptosSigner, ArweaveSigner, bundleAndSignData, createData, DataItem, EthereumSigner, MultiSignatureAptosSigner, Signer, SolanaSigner, TypedEthereumSigner } from "arbundles";
import { Keypair } from "@solana/web3.js";
import bs58 from "bs58";
import fs from "fs";
import fs from "fs/promises";
import crypto from "crypto";
import Bundlr from "@bundlr-network/client";
import { Wallet } from "ethers/wallet";
import Arweave from "arweave";

const MAX_BUNDLES_AMOUNT = 100;
const MAX_DATA_ITEMS = 100;
const MAX_DATA_BYTES = 1000;
const MAX_APTOS_SIGNERS = 20;

//Arweave
import jwk from "./res/test_wallet.json";
const jwk = await Arweave.init({}).wallets.generate();

//Ethereum
var params = { keyBytes: 32, ivBytes: 16 };
var { privateKey } = keythereum.create(params);
var { privateKey } = Wallet.createRandom();

//Solana
const solKeypair = Keypair.generate();
Expand All @@ -38,7 +34,7 @@ const wallet = {

// create signature collection function
// this function is called whenever the client needs to collect signatures for signing
const collectSignatures = async (message: Buffer) => {
const collectSignatures = async (message: Uint8Array) => {
//Select random amount of random acccounts within our aptos accounts
const accountAmount = Math.ceil(Math.random() * aptosAccounts.length);
const randomAccounts = aptosAccounts
Expand All @@ -47,29 +43,31 @@ const collectSignatures = async (message: Buffer) => {
.slice(0, accountAmount); // Get sample size
const signatures = randomAccounts.map(el => Buffer.from(el.account.signBuffer(message).toUint8Array()));
const bitmap = randomAccounts.map(el => el.i);
return { signatures: signatures, bitmap };
return { signatures, bitmap };
}

const bundlesAmount = MAX_BUNDLES_AMOUNT;

//Create all signers
//TODO: figure out how to instantiate signer directly (see below)
const multiAptosSigner = new Bundlr(
const bundlr = new Bundlr.default(
"https://devnet.bundlr.network",
"multiAptos",
wallet,
{ providerUrl: "https://fullnode.devnet.aptoslabs.com", currencyOpts: { collectSignatures } }
).getSigner();
);
await bundlr.ready();
let multiAptosSigner = bundlr.getSigner();

const signers: Signer[] = [
new ArweaveSigner(jwk),
new AlgorandSigner(algoKeypair.secretKey, algoKeypair.publicKey.toBuffer()),
// @ts-ignore
new EthereumSigner(privateKey),
new TypedEthereumSigner(privateKey),
new SolanaSigner(bs58.encode(solKeypair.secretKey)),
new AptosSigner(aptosAccounts[0].toPrivateKeyObject().privateKeyHex, aptosAccounts[0].toPrivateKeyObject().publicKeyHex),
//new MultiSignatureAptosSigner(Buffer.from(wallet.participants.join("")), collectSignatures)
multiAptosSigner
//multiAptosSigner //TODO: fix signer
];

for (let i = 1; i <= bundlesAmount; i++) {
Expand All @@ -80,7 +78,7 @@ for (let i = 1; i <= bundlesAmount; i++) {
const randomData = crypto.randomBytes(MAX_DATA_BYTES).toString('hex');
try {
const data = createData(randomData, signer);
data.sign(signer).then(() => {
await data.sign(signer).then(() => {
if (data.isSigned()) {
dataItems.push(data);
} else {
Expand All @@ -96,9 +94,8 @@ for (let i = 1; i <= bundlesAmount; i++) {

const finalSigner = signers[Math.floor(Math.random() * signers.length)];
bundleAndSignData(dataItems, finalSigner).then((bundle) => {
bundle.verify().then(ok => {
fs.writeFile(`res/gen_bundles/bundle_${i}`, bundle.getRaw(),
() => console.info(`Generated bundle ${i} in res/gen_bundles/bundle_${i}`));
bundle.verify().then(async ok => {
await fs.writeFile(`res/gen_bundles/bundle_${i}`, bundle.getRaw()).then(() => console.info(`Generated bundle ${i} with ${bundle.getIds().length} dataItems in res/gen_bundles/bundle_${i}`));
}).catch(err => {
console.log(`Invalid bundle: ${err}`)
});
Expand Down
2 changes: 1 addition & 1 deletion examples/verify_receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ fn main() -> Result<(), bundlr_sdk::error::BundlrError> {
.decode(&receipt.signature.into_bytes())
.unwrap();

ArweaveSigner::verify(pubk.into(), msg.into(), sig.into())
ArweaveSigner::verify(pubk.into(), msg, sig.into())
}
Loading

0 comments on commit 873db3a

Please sign in to comment.