-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add entrypoints #538
Merged
+350
−5
Merged
Add entrypoints #538
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
export interface EntrypointConfig { | ||
networkProviderUrl: string; | ||
networkProviderKind: string; | ||
chainId: string; | ||
} | ||
|
||
export class TestnetEntrypointConfig { | ||
networkProviderUrl: string; | ||
networkProviderKind: string; | ||
chainId: string; | ||
|
||
constructor({ | ||
networkProviderUrl = "https://testnet-api.multiversx.com", | ||
networkProviderKind = "api", | ||
chainId = "T", | ||
}: Partial<EntrypointConfig> = {}) { | ||
this.networkProviderUrl = networkProviderUrl; | ||
this.networkProviderKind = networkProviderKind; | ||
this.chainId = chainId; | ||
} | ||
} | ||
|
||
export class DevnetEntrypointConfig { | ||
networkProviderUrl: string; | ||
networkProviderKind: string; | ||
chainId: string; | ||
constructor({ | ||
networkProviderUrl = "https://devnet-api.multiversx.com", | ||
networkProviderKind = "api", | ||
chainId = "D", | ||
}: Partial<EntrypointConfig> = {}) { | ||
this.networkProviderUrl = networkProviderUrl; | ||
this.networkProviderKind = networkProviderKind; | ||
this.chainId = chainId; | ||
} | ||
} | ||
|
||
export class MainnetEntrypointConfig { | ||
networkProviderUrl: string; | ||
networkProviderKind: string; | ||
chainId: string; | ||
|
||
constructor({ | ||
networkProviderUrl = "https://api.multiversx.com", | ||
networkProviderKind = "api", | ||
chainId = "1", | ||
}: Partial<EntrypointConfig> = {}) { | ||
this.networkProviderUrl = networkProviderUrl; | ||
this.networkProviderKind = networkProviderKind; | ||
this.chainId = chainId; | ||
} | ||
} | ||
|
||
export class LocalnetEntrypointConfig { | ||
networkProviderUrl: string; | ||
networkProviderKind: string; | ||
chainId: string; | ||
|
||
constructor({ | ||
networkProviderUrl = "http://localhost:7950", | ||
networkProviderKind = "proxy", | ||
chainId = "localnet", | ||
}: Partial<EntrypointConfig> = {}) { | ||
this.networkProviderUrl = networkProviderUrl; | ||
this.networkProviderKind = networkProviderKind; | ||
this.chainId = chainId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { assert } from "chai"; | ||
import { readFileSync } from "fs"; | ||
import { Account } from "../accounts/account"; | ||
import { Address } from "../address"; | ||
import { loadAbiRegistry, loadTestWallet, TestWallet } from "../testutils"; | ||
import { TransactionComputer } from "../transactionComputer"; | ||
import { DevnetEntrypoint } from "./entrypoints"; | ||
|
||
describe("TestEntrypoint", () => { | ||
const entrypoint = new DevnetEntrypoint(); | ||
let alicePem: TestWallet; | ||
let bobPem: TestWallet; | ||
let txComputer: TransactionComputer; | ||
|
||
before(async function () { | ||
alicePem = await loadTestWallet("alice"); | ||
bobPem = await loadTestWallet("bob"); | ||
txComputer = new TransactionComputer(); | ||
}); | ||
|
||
it("native transfer", async () => { | ||
const controller = entrypoint.createTransfersController(); | ||
const sender = Account.newFromPem(alicePem.pemFileText); | ||
sender.nonce = 77777; | ||
|
||
const transaction = await controller.createTransactionForTransfer( | ||
sender, | ||
BigInt(sender.getNonceThenIncrement().valueOf()), | ||
{ | ||
receiver: sender.address, | ||
nativeAmount: BigInt(0), | ||
data: Buffer.from("hello"), | ||
}, | ||
); | ||
assert.equal( | ||
Buffer.from(transaction.signature).toString("hex"), | ||
"69bc7d1777edd0a901e6cf94830475716205c5efdf2fd44d4be31badead59fc8418b34f0aa3b2c80ba14aed5edd30031757d826af58a1abb690a0bee89ba9309", | ||
); | ||
}); | ||
|
||
it("contract flow", async function () { | ||
this.timeout(30000); | ||
const abi = await loadAbiRegistry("src/testdata/adder.abi.json"); | ||
const sender = Account.newFromPem(alicePem.pemFileText); | ||
const accountAddress = new Address(sender.address); | ||
sender.nonce = await entrypoint.recallAccountNonce(accountAddress); | ||
|
||
const controller = entrypoint.createSmartContractController(abi); | ||
const bytecode = readFileSync("src/testdata/adder.wasm"); | ||
|
||
const transaction = await controller.createTransactionForDeploy( | ||
sender, | ||
BigInt(sender.getNonceThenIncrement().valueOf()), | ||
{ | ||
bytecode, | ||
gasLimit: BigInt(10_000_000), | ||
arguments: [0], | ||
}, | ||
); | ||
|
||
const txHash = await entrypoint.sendTransaction(transaction); | ||
const outcome = await controller.awaitCompletedDeploy(txHash); | ||
|
||
assert.equal(outcome.contracts.length, 1); | ||
|
||
const contractAddress = Address.fromBech32(outcome.contracts[0].address); | ||
|
||
const executeTransaction = await controller.createTransactionForExecute( | ||
sender, | ||
BigInt(sender.getNonceThenIncrement().valueOf()), | ||
{ | ||
contract: contractAddress, | ||
gasLimit: BigInt(10_000_000), | ||
function: "add", | ||
arguments: [7], | ||
}, | ||
); | ||
|
||
const txHashExecute = await entrypoint.sendTransaction(executeTransaction); | ||
await entrypoint.awaitCompletedTransaction(txHashExecute); | ||
|
||
const queryResult = await controller.queryContract(contractAddress, "getSum", []); | ||
assert.equal(queryResult.length, 1); | ||
assert.equal(queryResult[0], 7); | ||
}); | ||
|
||
it("create relayed transaction", async function () { | ||
const transferController = entrypoint.createTransfersController(); | ||
const sender = Account.newFromPem(alicePem.pemFileText); | ||
sender.nonce = 77777; | ||
|
||
const relayer = Account.newFromPem(bobPem.pemFileText); | ||
relayer.nonce = 7; | ||
|
||
const transaction = await transferController.createTransactionForTransfer( | ||
sender, | ||
BigInt(sender.getNonceThenIncrement().valueOf()), | ||
{ | ||
receiver: sender.address, | ||
data: Buffer.from("hello"), | ||
}, | ||
); | ||
const innerTransactionGasLimit = transaction.gasLimit; | ||
transaction.gasLimit = BigInt(0); | ||
transaction.signature = await sender.sign(txComputer.computeBytesForSigning(transaction)); | ||
|
||
const relayedController = entrypoint.createRelayedController(); | ||
const relayedTransaction = await relayedController.createRelayedV2Transaction( | ||
relayer, | ||
BigInt(relayer.getNonceThenIncrement().valueOf()), | ||
{ | ||
innerTransaction: transaction, | ||
innerTransactionGasLimit, | ||
}, | ||
); | ||
assert.equal(relayedTransaction.chainID, "D"); | ||
assert.deepEqual( | ||
relayedTransaction.data, | ||
Buffer.from( | ||
"relayedTxV2@0139472eff6886771a982f3083da5d421f24c29181e63888228dc81ca60d69e1@012fd1@68656c6c6f@c1eed3ac766d6b94aa53a1348d38eac8db60be0a1b2d0873247b61b8b25bbcb45bf9c1518227bcadd5044d4c027bdb935e0164243b2b2df9a5b250a10aca260e", | ||
), | ||
); | ||
assert.equal(relayedTransaction.gasLimit, 442000n); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import { AbiRegistry } from "../abi"; | ||
import { AccountController } from "../accountManagement"; | ||
import { IAccount } from "../accounts/interfaces"; | ||
import { Address } from "../address"; | ||
import { DelegationController } from "../delegation"; | ||
import { ErrInvalidNetworkProviderKind } from "../errors"; | ||
import { Message, MessageComputer } from "../message"; | ||
import { ApiNetworkProvider, ProxyNetworkProvider, TransactionOnNetwork } from "../networkProviders"; | ||
import { RelayedController } from "../relayed/relayedController"; | ||
import { SmartContractController } from "../smartContracts/smartContractController"; | ||
import { TokenManagementController } from "../tokenManagement"; | ||
import { Transaction } from "../transaction"; | ||
import { TransactionComputer } from "../transactionComputer"; | ||
import { TransactionWatcher } from "../transactionWatcher"; | ||
import { TransfersController } from "../transfers/transfersControllers"; | ||
import { UserVerifier } from "../wallet"; | ||
import { DevnetEntrypointConfig, MainnetEntrypointConfig, TestnetEntrypointConfig } from "./config"; | ||
|
||
class NetworkEntrypoint { | ||
private networkProvider: ApiNetworkProvider | ProxyNetworkProvider; | ||
private chainId: string; | ||
|
||
constructor(options: { networkProviderUrl: string; networkProviderKind: string; chainId: string }) { | ||
if (options.networkProviderKind === "proxy") { | ||
this.networkProvider = new ProxyNetworkProvider(options.networkProviderUrl); | ||
} else if (options.networkProviderKind === "api") { | ||
this.networkProvider = new ApiNetworkProvider(options.networkProviderUrl); | ||
} else { | ||
throw new ErrInvalidNetworkProviderKind(); | ||
} | ||
|
||
this.chainId = options.chainId; | ||
} | ||
|
||
async signTransaction(transaction: Transaction, account: IAccount): Promise<void> { | ||
const txComputer = new TransactionComputer(); | ||
transaction.signature = await account.sign(txComputer.computeBytesForSigning(transaction)); | ||
} | ||
|
||
verifyTransactionSignature(transaction: Transaction): boolean { | ||
const verifier = UserVerifier.fromAddress(Address.fromBech32(transaction.sender)); | ||
const txComputer = new TransactionComputer(); | ||
return verifier.verify(txComputer.computeBytesForVerifying(transaction), transaction.signature); | ||
} | ||
|
||
async signMessage(message: Message, account: IAccount): Promise<void> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think they will be soon moved to account: multiversx/mx-sdk-specs#94 |
||
const messageComputer = new MessageComputer(); | ||
message.signature = await account.sign(messageComputer.computeBytesForSigning(message)); | ||
} | ||
|
||
verifyMessageSignature(message: Message): boolean { | ||
if (!message.address) { | ||
throw new Error("`address` property of Message is not set"); | ||
} | ||
|
||
if (!message.signature) { | ||
throw new Error("`signature` property of Message is not set"); | ||
} | ||
|
||
const verifier = UserVerifier.fromAddress(message.address); | ||
const messageComputer = new MessageComputer(); | ||
return verifier.verify(messageComputer.computeBytesForVerifying(message), message.signature); | ||
} | ||
|
||
async recallAccountNonce(address: Address): Promise<number> { | ||
return (await this.networkProvider.getAccount(address)).nonce; | ||
} | ||
|
||
sendTransactions(transactions: Transaction[]): Promise<string[]> { | ||
return this.networkProvider.sendTransactions(transactions); | ||
} | ||
|
||
sendTransaction(transaction: Transaction): Promise<string> { | ||
return this.networkProvider.sendTransaction(transaction); | ||
} | ||
|
||
async awaitCompletedTransaction(txHash: string): Promise<TransactionOnNetwork> { | ||
const transactionAwaiter = new TransactionWatcher(this.networkProvider); | ||
return transactionAwaiter.awaitCompleted(txHash); | ||
} | ||
|
||
createNetworkProvider(): ApiNetworkProvider | ProxyNetworkProvider { | ||
return this.networkProvider; | ||
} | ||
|
||
createDelegationController(): DelegationController { | ||
return new DelegationController({ chainID: this.chainId, networkProvider: this.networkProvider }); | ||
} | ||
|
||
createAccountController(): AccountController { | ||
return new AccountController({ chainID: this.chainId }); | ||
} | ||
|
||
createRelayedController(): RelayedController { | ||
return new RelayedController({ chainID: this.chainId }); | ||
} | ||
|
||
createSmartContractController(abi?: AbiRegistry): SmartContractController { | ||
return new SmartContractController({ chainID: this.chainId, networkProvider: this.networkProvider, abi }); | ||
} | ||
|
||
createTokenManagementController(): TokenManagementController { | ||
return new TokenManagementController({ chainID: this.chainId, networkProvider: this.networkProvider }); | ||
} | ||
|
||
createTransfersController(): TransfersController { | ||
return new TransfersController({ chainID: this.chainId }); | ||
} | ||
} | ||
|
||
export class TestnetEntrypoint extends NetworkEntrypoint { | ||
constructor(url?: string, kind?: string) { | ||
const entrypointConfig = new TestnetEntrypointConfig(); | ||
super({ | ||
networkProviderUrl: url || entrypointConfig.networkProviderUrl, | ||
networkProviderKind: kind || entrypointConfig.networkProviderKind, | ||
chainId: entrypointConfig.chainId, | ||
}); | ||
} | ||
} | ||
|
||
export class DevnetEntrypoint extends NetworkEntrypoint { | ||
constructor(url?: string, kind?: string) { | ||
const entrypointConfig = new DevnetEntrypointConfig(); | ||
super({ | ||
networkProviderUrl: url || entrypointConfig.networkProviderUrl, | ||
networkProviderKind: kind || entrypointConfig.networkProviderKind, | ||
chainId: entrypointConfig.chainId, | ||
}); | ||
} | ||
} | ||
|
||
export class MainnetEntrypoint extends NetworkEntrypoint { | ||
constructor(url?: string, kind?: string) { | ||
const entrypointConfig = new MainnetEntrypointConfig(); | ||
super({ | ||
networkProviderUrl: url || entrypointConfig.networkProviderUrl, | ||
networkProviderKind: kind || entrypointConfig.networkProviderKind, | ||
chainId: entrypointConfig.chainId, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./config"; | ||
export * from "./entrypoints"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should switch to
bigint
for the nonce instead ofINonce
.valueOf
also returns anumber
which can lead to errors in the future. can be done in the next PR.