|
| 1 | +require("dotenv").config(); |
| 2 | +const { ethers } = require("ethers"); |
| 3 | +const lighthouse = require('@lighthouse-web3/sdk'); |
| 4 | +const { ChainId } = require("@biconomy/core-types"); |
| 5 | +const HDWalletProvider = require("@truffle/hdwallet-provider"); |
| 6 | +const SmartAccount = require("@biconomy/smart-account").default; |
| 7 | + |
| 8 | +const sign_auth_message = async(publicKey, privateKey) =>{ |
| 9 | + const provider = new ethers.providers.JsonRpcProvider(); |
| 10 | + const signer = new ethers.Wallet(privateKey, provider); |
| 11 | + const messageRequested = (await lighthouse.getAuthMessage(publicKey)).data.message; |
| 12 | + const signedMessage = await signer.signMessage(messageRequested); |
| 13 | + return(signedMessage) |
| 14 | +} |
| 15 | + |
| 16 | +const deployEncrypted = async() =>{ |
| 17 | + const path = ""; //Give absolute path |
| 18 | + const apiKey = ""; |
| 19 | + const publicKey = ""; |
| 20 | + const privateKey = ""; |
| 21 | + const signed_message = await sign_auth_message(publicKey, privateKey); |
| 22 | + |
| 23 | + const response = await lighthouse.uploadEncrypted( |
| 24 | + path, |
| 25 | + apiKey, |
| 26 | + publicKey, |
| 27 | + signed_message |
| 28 | + ); |
| 29 | + return response.data.Hash |
| 30 | +} |
| 31 | + |
| 32 | +const pkey = ""; |
| 33 | +const polygonRPC = "https://rpc-mumbai.maticvigil.com/"; |
| 34 | + |
| 35 | +const main = async () => { |
| 36 | + const Model_Hash = await deployEncrypted(); |
| 37 | + console.log(Model_Hash) |
| 38 | + let provider = new HDWalletProvider(pkey, polygonRPC); |
| 39 | + const walletProvider = new ethers.providers.Web3Provider(provider); |
| 40 | + let options = { |
| 41 | + activeNetworkId: ChainId.POLYGON_MUMBAI, |
| 42 | + supportedNetworksIds: [ChainId.POLYGON_MUMBAI], |
| 43 | + networkConfig: [ |
| 44 | + { |
| 45 | + chainId: ChainId.POLYGON_MUMBAI, |
| 46 | + dappAPIKey: "", |
| 47 | + }, |
| 48 | + ], |
| 49 | + }; |
| 50 | + |
| 51 | + let smartAccount = new SmartAccount(walletProvider, options); |
| 52 | + smartAccount = await smartAccount.init(); |
| 53 | + |
| 54 | + // Transaction subscription |
| 55 | + |
| 56 | + smartAccount.on("txHashGenerated", (response) => { |
| 57 | + console.log("txHashGenerated event received via emitter", response); |
| 58 | + }); |
| 59 | + |
| 60 | + smartAccount.on("txMined", (response) => { |
| 61 | + console.log("txMined event received via emitter", response); |
| 62 | + }); |
| 63 | + |
| 64 | + smartAccount.on("error", (response) => { |
| 65 | + console.log("error event received via emitter", response); |
| 66 | + }); |
| 67 | + |
| 68 | + const fileInterface = new ethers.utils.Interface([ |
| 69 | + { |
| 70 | + inputs: [ |
| 71 | + { |
| 72 | + internalType: "address payable", |
| 73 | + name: "viewerNode", |
| 74 | + type: "address", |
| 75 | + }, |
| 76 | + ], |
| 77 | + name: "fetchModel", |
| 78 | + outputs: [ |
| 79 | + { |
| 80 | + internalType: "string", |
| 81 | + name: "", |
| 82 | + type: "string", |
| 83 | + }, |
| 84 | + ], |
| 85 | + stateMutability: "view", |
| 86 | + type: "function", |
| 87 | + }, |
| 88 | + { |
| 89 | + inputs: [], |
| 90 | + name: "modelCount", |
| 91 | + outputs: [ |
| 92 | + { |
| 93 | + internalType: "uint256", |
| 94 | + name: "", |
| 95 | + type: "uint256", |
| 96 | + }, |
| 97 | + ], |
| 98 | + stateMutability: "view", |
| 99 | + type: "function", |
| 100 | + }, |
| 101 | + { |
| 102 | + inputs: [ |
| 103 | + { |
| 104 | + internalType: "address", |
| 105 | + name: "", |
| 106 | + type: "address", |
| 107 | + }, |
| 108 | + ], |
| 109 | + name: "models", |
| 110 | + outputs: [ |
| 111 | + { |
| 112 | + internalType: "string", |
| 113 | + name: "modelHash", |
| 114 | + type: "string", |
| 115 | + }, |
| 116 | + { |
| 117 | + internalType: "uint256", |
| 118 | + name: "fileSize", |
| 119 | + type: "uint256", |
| 120 | + }, |
| 121 | + { |
| 122 | + internalType: "address payable", |
| 123 | + name: "node", |
| 124 | + type: "address", |
| 125 | + }, |
| 126 | + ], |
| 127 | + stateMutability: "view", |
| 128 | + type: "function", |
| 129 | + }, |
| 130 | + { |
| 131 | + inputs: [ |
| 132 | + { |
| 133 | + internalType: "string", |
| 134 | + name: "_modelHash", |
| 135 | + type: "string", |
| 136 | + }, |
| 137 | + { |
| 138 | + internalType: "uint256", |
| 139 | + name: "_modelSize", |
| 140 | + type: "uint256", |
| 141 | + }, |
| 142 | + { |
| 143 | + internalType: "address payable", |
| 144 | + name: "callerNode", |
| 145 | + type: "address", |
| 146 | + }, |
| 147 | + ], |
| 148 | + name: "uploadModel", |
| 149 | + outputs: [], |
| 150 | + stateMutability: "nonpayable", |
| 151 | + type: "function", |
| 152 | + }, |
| 153 | + ]); |
| 154 | + |
| 155 | + console.log(Model_Hash) |
| 156 | + |
| 157 | + const data = fileInterface.encodeFunctionData("uploadModel", [ |
| 158 | + Model_Hash, |
| 159 | + 1000, |
| 160 | + "", |
| 161 | + ]); |
| 162 | + |
| 163 | + const tx1 = { |
| 164 | + to: "", |
| 165 | + data: data, |
| 166 | + // value can also be added for example ethers.utils.parseEther("1") |
| 167 | + }; |
| 168 | + |
| 169 | + // Gasless |
| 170 | + const txResponse = await smartAccount.sendGasLessTransaction({ |
| 171 | + transaction: tx1, |
| 172 | + }); |
| 173 | +}; |
| 174 | +main(); |
0 commit comments