Skip to content

Commit c365cea

Browse files
authored
chore: code reduction on doHash (#377)
1 parent 2c2c068 commit c365cea

File tree

1 file changed

+5
-29
lines changed

1 file changed

+5
-29
lines changed

go/ops.go

+5-29
Original file line numberDiff line numberDiff line change
@@ -190,41 +190,17 @@ func doHash(hashOp HashOp, preimage []byte) ([]byte, error) {
190190
return hashBz(crypto.RIPEMD160, preimage)
191191
case HashOp_BITCOIN:
192192
// ripemd160(sha256(x))
193-
sha := crypto.SHA256.New()
194-
_, err := sha.Write(preimage)
193+
tmp, err := hashBz(crypto.SHA256, preimage)
195194
if err != nil {
196195
return nil, err
197196
}
198-
tmp := sha.Sum(nil)
199-
bitcoinHash := crypto.RIPEMD160.New()
200-
_, err = bitcoinHash.Write(tmp)
201-
if err != nil {
202-
return nil, err
203-
}
204-
return bitcoinHash.Sum(nil), nil
197+
return hashBz(crypto.RIPEMD160, tmp)
205198
case HashOp_SHA512_256:
206-
shaHash := crypto.SHA512_256.New()
207-
_, err := shaHash.Write(preimage)
208-
if err != nil {
209-
return nil, err
210-
}
211-
return shaHash.Sum(nil), nil
199+
return hashBz(crypto.SHA512_256, preimage)
212200
case HashOp_BLAKE2B_512:
213-
blakeHash := crypto.BLAKE2b_512.New()
214-
_, err := blakeHash.Write(preimage)
215-
if err != nil {
216-
return nil, err
217-
}
218-
return blakeHash.Sum(nil), nil
201+
return hashBz(crypto.BLAKE2b_512, preimage)
219202
case HashOp_BLAKE2S_256:
220-
blakeHash := crypto.BLAKE2s_256.New()
221-
_, err := blakeHash.Write(preimage)
222-
if err != nil {
223-
return nil, err
224-
}
225-
return blakeHash.Sum(nil), nil
226-
// TODO: there doesn't seem to be an "official" implementation of BLAKE3 in Go,
227-
// so we are unable to support it for now
203+
return hashBz(crypto.BLAKE2s_256, preimage)
228204
}
229205
return nil, fmt.Errorf("unsupported hashop: %d", hashOp)
230206
}

0 commit comments

Comments
 (0)