Permalink
Browse files

refactored create_value_request

 - split into smaller methods
 - remove superfluous comments
 - added verbosity
  • Loading branch information...
1 parent b447859 commit 36689ed119c8173bf4ac44080181b7b96bcc4e11 @xorrbit xorrbit committed Apr 14, 2012
Showing with 31 additions and 33 deletions.
  1. +31 −33 lib/mintchip.rb
View
@@ -56,7 +56,7 @@ def load_value(vm)
end
# converts an integer to binary coded decimal
- def to_bcd(n)
+ def to_binary_coded_decimal(n)
str = n.to_s
bin = ""
str.each_char do |c|
@@ -65,47 +65,45 @@ def to_bcd(n)
bin
end
- # creates a base64 encoded value request message
- def create_value_request(value, annotation_ = "", response_url_ = "")
- # we need the currency code and MintChip ID so first we'll grab the info from the server
- info = info()
-
- message_version = OpenSSL::ASN1::Enumerated.new(1, 0, :EXPLICIT)
+ # 64 bit bcd representation, padded with zeros, in ascii
+ def to_padded_ascii_binary_coded_decimal(value, length_in_bits, pad_character = '0')
+ res = to_binary_coded_decimal(value).to_s
+ res = res.rjust(length_in_bits, pad_character)
+ res = [res].pack("B*")
+ end
- # annotation is optional
- annotation = OpenSSL::ASN1::IA5String.new(annotation_, 1, :EXPLICIT)
+ def to_octet_string(value, tag = -1, tagging = :EXPLICIT)
+ if tag == -1 then
+ res = OpenSSL::ASN1::OctetString.new(value)
+ else
+ res = OpenSSL::ASN1::OctetString.new(value, tag, tagging)
+ end
+ res
+ end
- # 64 bit bcd representation, padded with zeros, in ascii
- payee_id_ = to_bcd(info.id).to_s.rjust(64, "0")
- payee_id_ = [payee_id_].pack("B*")
- # and encode it
- payee_id = OpenSSL::ASN1::OctetString.new(payee_id_)
-
+ def create_value_message_request_packet(value, response_url_ = "")
+ payee_id = to_octet_string(to_padded_ascii_binary_coded_decimal(info.id, 64))
currency_code = OpenSSL::ASN1::OctetString.new(info.currency_code.chr)
-
- # bcd representation, padded with leading zeroes to 24 bits (3 bytes), in ascii
- transfer_value_ = to_bcd(value).to_s.rjust(24, "0")
- transfer_value_ = [transfer_value_].pack("B*")
- # and encode it
- transfer_value = OpenSSL::ASN1::OctetString.new(transfer_value_)
-
+ transfer_value = to_octet_string(to_padded_ascii_binary_coded_decimal(value, 24))
include_cert = OpenSSL::ASN1::Boolean.new(true)
-
- # this part is optional also
response_url = OpenSSL::ASN1::IA5String(response_url_)
-
- random_challenge = OpenSSL::ASN1::OctetString.new(Random.new().bytes(4), 0, :IMPLICIT)
-
- # now we create the 1-tagged ASN1Data inner packet containing a sequence
+ random_challenge = to_octet_string(Random.new().bytes(4), 0, :IMPLICIT)
+
packet_ = OpenSSL::ASN1::Sequence.new( [ payee_id, currency_code, transfer_value, include_cert, response_url, random_challenge ], 1, :EXPLICIT )
- # and finally the 2-tagged ASN1Data outer packet
packet = OpenSSL::ASN1::ASN1Data.new( [ packet_ ] , 2, :CONTEXT_SPECIFIC)
+ end
- # and package all that in 0-tagged APPLICATION message
- message = OpenSSL::ASN1::Sequence.new( [ message_version, annotation, packet ], 0, :EXPLICIT, :APPLICATION )
+ def create_mintchip_message(packet, annotation_ = "")
+ message_version = OpenSSL::ASN1::Enumerated.new(1, 0, :EXPLICIT)
+ annotation = OpenSSL::ASN1::IA5String.new(annotation_, 1, :EXPLICIT)
+ message = OpenSSL::ASN1::Sequence.new( [ message_version, annotation, packet ], 0, :EXPLICIT, :APPLICATION)
+ end
- # DER encode it all and base64 it
- vrm = Base64.strict_encode64(message.to_der)
+ # creates a base64 encoded value request message
+ def create_value_request(value, annotation_ = "", response_url_ = "")
+ packet = create_value_message_request_packet(value, response_url_)
+ message = create_mintchip_message(packet, annotation_)
+ encoded_value_request_message = Base64.strict_encode64(message.to_der)
end
# POST /payments/request

0 comments on commit 36689ed

Please sign in to comment.