Skip to content
This repository was archived by the owner on Dec 15, 2024. It is now read-only.

Commit 186d537

Browse files
committed
[gopherjs-libra] update README and publish. Add pubkeyToAuthKey
1 parent eb5dfdf commit 186d537

File tree

6 files changed

+36
-20
lines changed

6 files changed

+36
-20
lines changed

client/account.go

+10
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,13 @@ func PubkeyMustToAddress(pubkey []byte) (out types.AccountAddress) {
3434
copy(out[:], keyHash[hasher.Size()-types.AccountAddressLength:])
3535
return
3636
}
37+
38+
// PubkeyMustToAuthKey converts an ed25519 public key (32 bytes) into auth key (32 bytes).
39+
func PubkeyMustToAuthKey(pubkey []byte) []byte {
40+
if len(pubkey) != ed25519.PublicKeySize {
41+
panic("wrong pubkey length")
42+
}
43+
hasher := sha3.New256()
44+
hasher.Write(pubkey)
45+
return hasher.Sum([]byte{})
46+
}

gopherjs-libra/README.md

+20-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
A Libra js client library with crypto verifications, for NodeJS and browsers.
22

3+
Compatible with libra testnet updated on 4/8/2020.
4+
35
# Usage
46

57
In order to work with browsers, `gopherjs-libra` uses gRPC-Web which is not directly compatible with gRPC. A proxy is needed to forward gRPC-Web requests to gRPC backends. You can setup an Envoy proxy (https://grpc.io/docs/tutorials/basic/web/), or use my demo proxy shown in the examples.
@@ -62,7 +64,10 @@ const { libra } = require("gopherjs-libra");
6264

6365
keyPair = sign.keyPair();
6466
address = libra.pubkeyToAddress(keyPair.publicKey);
65-
// address is a libra account address (Uint8Array, 32-byte)
67+
authKey = libra.pubkeyToAuthKey(keyPair.publicKey);
68+
// address is a libra account address (Uint8Array, 16-byte)
69+
// authKey is the full auth key (Uint8Array, 32-byte)
70+
// Use authkey[0:16] if you need authKeyPrefix
6671
// keyPair.secretKey is the corresponding private key (Uint8Array, 64-byte)
6772
```
6873

@@ -120,6 +125,10 @@ Arguments:
120125

121126
Returns a `Uint8Array`: the raw path to the Libra account resource, which is `0x01+hash(0x0.LibraAccount.T)`.
122127

128+
### `.balanceResourcePath()`
129+
130+
Returns a `Uint8Array`: the raw path to the Libra balance resource, which is `0x01+hash(0x0.LibraAccount.Balance)`.
131+
123132
### `.accountSentEventPath()`
124133

125134
Returns a `Uint8Array`: the raw path to Libra coin sent events, which is `0x01+hash(0x0.LibraAccount.T)/sent_events_count/`.
@@ -133,7 +142,14 @@ Returns a `Uint8Array`: the raw path to Libra coin received events, which is `0x
133142
Arguments:
134143
- publicKey (Uint8Array): 32-byte ed25519 public key.
135144

136-
Returns SHA3 hash of input public key, which is used as Libra account address.
145+
Returns last 16 bytes of SHA3 hash of input public key, which is used as Libra account address.
146+
147+
### `.pubkeyToAuthKey(publicKey)`
148+
149+
Arguments:
150+
- publicKey (Uint8Array): 32-byte ed25519 public key.
151+
152+
Returns the full SHA3 hash of input public key, which is used as initial Libra account auth key.
137153

138154
## Object: `Client`
139155

@@ -176,6 +192,7 @@ Arguments:
176192
- `p2pTxn` (Object): a p2p transaction object, with following keys
177193
- `senderAddr` (Uint8Array): sender address
178194
- `recvAddr` (Uint8Array): receiver address
195+
- `recvAuthKeyPrefix` (Uint8Array): receiver auth key prefix (first 16 bytes of auth key)
179196
- `senderPrivateKey` (Uint8Array): sender ed25519 secret key (64 bytes)
180197
- `senderSeq` (integer): current sender account sequence number
181198
- `amountMicro` (integer): amount to transfer in micro libra
@@ -300,20 +317,7 @@ It has a list of getters (with return type), whose names are self-descriptive:
300317
- `.getLedgerInfo()` (`provenLedgerInfo`)
301318
- `.getAddress()` (Uint8Array)
302319
- `.getResource(path)` (Uint8Array): Returns a binary resource content on the given access path. Use `resourcePath()` to build a path.
303-
- `.getLibraAccountResource()` (`provenAccountResource`): Returns the Libra account resource.
304-
305-
## Object: `provenAccountResource`
306-
307-
Represents the libra account resource (0x0.LibraAccount.T) of an account, proven to be at a certain version.
308-
309-
It has a list of getters (with return type), whose names are self-descriptive:
310-
- `.getLedgerInfo()` (`provenLedgerInfo`)
311-
- `.getAddress()` (Uint8Array)
312-
- `.getBalance()` (integer)
313-
- `.getSequenceNumber()` (integer)
314-
- `.getSentEvents()` (object)
315-
- `.getReceivedEvents()` (object)
316-
- `.getDelegatedWithdrawalCapability()` (bool)
320+
- `.getLibraResources()` (object): Returns the Libra account resources, i.e. `accountResource` and `balanceResource`.
317321

318322
## Object: `provenTransaction`
319323

gopherjs-libra/gopherjs-libra.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gopherjs-libra/gopherjs-libra.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gopherjs-libra/main.go

+2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ func main() {
2424
"client": newClient,
2525
"resourcePath": types.ResourcePath,
2626
"accountResourcePath": types.AccountResourcePath,
27+
"balanceResourcePath": types.BalanceResourcePath,
2728
"accountSentEventPath": types.AccountSentEventPath,
2829
"accountReceivedEventPath": types.AccountReceivedEventPath,
2930
"pubkeyToAddress": client.PubkeyMustToAddress,
31+
"pubkeyToAuthKey": client.PubkeyMustToAuthKey,
3032
"inferProgramName": stdscript.InferProgramName,
3133
})
3234
jsTypeOf = js.Global.Call("eval", `(function(x){return typeof(x);})`)

gopherjs-libra/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gopherjs-libra",
3-
"version": "0.1.12",
3+
"version": "0.1.13",
44
"description": "Libra js client library with crypto verifications",
55
"homepage": "https://github.com/the729/go-libra/tree/master/gopherjs-libra",
66
"main": "gopherjs-libra.js",

0 commit comments

Comments
 (0)