zcash-primitives-js/src/types.js

72 lines
1.7 KiB
JavaScript
Raw Normal View History

2017-05-31 06:13:38 -07:00
var typeforce = require('typeforce')
function Buffer252bit (value) {
return typeforce.BufferN(32)(value) && (value[0] & 0x0f) === value[0]
}
2017-06-01 19:35:08 -07:00
var ZATOSHI_MAX = 21 * 1e14
function Zatoshi (value) {
return typeforce.UInt53(value) && value <= ZATOSHI_MAX
}
2017-05-31 06:13:38 -07:00
function BoolNum (value) {
return typeforce.Number(value) &&
value >= 0 &&
value <= 1
}
2017-05-31 06:16:33 -07:00
// exposed, external API
2017-06-01 19:35:08 -07:00
var Note = typeforce.compile({
a_pk: typeforce.BufferN(32),
value: Zatoshi,
rho: typeforce.BufferN(32),
r: typeforce.BufferN(32)
})
2017-05-31 06:16:33 -07:00
var PaymentAddress = typeforce.compile({
a_pk: typeforce.BufferN(32),
pk_enc: typeforce.BufferN(32)
})
var SpendingKey = typeforce.compile({
a_sk: Buffer252bit
})
var ZCIncrementalMerkleTree = typeforce.compile({
left: typeforce.maybe(typeforce.BufferN(32)),
right: typeforce.maybe(typeforce.BufferN(32)),
parents: [typeforce.maybe(typeforce.BufferN(32))]
})
var ZCIncrementalWitness = typeforce.compile({
tree: ZCIncrementalMerkleTree,
filled: [typeforce.BufferN(32)],
cursor: typeforce.maybe(ZCIncrementalMerkleTree)
})
var JSInput = typeforce.compile({
witness: ZCIncrementalWitness,
note: Note,
key: SpendingKey
})
var JSOutput = typeforce.compile({
addr: PaymentAddress,
value: Zatoshi,
memo: typeforce.Buffer
})
2017-05-31 06:16:33 -07:00
2017-05-31 06:13:38 -07:00
// extend typeforce types with ours
var types = {
BoolNum: BoolNum,
Buffer252bit: Buffer252bit,
2017-05-31 06:16:33 -07:00
Buffer256bit: typeforce.BufferN(32),
JSInput: JSInput,
JSOutput: JSOutput,
2017-06-01 19:35:08 -07:00
Note: Note,
2017-05-31 06:16:33 -07:00
PaymentAddress: PaymentAddress,
2017-06-01 19:35:08 -07:00
SpendingKey: SpendingKey,
ZCIncrementalWitness: ZCIncrementalWitness,
2017-06-01 19:35:08 -07:00
Zatoshi: Zatoshi
2017-05-31 06:13:38 -07:00
}
for (var typeName in typeforce) {
types[typeName] = typeforce[typeName]
}
module.exports = types