Skip to content
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

v2: Simplify setup (no more transactionFactory / messageFactory). #2

Merged
merged 11 commits into from
May 2, 2022
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Signing provider for dApps: Maiar DeFi Wallet.

An integration sample can be found [here](examples/app.js). However, for all purposes, **we recommend using [dapp-core](https://github.com/ElrondNetwork/dapp-core)** instead of integrating the signing provider on your own.

## Distribution

[npm](https://www.npmjs.com/package/@elrondnetwork/erdjs-extension-provider)
Expand Down
88 changes: 35 additions & 53 deletions examples/app.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,53 @@
import assert from "assert";
import { ExtensionProvider } from "../out/extensionProvider";
import { TransactionFactoryLocator, SignableMessageFactoryLocator } from "../out/locators";
import { Address } from "../out/primitives";
import { DummyMessage } from "./dummyMessage";
import { DummyTransaction } from "./dummyTransaction";

export async function main() {
export async function login() {
let provider = ExtensionProvider.getInstance();
await provider.init();
let address = await provider.login();
console.log("Address:", address);

// Setup transaction factory (dependency of extension provider).
TransactionFactoryLocator.setTransactionFactory({
fromPlainObject: function(obj) {
console.log("transactionFactory.fromPlainObject()");
console.log(obj);
// In production, if using erdjs, a Transaction object could be created & returned.
return obj;
}
});
alert(`Address: ${address}`);
}

// Setup message factory (dependency of extension provider).
SignableMessageFactoryLocator.setMessageFactory({
fromPlainObject: function(obj) {
console.log("messageFactory.fromPlainObject()");
console.log(obj);
// In production, if using erdjs, a SignableMessage object could be created & returned.
return obj;
}
});
export async function signTransactions() {
let provider = ExtensionProvider.getInstance();

// Sign a transaction
let firstTransaction = await provider.signTransaction({
toPlainObject: function() {
return {
nonce: 42,
value: "1",
receiver: "erd1uv40ahysflse896x4ktnh6ecx43u7cmy9wnxnvcyp7deg299a4sq6vaywa",
gasPrice: 1000000000,
gasLimit: 50000,
data: "",
chainID: "T"
};
}
let firstTransaction = new DummyTransaction({
nonce: 42,
value: "1",
receiver: new Address("erd1uv40ahysflse896x4ktnh6ecx43u7cmy9wnxnvcyp7deg299a4sq6vaywa"),
gasPrice: 1000000000,
gasLimit: 50000,
data: "",
chainID: "T",
version: 1
});

let firstTransactionSigned = await provider.signTransaction(firstTransaction);

assert(firstTransaction === firstTransactionSigned, "The extension provider should return the same object passed as input");
console.log("First transaction, upon signing:");
console.log(firstTransaction);

// Sign & broadcast another transaction.
// This should fail (bad nonce etc.)
await provider.sendTransaction({
toPlainObject: function() {
return {
nonce: 43,
value: "1",
receiver: "erd1uv40ahysflse896x4ktnh6ecx43u7cmy9wnxnvcyp7deg299a4sq6vaywa",
gasPrice: 1000000000,
gasLimit: 200000,
data: Buffer.from("hello").toString("base64"),
chainID: "T"
};
}
});
alert(`Signature: ${firstTransaction.signature}`);
}

export async function signMessages() {
let provider = ExtensionProvider.getInstance();

// Sign a message.
let message = await provider.signMessage({
message: "hello"
let message = new DummyMessage({
message: Buffer.from("hello")
});
let messageSigned = await provider.signMessage(message);

assert(message === messageSigned, "The extension provider should return the same object passed as input");
console.log("Message, upon signing:");
console.log(message);
console.log(messageSigned);

alert(`Signature: ${message.signature}`);
}


15 changes: 15 additions & 0 deletions examples/dummyMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export class DummyMessage {
address = null;
message = Buffer.from("");
signature = null;

constructor(init) {
Object.assign(this, init);
}

applySignature(signature, signedBy) {
this.signature = signature.hex();
this.address = signedBy.bech32();
console.log("applySignature()", this.signature, this.signedBy);
}
}
37 changes: 37 additions & 0 deletions examples/dummyTransaction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export class DummyTransaction {
nonce = 0;
value = "";
receiver = null;
sender = null;
gasPrice = 0;
gasLimit = 0;
data = "";
chainID = "";
version = 0;
options = undefined;
signature = null;

constructor(init) {
Object.assign(this, init);
}

toPlainObject() {
return {
nonce: this.nonce,
value: this.value,
receiver: this.receiver.bech32(),
gasPrice: this.gasPrice,
gasLimit: this.gasLimit,
data: this.data,
chainID: this.chainID,
version: this.version,
options: this.options
};
}

applySignature(signature, signedBy) {
this.signature = signature.hex();
this.address = signedBy.bech32();
console.log("applySignature()", this.signature, this.signedBy);
}
}
16 changes: 13 additions & 3 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,23 @@

<body>
<div>
<button onclick="main()">Run example</button>
<button onclick="login()">Login</button>
<button onclick="signTransactions()">Sign transactions</button>
<button onclick="signMessages()">Sign messages</button>
</div>

<script src="../out-examples/app.js"></script>
<script>
async function main() {
await app.main();
async function login() {
await app.login();
}

async function signTransactions() {
await app.signTransactions();
}

async function signMessages() {
await app.signMessages();
}
</script>
</body>
Expand Down
Loading