diff --git a/src/ocean/Account.ts b/src/ocean/Account.ts index 5339403..ccafbf7 100644 --- a/src/ocean/Account.ts +++ b/src/ocean/Account.ts @@ -1,4 +1,5 @@ import BigNumber from "bignumber.js" +import * as EthJsUtils from "ethereumjs-util" import Keeper from "../keeper/Keeper" import Web3Provider from "../keeper/Web3Provider" import Balance from "../models/Balance" @@ -33,9 +34,19 @@ export default class Account extends OceanBase { return this.balance } - // Transactions with gas cost public async requestTokens(amount: number): Promise { await (await Keeper.getInstance()).market.requestTokens(amount, this.id) return amount } + + public async getPublicKey(): Promise { + + const web3 = Web3Provider.getWeb3() + + const msg = web3.utils.sha3(this.getId()) + const sig = await web3.eth.sign(msg, this.getId()) + const {v, r, s} = EthJsUtils.fromRpcSig(sig) + + return EthJsUtils.ecrecover(EthJsUtils.toBuffer(msg), v, r, s).toString("hex") + } } diff --git a/src/ocean/Ocean.ts b/src/ocean/Ocean.ts index 7f3afb7..0ad0e7a 100644 --- a/src/ocean/Ocean.ts +++ b/src/ocean/Ocean.ts @@ -112,7 +112,7 @@ export default class Ocean { owner: did, }, { - publicKeyBase58: publisher.getId(), + publicKeyBase58: publisher.getPublicKey(), }, ], service: [ diff --git a/test/ocean/Account.test.ts b/test/ocean/Account.test.ts index 17306df..209d76a 100644 --- a/test/ocean/Account.test.ts +++ b/test/ocean/Account.test.ts @@ -75,4 +75,14 @@ describe("Account", () => { assert(tokensGranted === tokens) }) }) + + describe("#getPublicKey()", () => { + + it("should get the public key of an account", async () => { + const publicKey = await accounts[1].getPublicKey() + assert(publicKey) + + assert(publicKey.length === 128) + }) + }) })