-
Notifications
You must be signed in to change notification settings - Fork 458
/
utils.go
44 lines (33 loc) · 1.11 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package sectorbuilder
import (
"bytes"
"encoding/binary"
"github.com/filecoin-project/go-filecoin/address"
)
// AddressToProverID creates a prover id by padding an address hash to 31 bytes
func AddressToProverID(addr address.Address) [31]byte {
// this code will no longer WAE when ID's or BLS pub keys are added
// as they will break the assumption of addresses all being the same length
if addr.Protocol() != address.Actor && addr.Protocol() != address.SECP256K1 {
panic("cannot create prover hash from new address protocol")
}
hash := addr.Payload()
dlen := 31 // desired length
hlen := len(hash) // hash length
padl := dlen - hlen // padding length
var prid [31]byte
// will copy dlen bytes from hash
copy(prid[:], hash)
if padl > 0 {
copy(prid[hlen:], bytes.Repeat([]byte{0}, padl))
}
return prid
}
// SectorIDToBytes creates a prover id by padding an address hash to 31 bytes
func SectorIDToBytes(sectorID uint64) [31]byte {
slice := make([]byte, 31)
binary.LittleEndian.PutUint64(slice, sectorID)
var sectorIDAsBytes [31]byte
copy(sectorIDAsBytes[:], slice)
return sectorIDAsBytes
}