1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00

add get public key method to account

This commit is contained in:
Sebastian Gerske 2018-11-12 10:06:24 +01:00
parent c425f03efd
commit 86d1621046
3 changed files with 23 additions and 2 deletions

View File

@ -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<number> {
await (await Keeper.getInstance()).market.requestTokens(amount, this.id)
return amount
}
public async getPublicKey(): Promise<string> {
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")
}
}

View File

@ -112,7 +112,7 @@ export default class Ocean {
owner: did,
},
{
publicKeyBase58: publisher.getId(),
publicKeyBase58: publisher.getPublicKey(),
},
],
service: [

View File

@ -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)
})
})
})