Skip to content

Commit

Permalink
Feat: Added sign-message functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rkmonarch committed Oct 31, 2023
1 parent 21bb8ec commit e9aed9b
Show file tree
Hide file tree
Showing 9 changed files with 1,878 additions and 15 deletions.
1,819 changes: 1,805 additions & 14 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,11 @@
"dependencies": {
"axios": "^1.5.1",
"zod": "^3.22.4"
},
"peerDependencies": {
"ethers": "^5.5.0",
"aptos": "^1.20.0",
"@solana/web3.js": "^1.87.3",
"tweetnacl": "^1.0.3"
}
}
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Addressbook from "./address-book";
import { Headers, headersSchema } from "./types";
import Identity from "./identity";
import Auth from "./auth";
import Sign from "./utils/sign/adapters";

export default class Fetcch {
secretKey: string;
Expand All @@ -12,7 +13,7 @@ export default class Fetcch {
payments: Payments;
addressBook: Addressbook;
auth: Auth;

sign: Sign;
private headers: Headers

constructor(secretKey: string) {
Expand All @@ -29,5 +30,6 @@ export default class Fetcch {
this.request = new Request(this.headers)
this.identity = new Identity(this.headers)
this.auth = new Auth(this.headers)
this.sign = new Sign()
}
}
6 changes: 6 additions & 0 deletions src/utils/sign/adapters/aptos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// import { signMessage } from 'aptos';

// export const _signAptosMessage = async (message: string, privateKey: string): Promise<string> => {
// const signature = await aptos.sign(message, privateKey);
// return signature;
// }
7 changes: 7 additions & 0 deletions src/utils/sign/adapters/eth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ethers } from "ethers";

export const _signEthereumMessage = async (message: string, privateKey: string): Promise<string> => {
const wallet = new ethers.Wallet(privateKey);
const signature = await wallet.signMessage(message);
return signature;
}
15 changes: 15 additions & 0 deletions src/utils/sign/adapters/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { _signEthereumMessage } from "./eth";
import { _signSolanaMessage } from "./solana";


export default class Sign {

signEthereurmMessage(message: string, privateKey: string) {
return _signEthereumMessage(message, privateKey);
}

signSolanaMessage(message: string, privateKey: string) {
return _signSolanaMessage(message, privateKey);
}

}
10 changes: 10 additions & 0 deletions src/utils/sign/adapters/solana.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import nacl from "tweetnacl";

export const _signSolanaMessage = async (message: string, privateKey: string) => {
const messageBytes = new TextEncoder().encode(message);

const signature = nacl.sign.detached(messageBytes, new TextEncoder().encode(privateKey));
return signature;
};


13 changes: 13 additions & 0 deletions tests/sign/sign-ethereum-message.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Fetcch from "../../src";

describe("module: sign-ethereum-message", () => {
const fetcch = new Fetcch("4ff9ecc8-4537-4e2e-950d-0cefbd16f2a5");
test("sign ethereum message", async () => {
const signature = await fetcch.sign.signEthereurmMessage(
"fujh@pay",
"c36c3587ae76d32737cb441334c5ac858d0fc906b329ba9fc6998e090625b60d",
);
console.log("signaturee", signature);
expect(signature).toBeDefined();
});
});
13 changes: 13 additions & 0 deletions tests/sign/sign-solana-message.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Fetcch from "../../src";

describe("module: sign-solana-message", () => {
const fetcch = new Fetcch("4ff9ecc8-4537-4e2e-950d-0cefbd16f2a5");
test("sign solana message", async () => {
const signature = await fetcch.sign.signSolanaMessage(
"fujh@pay",
"c36c3587ae76d32737cb441334c5ac858d0fc906b329ba9fc6998e090625b60d",
);
console.log("signature solana", signature);
expect(signature).toBeDefined();
});
});

0 comments on commit e9aed9b

Please sign in to comment.