From bd92c8b10aeb3bca6cdadd8d22b6385256b524f1 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Tue, 21 May 2019 20:12:17 -0500 Subject: [PATCH] Switch IntegrityCheck encoding to multihash --- dhall.gemspec | 1 + lib/dhall/ast.rb | 16 +++++----------- lib/dhall/binary.rb | 19 ++++++++++++++----- lib/dhall/parser.rb | 7 ++++++- test/test_resolve.rb | 5 +++-- 5 files changed, 29 insertions(+), 19 deletions(-) diff --git a/dhall.gemspec b/dhall.gemspec index 8507478..69912c6 100644 --- a/dhall.gemspec +++ b/dhall.gemspec @@ -27,6 +27,7 @@ Gem::Specification.new do |spec| spec.add_dependency "cbor", "~> 0.5.9.3" spec.add_dependency "citrus", "~> 3.0" + spec.add_dependency "multihashes", "~> 0.1.3" spec.add_dependency "promise.rb", "~> 0.7.4" spec.add_dependency "value_semantics", "~> 3.0" diff --git a/lib/dhall/ast.rb b/lib/dhall/ast.rb index b3d9689..22494b7 100644 --- a/lib/dhall/ast.rb +++ b/lib/dhall/ast.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +require "multihashes" require "uri" require "value_semantics" @@ -1189,25 +1190,18 @@ module Dhall class Import < Expression class IntegrityCheck include(ValueSemantics.for_attributes do - protocol "sha256" - data Either(::String, nil) + code ::Integer + digest ::String end) class FailureException < StandardError; end - def initialize(protocol, data=nil) - super( - protocol: protocol, - data: data - ) - end - def to_s - "#{@protocol}:#{hexdigest}" + "#{Multihashes::TABLE[code].sub(/\Asha2-/, "sha")}:#{hexdigest}" end def hexdigest - @data.unpack("H*").first.encode(Encoding::UTF_8) + digest.unpack("H*").first.encode(Encoding::UTF_8) end def check(expr) diff --git a/lib/dhall/binary.rb b/lib/dhall/binary.rb index a3a9d11..ba45005 100644 --- a/lib/dhall/binary.rb +++ b/lib/dhall/binary.rb @@ -2,6 +2,7 @@ require "cbor" require "digest/sha2" +require "multihashes" require "dhall/ast" require "dhall/builtins" @@ -190,15 +191,23 @@ module Dhall end class Import + class IntegrityCheck + def self.decode(integrity_check) + return unless integrity_check + + IntegrityCheck.new( + Multihashes.decode(integrity_check).select { |k, _| + [:code, :digest].include?(k) + } + ) + end + end + def self.decode(integrity_check, import_type, path_type, *parts) parts[0] = Dhall.decode(parts[0]) if path_type < 2 && !parts[0].nil? - check = if integrity_check - IntegrityCheck.new(integrity_check[0], [integrity_check[1]].pack("H*")) - end - new( - check, + IntegrityCheck.decode(integrity_check), IMPORT_TYPES[import_type], PATH_TYPES[path_type].new(*parts) ) diff --git a/lib/dhall/parser.rb b/lib/dhall/parser.rb index 5418e73..04dcaca 100644 --- a/lib/dhall/parser.rb +++ b/lib/dhall/parser.rb @@ -525,7 +525,12 @@ module Dhall module Hash def value protocol, data = string.split(/:/, 2) - Dhall::Import::IntegrityCheck.new(protocol, [data].pack("H*")) + Dhall::Import::IntegrityCheck.new( + code: Multihashes::TABLE.key( + protocol.sub(/\Asha(\d{3})/, "sha2-\\1") + ), + digest: [data].pack("H*") + ) end end diff --git a/test/test_resolve.rb b/test/test_resolve.rb index d7d4b93..b270932 100644 --- a/test/test_resolve.rb +++ b/test/test_resolve.rb @@ -167,7 +167,7 @@ class TestResolve < Minitest::Test def test_integrity_check_failure expr = Dhall::Import.new( - Dhall::Import::IntegrityCheck.new("sha256", "badhash"), + Dhall::Import::IntegrityCheck.new(code: 0x12, digest: "badhash".b), Dhall::Import::Expression, Dhall::Import::RelativePath.new("var") ) @@ -260,7 +260,8 @@ class TestResolve < Minitest::Test expr = Dhall::Import.new( Dhall::Import::IntegrityCheck.new( - "sha256", Dhall::Variable["_"].digest.digest + code: 0x12, + digest: Dhall::Variable["_"].digest.digest ), Dhall::Import::Expression, Dhall::Import::Http.new(nil, "example.com", "thing.dhall", nil) -- 2.38.5